blob: d0c32eb77c68693c525b04726c97c67ee0db9273 [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"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000032#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000033#include "SkRRect.h"
34#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000035#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000036#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000037#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000038#include "SkVertState.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkErrorInternals.h"
40
41#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
42
43#if 0
44 extern bool (*gShouldDrawProc)();
45 #define CHECK_SHOULD_DRAW(draw, forceI) \
46 do { \
47 if (gShouldDrawProc && !gShouldDrawProc()) return; \
48 this->prepareDraw(draw, forceI); \
49 } while (0)
50#else
51 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
52#endif
53
54// This constant represents the screen alignment criterion in texels for
55// requiring texture domain clamping to prevent color bleeding when drawing
56// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000057#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000058
59#define DO_DEFERRED_CLEAR() \
60 do { \
61 if (fNeedClear) { \
62 this->clear(SK_ColorTRANSPARENT); \
63 } \
64 } while (false) \
65
66///////////////////////////////////////////////////////////////////////////////
67
68#define CHECK_FOR_ANNOTATION(paint) \
69 do { if (paint.getAnnotation()) { return; } } while (0)
70
71///////////////////////////////////////////////////////////////////////////////
72
73
74class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
75public:
76 SkAutoCachedTexture()
77 : fDevice(NULL)
78 , fTexture(NULL) {
79 }
80
81 SkAutoCachedTexture(SkGpuDevice* device,
82 const SkBitmap& bitmap,
83 const GrTextureParams* params,
84 GrTexture** texture)
85 : fDevice(NULL)
86 , fTexture(NULL) {
87 SkASSERT(NULL != texture);
88 *texture = this->set(device, bitmap, params);
89 }
90
91 ~SkAutoCachedTexture() {
92 if (NULL != fTexture) {
93 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
94 }
95 }
96
97 GrTexture* set(SkGpuDevice* device,
98 const SkBitmap& bitmap,
99 const GrTextureParams* params) {
100 if (NULL != fTexture) {
101 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
102 fTexture = NULL;
103 }
104 fDevice = device;
105 GrTexture* result = (GrTexture*)bitmap.getTexture();
106 if (NULL == result) {
107 // Cannot return the native texture so look it up in our cache
108 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
109 result = fTexture;
110 }
111 return result;
112 }
113
114private:
115 SkGpuDevice* fDevice;
116 GrTexture* fTexture;
117};
118
119///////////////////////////////////////////////////////////////////////////////
120
121struct GrSkDrawProcs : public SkDrawProcs {
122public:
123 GrContext* fContext;
124 GrTextContext* fTextContext;
125 GrFontScaler* fFontScaler; // cached in the skia glyphcache
126};
127
128///////////////////////////////////////////////////////////////////////////////
129
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000130/*
131 * GrRenderTarget does not know its opaqueness, only its config, so we have
132 * to make conservative guesses when we return an "equivalent" bitmap.
133 */
134static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000135 SkBitmap bitmap;
reed6c225732014-06-09 19:52:07 -0700136 bitmap.setInfo(renderTarget->info());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000137 return bitmap;
138}
139
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000140SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141 SkASSERT(NULL != surface);
142 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
143 return NULL;
144 }
145 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000146 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000147 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000148 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149 }
150}
151
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000152SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000153 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000154 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000155}
156
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000157SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000159 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000160}
161
162void SkGpuDevice::initFromRenderTarget(GrContext* context,
163 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165 fDrawProcs = NULL;
166
167 fContext = context;
168 fContext->ref();
169
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000170 bool useDFFonts = !!(flags & kDFFonts_Flag);
171 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
172 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000173 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
174
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000175 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000176 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000177
178 SkASSERT(NULL != renderTarget);
179 fRenderTarget = renderTarget;
180 fRenderTarget->ref();
181
182 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
183 // on the RT but not vice-versa.
184 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
185 // busting chrome (for a currently unknown reason).
186 GrSurface* surface = fRenderTarget->asTexture();
187 if (NULL == surface) {
188 surface = fRenderTarget;
189 }
reed@google.combf790232013-12-13 19:45:58 +0000190
reed6c225732014-06-09 19:52:07 -0700191 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
192 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000193
reed@google.com672588b2014-01-08 15:42:01 +0000194 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000195}
196
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000197SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
198 int sampleCount) {
199 if (kUnknown_SkColorType == origInfo.colorType() ||
200 origInfo.width() < 0 || origInfo.height() < 0) {
201 return NULL;
202 }
203
204 SkImageInfo info = origInfo;
205 // TODO: perhas we can loosen this check now that colortype is more detailed
206 // e.g. can we support both RGBA and BGRA here?
207 if (kRGB_565_SkColorType == info.colorType()) {
208 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
209 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000210 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000211 if (kOpaque_SkAlphaType != info.alphaType()) {
212 info.fAlphaType = kPremul_SkAlphaType; // force this setting
213 }
214 }
215
216 GrTextureDesc desc;
217 desc.fFlags = kRenderTarget_GrTextureFlagBit;
218 desc.fWidth = info.width();
219 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000220 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000221 desc.fSampleCnt = sampleCount;
222
223 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
224 if (!texture.get()) {
225 return NULL;
226 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000227
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000228 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
229}
230
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000231SkGpuDevice::~SkGpuDevice() {
232 if (fDrawProcs) {
233 delete fDrawProcs;
234 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000235
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000236 delete fMainTextContext;
237 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000238
239 // The GrContext takes a ref on the target. We don't want to cause the render
240 // target to be unnecessarily kept alive.
241 if (fContext->getRenderTarget() == fRenderTarget) {
242 fContext->setRenderTarget(NULL);
243 }
244
245 if (fContext->getClip() == &fClipData) {
246 fContext->setClip(NULL);
247 }
248
249 SkSafeUnref(fRenderTarget);
250 fContext->unref();
251}
252
253///////////////////////////////////////////////////////////////////////////////
254
255void SkGpuDevice::makeRenderTargetCurrent() {
256 DO_DEFERRED_CLEAR();
257 fContext->setRenderTarget(fRenderTarget);
258}
259
260///////////////////////////////////////////////////////////////////////////////
261
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000262bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
263 int x, int y) {
264 DO_DEFERRED_CLEAR();
265
266 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000267 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000268 if (kUnknown_GrPixelConfig == config) {
269 return false;
270 }
271
272 uint32_t flags = 0;
273 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
274 flags = GrContext::kUnpremul_PixelOpsFlag;
275 }
276 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
277 config, dstPixels, dstRowBytes, flags);
278}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000279
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000280bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
281 int x, int y) {
282 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000283 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000284 if (kUnknown_GrPixelConfig == config) {
285 return false;
286 }
287 uint32_t flags = 0;
288 if (kUnpremul_SkAlphaType == info.alphaType()) {
289 flags = GrContext::kUnpremul_PixelOpsFlag;
290 }
291 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
292
293 // need to bump our genID for compatibility with clients that "know" we have a bitmap
294 this->onAccessBitmap().notifyPixelsChanged();
295
296 return true;
297}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000298
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000299const SkBitmap& SkGpuDevice::onAccessBitmap() {
300 DO_DEFERRED_CLEAR();
301 return INHERITED::onAccessBitmap();
302}
303
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000304void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
305 INHERITED::onAttachToCanvas(canvas);
306
307 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
308 fClipData.fClipStack = canvas->getClipStack();
309}
310
311void SkGpuDevice::onDetachFromCanvas() {
312 INHERITED::onDetachFromCanvas();
313 fClipData.fClipStack = NULL;
314}
315
316// call this every draw call, to ensure that the context reflects our state,
317// and not the state from some other canvas/device
318void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
319 SkASSERT(NULL != fClipData.fClipStack);
320
321 fContext->setRenderTarget(fRenderTarget);
322
323 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
324
325 if (forceIdentity) {
326 fContext->setIdentityMatrix();
327 } else {
328 fContext->setMatrix(*draw.fMatrix);
329 }
330 fClipData.fOrigin = this->getOrigin();
331
332 fContext->setClip(&fClipData);
333
334 DO_DEFERRED_CLEAR();
335}
336
337GrRenderTarget* SkGpuDevice::accessRenderTarget() {
338 DO_DEFERRED_CLEAR();
339 return fRenderTarget;
340}
341
342///////////////////////////////////////////////////////////////////////////////
343
344SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
347SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
348SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
349 shader_type_mismatch);
350SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
351 shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
353SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
354
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000357void SkGpuDevice::clear(SkColor color) {
358 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
359 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
360 fNeedClear = false;
361}
362
363void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
364 CHECK_SHOULD_DRAW(draw, false);
365
366 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000367 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000368
369 fContext->drawPaint(grPaint);
370}
371
372// must be in SkCanvas::PointMode order
373static const GrPrimitiveType gPointMode2PrimtiveType[] = {
374 kPoints_GrPrimitiveType,
375 kLines_GrPrimitiveType,
376 kLineStrip_GrPrimitiveType
377};
378
379void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
380 size_t count, const SkPoint pts[], const SkPaint& paint) {
381 CHECK_FOR_ANNOTATION(paint);
382 CHECK_SHOULD_DRAW(draw, false);
383
384 SkScalar width = paint.getStrokeWidth();
385 if (width < 0) {
386 return;
387 }
388
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000389 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700390 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
391 GrPaint grPaint;
392 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
393 SkPath path;
394 path.moveTo(pts[0]);
395 path.lineTo(pts[1]);
396 fContext->drawPath(grPaint, path, strokeInfo);
397 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000398 }
399
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000400 // we only handle hairlines and paints without path effects or mask filters,
401 // else we let the SkDraw call our drawPath()
402 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
403 draw.drawPoints(mode, count, pts, paint, true);
404 return;
405 }
406
407 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000408 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000409
410 fContext->drawVertices(grPaint,
411 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000412 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000413 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000414 NULL,
415 NULL,
416 NULL,
417 0);
418}
419
420///////////////////////////////////////////////////////////////////////////////
421
422void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
423 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700424 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
425
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000426 CHECK_FOR_ANNOTATION(paint);
427 CHECK_SHOULD_DRAW(draw, false);
428
429 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
430 SkScalar width = paint.getStrokeWidth();
431
432 /*
433 We have special code for hairline strokes, miter-strokes, bevel-stroke
434 and fills. Anything else we just call our path code.
435 */
436 bool usePath = doStroke && width > 0 &&
437 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
438 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
439 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700440
441 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000442 usePath = true;
443 }
egdanield58a0ba2014-06-11 10:30:05 -0700444
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000445 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
446#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
447 if (doStroke) {
448#endif
449 usePath = true;
450#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
451 } else {
452 usePath = !fContext->getMatrix().preservesRightAngles();
453 }
454#endif
455 }
456 // until we can both stroke and fill rectangles
457 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
458 usePath = true;
459 }
460
egdanield58a0ba2014-06-11 10:30:05 -0700461 GrStrokeInfo strokeInfo(paint);
462
463 const SkPathEffect* pe = paint.getPathEffect();
464 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
465 usePath = true;
466 }
467
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000468 if (usePath) {
469 SkPath path;
470 path.addRect(rect);
471 this->drawPath(draw, path, paint, NULL, true);
472 return;
473 }
474
475 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000476 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700477
478 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000479}
480
481///////////////////////////////////////////////////////////////////////////////
482
483void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
484 const SkPaint& paint) {
485 CHECK_FOR_ANNOTATION(paint);
486 CHECK_SHOULD_DRAW(draw, false);
487
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000488 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000489 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
egdanield58a0ba2014-06-11 10:30:05 -0700490
491 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000492 if (paint.getMaskFilter()) {
493 // try to hit the fast path for drawing filtered round rects
494
495 SkRRect devRRect;
496 if (rect.transform(fContext->getMatrix(), &devRRect)) {
497 if (devRRect.allCornersCircular()) {
498 SkRect maskRect;
499 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
500 draw.fClip->getBounds(),
501 fContext->getMatrix(),
502 &maskRect)) {
503 SkIRect finalIRect;
504 maskRect.roundOut(&finalIRect);
505 if (draw.fClip->quickReject(finalIRect)) {
506 // clipped out
507 return;
508 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000509 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700510 strokeInfo.getStrokeRec(),
511 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000512 return;
513 }
514 }
515
516 }
517 }
518
519 }
520
egdanield58a0ba2014-06-11 10:30:05 -0700521 bool usePath = false;
522
523 if (paint.getMaskFilter()) {
524 usePath = true;
525 } else {
526 const SkPathEffect* pe = paint.getPathEffect();
527 if (NULL != pe && !strokeInfo.isDashed()) {
528 usePath = true;
529 }
530 }
531
532
533 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000534 SkPath path;
535 path.addRRect(rect);
536 this->drawPath(draw, path, paint, NULL, true);
537 return;
538 }
egdanield58a0ba2014-06-11 10:30:05 -0700539
540 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541}
542
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000543void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
544 const SkRRect& inner, const SkPaint& paint) {
545 SkStrokeRec stroke(paint);
546 if (stroke.isFillStyle()) {
547
548 CHECK_FOR_ANNOTATION(paint);
549 CHECK_SHOULD_DRAW(draw, false);
550
551 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000552 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000553
554 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
555 fContext->drawDRRect(grPaint, outer, inner);
556 return;
557 }
558 }
559
560 SkPath path;
561 path.addRRect(outer);
562 path.addRRect(inner);
563 path.setFillType(SkPath::kEvenOdd_FillType);
564
565 this->drawPath(draw, path, paint, NULL, true);
566}
567
568
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000569/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570
571void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
572 const SkPaint& paint) {
573 CHECK_FOR_ANNOTATION(paint);
574 CHECK_SHOULD_DRAW(draw, false);
575
egdanield58a0ba2014-06-11 10:30:05 -0700576 GrStrokeInfo strokeInfo(paint);
577
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000578 bool usePath = false;
579 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700580 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000581 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700582 } else {
583 const SkPathEffect* pe = paint.getPathEffect();
584 if (NULL != pe && !strokeInfo.isDashed()) {
585 usePath = true;
586 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000587 }
588
589 if (usePath) {
590 SkPath path;
591 path.addOval(oval);
592 this->drawPath(draw, path, paint, NULL, true);
593 return;
594 }
595
596 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000597 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598
egdanield58a0ba2014-06-11 10:30:05 -0700599 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000600}
601
602#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000603
604///////////////////////////////////////////////////////////////////////////////
605
606// helpers for applying mask filters
607namespace {
608
609// Draw a mask using the supplied paint. Since the coverage/geometry
610// is already burnt into the mask this boils down to a rect draw.
611// Return true if the mask was successfully drawn.
612bool draw_mask(GrContext* context, const SkRect& maskRect,
613 GrPaint* grp, GrTexture* mask) {
614 GrContext::AutoMatrix am;
615 if (!am.setIdentity(context, grp)) {
616 return false;
617 }
618
619 SkMatrix matrix;
620 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
621 matrix.postIDiv(mask->width(), mask->height());
622
623 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
624 context->drawRect(*grp, maskRect);
625 return true;
626}
627
628bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700629 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000630 GrPaint* grp, SkPaint::Style style) {
631 SkMask srcM, dstM;
632
633 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
634 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
635 return false;
636 }
637 SkAutoMaskFreeImage autoSrc(srcM.fImage);
638
639 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
640 return false;
641 }
642 // this will free-up dstM when we're done (allocated in filterMask())
643 SkAutoMaskFreeImage autoDst(dstM.fImage);
644
645 if (clip.quickReject(dstM.fBounds)) {
646 return false;
647 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648
649 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
650 // the current clip (and identity matrix) and GrPaint settings
651 GrTextureDesc desc;
652 desc.fWidth = dstM.fBounds.width();
653 desc.fHeight = dstM.fBounds.height();
654 desc.fConfig = kAlpha_8_GrPixelConfig;
655
656 GrAutoScratchTexture ast(context, desc);
657 GrTexture* texture = ast.texture();
658
659 if (NULL == texture) {
660 return false;
661 }
662 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
663 dstM.fImage, dstM.fRowBytes);
664
665 SkRect maskRect = SkRect::Make(dstM.fBounds);
666
667 return draw_mask(context, maskRect, grp, texture);
668}
669
670// Create a mask of 'devPath' and place the result in 'mask'. Return true on
671// success; false otherwise.
672bool create_mask_GPU(GrContext* context,
673 const SkRect& maskRect,
674 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700675 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000676 bool doAA,
677 GrAutoScratchTexture* mask) {
678 GrTextureDesc desc;
679 desc.fFlags = kRenderTarget_GrTextureFlagBit;
680 desc.fWidth = SkScalarCeilToInt(maskRect.width());
681 desc.fHeight = SkScalarCeilToInt(maskRect.height());
682 // We actually only need A8, but it often isn't supported as a
683 // render target so default to RGBA_8888
684 desc.fConfig = kRGBA_8888_GrPixelConfig;
685 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
686 desc.fConfig = kAlpha_8_GrPixelConfig;
687 }
688
689 mask->set(context, desc);
690 if (NULL == mask->texture()) {
691 return false;
692 }
693
694 GrTexture* maskTexture = mask->texture();
695 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
696
697 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
698 GrContext::AutoClip ac(context, clipRect);
699
700 context->clear(NULL, 0x0, true);
701
702 GrPaint tempPaint;
703 if (doAA) {
704 tempPaint.setAntiAlias(true);
705 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
706 // blend coeff of zero requires dual source blending support in order
707 // to properly blend partially covered pixels. This means the AA
708 // code path may not be taken. So we use a dst blend coeff of ISA. We
709 // could special case AA draws to a dst surface with known alpha=0 to
710 // use a zero dst coeff when dual source blending isn't available.
711 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
712 }
713
714 GrContext::AutoMatrix am;
715
716 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
717 SkMatrix translate;
718 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
719 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700720 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721 return true;
722}
723
724SkBitmap wrap_texture(GrTexture* texture) {
725 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700726 result.setInfo(texture->info());
727 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000728 return result;
729}
730
731};
732
733void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
734 const SkPaint& paint, const SkMatrix* prePathMatrix,
735 bool pathIsMutable) {
736 CHECK_FOR_ANNOTATION(paint);
737 CHECK_SHOULD_DRAW(draw, false);
738
739 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000740 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 // If we have a prematrix, apply it to the path, optimizing for the case
743 // where the original path can in fact be modified in place (even though
744 // its parameter type is const).
745 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000746 SkTLazy<SkPath> tmpPath;
747 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000748
749 if (prePathMatrix) {
750 SkPath* result = pathPtr;
751
752 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000753 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 pathIsMutable = true;
755 }
756 // should I push prePathMatrix on our MV stack temporarily, instead
757 // of applying it here? See SkDraw.cpp
758 pathPtr->transform(*prePathMatrix, result);
759 pathPtr = result;
760 }
761 // at this point we're done with prePathMatrix
762 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
763
egdanield58a0ba2014-06-11 10:30:05 -0700764 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 SkPathEffect* pathEffect = paint.getPathEffect();
766 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700767 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
768 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000769 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000770 pathPtr = effectPath.get();
771 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700772 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 }
774
egdanield58a0ba2014-06-11 10:30:05 -0700775 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 if (paint.getMaskFilter()) {
777 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000778 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
779 if (stroke.applyToPath(strokedPath, *pathPtr)) {
780 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700782 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000783 }
784 }
785
786 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000787 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000788
789 // transform the path into device space
790 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000791
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000792 SkRect maskRect;
793 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
794 draw.fClip->getBounds(),
795 fContext->getMatrix(),
796 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000797 // The context's matrix may change while creating the mask, so save the CTM here to
798 // pass to filterMaskGPU.
799 const SkMatrix ctm = fContext->getMatrix();
800
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000801 SkIRect finalIRect;
802 maskRect.roundOut(&finalIRect);
803 if (draw.fClip->quickReject(finalIRect)) {
804 // clipped out
805 return;
806 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000807
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000808 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000809 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000810 // the mask filter was able to draw itself directly, so there's nothing
811 // left to do.
812 return;
813 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000814
815 GrAutoScratchTexture mask;
816
egdanield58a0ba2014-06-11 10:30:05 -0700817 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000818 grPaint.isAntiAlias(), &mask)) {
819 GrTexture* filtered;
820
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000821 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000822 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823 // filterMaskGPU gives us ownership of a ref to the result
824 SkAutoTUnref<GrTexture> atu(filtered);
825
826 // If the scratch texture that we used as the filter src also holds the filter
827 // result then we must detach so that this texture isn't recycled for a later
828 // draw.
829 if (filtered == mask.texture()) {
830 mask.detach();
831 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
832 }
833
834 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
835 // This path is completely drawn
836 return;
837 }
838 }
839 }
840 }
841
842 // draw the mask on the CPU - this is a fallthrough path in case the
843 // GPU path fails
844 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
845 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700846 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
847 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000848 return;
849 }
850
egdanield58a0ba2014-06-11 10:30:05 -0700851 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000852}
853
854static const int kBmpSmallTileSize = 1 << 10;
855
856static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
857 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
858 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
859 return tilesX * tilesY;
860}
861
862static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
863 if (maxTileSize <= kBmpSmallTileSize) {
864 return maxTileSize;
865 }
866
867 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
868 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
869
870 maxTileTotalTileSize *= maxTileSize * maxTileSize;
871 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
872
873 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
874 return kBmpSmallTileSize;
875 } else {
876 return maxTileSize;
877 }
878}
879
880// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
881// pixels from the bitmap are necessary.
882static void determine_clipped_src_rect(const GrContext* context,
883 const SkBitmap& bitmap,
884 const SkRect* srcRectPtr,
885 SkIRect* clippedSrcIRect) {
886 const GrClipData* clip = context->getClip();
887 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
888 SkMatrix inv;
889 if (!context->getMatrix().invert(&inv)) {
890 clippedSrcIRect->setEmpty();
891 return;
892 }
893 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
894 inv.mapRect(&clippedSrcRect);
895 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000896 // we've setup src space 0,0 to map to the top left of the src rect.
897 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000898 if (!clippedSrcRect.intersect(*srcRectPtr)) {
899 clippedSrcIRect->setEmpty();
900 return;
901 }
902 }
903 clippedSrcRect.roundOut(clippedSrcIRect);
904 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
905 if (!clippedSrcIRect->intersect(bmpBounds)) {
906 clippedSrcIRect->setEmpty();
907 }
908}
909
910bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
911 const GrTextureParams& params,
912 const SkRect* srcRectPtr,
913 int maxTileSize,
914 int* tileSize,
915 SkIRect* clippedSrcRect) const {
916 // if bitmap is explictly texture backed then just use the texture
917 if (NULL != bitmap.getTexture()) {
918 return false;
919 }
920
921 // if it's larger than the max tile size, then we have no choice but tiling.
922 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
923 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
924 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
925 return true;
926 }
927
928 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
929 return false;
930 }
931
932 // if the entire texture is already in our cache then no reason to tile it
933 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
934 return false;
935 }
936
937 // At this point we know we could do the draw by uploading the entire bitmap
938 // as a texture. However, if the texture would be large compared to the
939 // cache size and we don't require most of it for this draw then tile to
940 // reduce the amount of upload and cache spill.
941
942 // assumption here is that sw bitmap size is a good proxy for its size as
943 // a texture
944 size_t bmpSize = bitmap.getSize();
945 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000946 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947 if (bmpSize < cacheSize / 2) {
948 return false;
949 }
950
951 // Figure out how much of the src we will need based on the src rect and clipping.
952 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
953 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
954 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
955 kBmpSmallTileSize * kBmpSmallTileSize;
956
957 return usedTileBytes < 2 * bmpSize;
958}
959
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000960void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 const SkBitmap& bitmap,
962 const SkMatrix& m,
963 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000964 SkMatrix concat;
965 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
966 if (!m.isIdentity()) {
967 concat.setConcat(*draw->fMatrix, m);
968 draw.writable()->fMatrix = &concat;
969 }
970 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000971}
972
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000973// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000974// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
975// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000976static inline void clamped_outset_with_offset(SkIRect* iRect,
977 int outset,
978 SkPoint* offset,
979 const SkIRect& clamp) {
980 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000981
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982 int leftClampDelta = clamp.fLeft - iRect->fLeft;
983 if (leftClampDelta > 0) {
984 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 iRect->fLeft = clamp.fLeft;
986 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000987 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000988 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000989
990 int topClampDelta = clamp.fTop - iRect->fTop;
991 if (topClampDelta > 0) {
992 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 iRect->fTop = clamp.fTop;
994 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000995 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000996 }
997
998 if (iRect->fRight > clamp.fRight) {
999 iRect->fRight = clamp.fRight;
1000 }
1001 if (iRect->fBottom > clamp.fBottom) {
1002 iRect->fBottom = clamp.fBottom;
1003 }
1004}
1005
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001006static bool has_aligned_samples(const SkRect& srcRect,
1007 const SkRect& transformedRect) {
1008 // detect pixel disalignment
1009 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1010 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1011 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1012 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1013 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1014 COLOR_BLEED_TOLERANCE &&
1015 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1016 COLOR_BLEED_TOLERANCE) {
1017 return true;
1018 }
1019 return false;
1020}
1021
1022static bool may_color_bleed(const SkRect& srcRect,
1023 const SkRect& transformedRect,
1024 const SkMatrix& m) {
1025 // Only gets called if has_aligned_samples returned false.
1026 // So we can assume that sampling is axis aligned but not texel aligned.
1027 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1028 SkRect innerSrcRect(srcRect), innerTransformedRect,
1029 outerTransformedRect(transformedRect);
1030 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1031 m.mapRect(&innerTransformedRect, innerSrcRect);
1032
1033 // The gap between outerTransformedRect and innerTransformedRect
1034 // represents the projection of the source border area, which is
1035 // problematic for color bleeding. We must check whether any
1036 // destination pixels sample the border area.
1037 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1038 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1039 SkIRect outer, inner;
1040 outerTransformedRect.round(&outer);
1041 innerTransformedRect.round(&inner);
1042 // If the inner and outer rects round to the same result, it means the
1043 // border does not overlap any pixel centers. Yay!
1044 return inner != outer;
1045}
1046
1047static bool needs_texture_domain(const SkBitmap& bitmap,
1048 const SkRect& srcRect,
1049 GrTextureParams &params,
1050 const SkMatrix& contextMatrix,
1051 bool bicubic) {
1052 bool needsTextureDomain = false;
1053
1054 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1055 // Need texture domain if drawing a sub rect
1056 needsTextureDomain = srcRect.width() < bitmap.width() ||
1057 srcRect.height() < bitmap.height();
1058 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1059 // sampling is axis-aligned
1060 SkRect transformedRect;
1061 contextMatrix.mapRect(&transformedRect, srcRect);
1062
1063 if (has_aligned_samples(srcRect, transformedRect)) {
1064 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1065 needsTextureDomain = false;
1066 } else {
1067 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1068 }
1069 }
1070 }
1071 return needsTextureDomain;
1072}
1073
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1075 const SkBitmap& bitmap,
1076 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001077 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001078 const SkPaint& paint,
1079 SkCanvas::DrawBitmapRectFlags flags) {
1080 CHECK_SHOULD_DRAW(draw, false);
1081
1082 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001084 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1085 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001086 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001087 SkScalar w = SkIntToScalar(bitmap.width());
1088 SkScalar h = SkIntToScalar(bitmap.height());
1089 dstSize.fWidth = w;
1090 dstSize.fHeight = h;
1091 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001092 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001094 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001097 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1098 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1099 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1100 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001101 }
1102
1103 if (paint.getMaskFilter()){
1104 // Convert the bitmap to a shader so that the rect can be drawn
1105 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 SkBitmap tmp; // subset of bitmap, if necessary
1107 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001108 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001109 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1111 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1112 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001113 // In bleed mode we position and trim the bitmap based on the src rect which is
1114 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1115 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1116 // compensate.
1117 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1118 SkIRect iSrc;
1119 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001121 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1122 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001123
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001124 if (!bitmap.extractSubset(&tmp, iSrc)) {
1125 return; // extraction failed
1126 }
1127 bitmapPtr = &tmp;
1128 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001129
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001130 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001131 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001133 } else {
1134 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001135 }
1136
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001137 SkPaint paintWithShader(paint);
1138 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001139 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001140 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1141 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001142
1143 return;
1144 }
1145
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1147 // the view matrix rather than a local matrix.
1148 SkMatrix m;
1149 m.setScale(dstSize.fWidth / srcRect.width(),
1150 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 fContext->concatMatrix(m);
1152
1153 GrTextureParams params;
1154 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1155 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001156
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001157 bool doBicubic = false;
1158
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 switch(paintFilterLevel) {
1160 case SkPaint::kNone_FilterLevel:
1161 textureFilterMode = GrTextureParams::kNone_FilterMode;
1162 break;
1163 case SkPaint::kLow_FilterLevel:
1164 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1165 break;
1166 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001167 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001168 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1169 } else {
1170 // Don't trigger MIP level generation unnecessarily.
1171 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1172 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001173 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001174 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001175 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001176 doBicubic =
1177 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001178 break;
1179 default:
1180 SkErrorInternals::SetError( kInvalidPaint_SkError,
1181 "Sorry, I don't understand the filtering "
1182 "mode you asked for. Falling back to "
1183 "MIPMaps.");
1184 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1185 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 }
1187
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001188 int tileFilterPad;
1189 if (doBicubic) {
1190 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1191 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1192 tileFilterPad = 0;
1193 } else {
1194 tileFilterPad = 1;
1195 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 params.setFilterMode(textureFilterMode);
1197
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001198 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001199 int tileSize;
1200
1201 SkIRect clippedSrcRect;
1202 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1203 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001204 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1205 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001206 } else {
1207 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001208 bool needsTextureDomain = needs_texture_domain(bitmap,
1209 srcRect,
1210 params,
1211 fContext->getMatrix(),
1212 doBicubic);
1213 this->internalDrawBitmap(bitmap,
1214 srcRect,
1215 params,
1216 paint,
1217 flags,
1218 doBicubic,
1219 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001220 }
1221}
1222
1223// Break 'bitmap' into several tiles to draw it since it has already
1224// been determined to be too large to fit in VRAM
1225void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1226 const SkRect& srcRect,
1227 const SkIRect& clippedSrcIRect,
1228 const GrTextureParams& params,
1229 const SkPaint& paint,
1230 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001231 int tileSize,
1232 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001233 // The following pixel lock is technically redundant, but it is desirable
1234 // to lock outside of the tile loop to prevent redecoding the whole image
1235 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1236 // is larger than the limit of the discardable memory pool.
1237 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001238 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1239
1240 int nx = bitmap.width() / tileSize;
1241 int ny = bitmap.height() / tileSize;
1242 for (int x = 0; x <= nx; x++) {
1243 for (int y = 0; y <= ny; y++) {
1244 SkRect tileR;
1245 tileR.set(SkIntToScalar(x * tileSize),
1246 SkIntToScalar(y * tileSize),
1247 SkIntToScalar((x + 1) * tileSize),
1248 SkIntToScalar((y + 1) * tileSize));
1249
1250 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1251 continue;
1252 }
1253
1254 if (!tileR.intersect(srcRect)) {
1255 continue;
1256 }
1257
1258 SkBitmap tmpB;
1259 SkIRect iTileR;
1260 tileR.roundOut(&iTileR);
1261 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1262 SkIntToScalar(iTileR.fTop));
1263
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001264 // Adjust the context matrix to draw at the right x,y in device space
1265 SkMatrix tmpM;
1266 GrContext::AutoMatrix am;
1267 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1268 am.setPreConcat(fContext, tmpM);
1269
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001270 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001271 SkIRect iClampRect;
1272
1273 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1274 // In bleed mode we want to always expand the tile on all edges
1275 // but stay within the bitmap bounds
1276 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1277 } else {
1278 // In texture-domain/clamp mode we only want to expand the
1279 // tile on edges interior to "srcRect" (i.e., we want to
1280 // not bleed across the original clamped edges)
1281 srcRect.roundOut(&iClampRect);
1282 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001283 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1284 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001285 }
1286
1287 if (bitmap.extractSubset(&tmpB, iTileR)) {
1288 // now offset it to make it "local" to our tmp bitmap
1289 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001290 GrTextureParams paramsTemp = params;
1291 bool needsTextureDomain = needs_texture_domain(bitmap,
1292 srcRect,
1293 paramsTemp,
1294 fContext->getMatrix(),
1295 bicubic);
1296 this->internalDrawBitmap(tmpB,
1297 tileR,
1298 paramsTemp,
1299 paint,
1300 flags,
1301 bicubic,
1302 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001303 }
1304 }
1305 }
1306}
1307
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001308
1309/*
1310 * This is called by drawBitmap(), which has to handle images that may be too
1311 * large to be represented by a single texture.
1312 *
1313 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1314 * and that non-texture portion of the GrPaint has already been setup.
1315 */
1316void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1317 const SkRect& srcRect,
1318 const GrTextureParams& params,
1319 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001320 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001321 bool bicubic,
1322 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001323 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1324 bitmap.height() <= fContext->getMaxTextureSize());
1325
1326 GrTexture* texture;
1327 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1328 if (NULL == texture) {
1329 return;
1330 }
1331
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001332 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001333 SkRect paintRect;
1334 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1335 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1336 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1337 SkScalarMul(srcRect.fTop, hInv),
1338 SkScalarMul(srcRect.fRight, wInv),
1339 SkScalarMul(srcRect.fBottom, hInv));
1340
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 SkRect textureDomain = SkRect::MakeEmpty();
1342 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001343 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001344 // Use a constrained texture domain to avoid color bleeding
1345 SkScalar left, top, right, bottom;
1346 if (srcRect.width() > SK_Scalar1) {
1347 SkScalar border = SK_ScalarHalf / texture->width();
1348 left = paintRect.left() + border;
1349 right = paintRect.right() - border;
1350 } else {
1351 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1352 }
1353 if (srcRect.height() > SK_Scalar1) {
1354 SkScalar border = SK_ScalarHalf / texture->height();
1355 top = paintRect.top() + border;
1356 bottom = paintRect.bottom() - border;
1357 } else {
1358 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1359 }
1360 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001361 if (bicubic) {
1362 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1363 } else {
1364 effect.reset(GrTextureDomainEffect::Create(texture,
1365 SkMatrix::I(),
1366 textureDomain,
1367 GrTextureDomain::kClamp_Mode,
1368 params.filterMode()));
1369 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001370 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001371 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1372 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1373 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001374 } else {
1375 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1376 }
1377
1378 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1379 // the rest from the SkPaint.
1380 GrPaint grPaint;
1381 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001382 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
dandov9de5b512014-06-10 14:38:28 -07001383 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1384 SkColor2GrColor(paint.getColor());
1385 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001386
1387 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1388}
1389
1390static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001391 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001392 int w, int h, const SkImageFilter::Context& ctx,
1393 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001394 SkASSERT(filter);
1395 SkDeviceImageFilterProxy proxy(device);
1396
1397 if (filter->canFilterImageGPU()) {
1398 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1399 // filter. Also set the clip wide open and the matrix to identity.
1400 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001401 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001402 } else {
1403 return false;
1404 }
1405}
1406
1407void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1408 int left, int top, const SkPaint& paint) {
1409 // drawSprite is defined to be in device coords.
1410 CHECK_SHOULD_DRAW(draw, true);
1411
1412 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1413 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1414 return;
1415 }
1416
1417 int w = bitmap.width();
1418 int h = bitmap.height();
1419
1420 GrTexture* texture;
1421 // draw sprite uses the default texture params
1422 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1423
1424 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001425 // This bitmap will own the filtered result as a texture.
1426 SkBitmap filteredBitmap;
1427
1428 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001429 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001430 SkMatrix matrix(*draw.fMatrix);
1431 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001432 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001433 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1434 SkAutoUnref aur(cache);
1435 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001436 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001437 &offset)) {
1438 texture = (GrTexture*) filteredBitmap.getTexture();
1439 w = filteredBitmap.width();
1440 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001441 left += offset.x();
1442 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001443 } else {
1444 return;
1445 }
1446 }
1447
1448 GrPaint grPaint;
1449 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1450
dandov9de5b512014-06-10 14:38:28 -07001451 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1452 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001453
1454 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001455 SkRect::MakeXYWH(SkIntToScalar(left),
1456 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 SkIntToScalar(w),
1458 SkIntToScalar(h)),
1459 SkRect::MakeXYWH(0,
1460 0,
1461 SK_Scalar1 * w / texture->width(),
1462 SK_Scalar1 * h / texture->height()));
1463}
1464
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001465void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001466 const SkRect* src, const SkRect& dst,
1467 const SkPaint& paint,
1468 SkCanvas::DrawBitmapRectFlags flags) {
1469 SkMatrix matrix;
1470 SkRect bitmapBounds, tmpSrc;
1471
1472 bitmapBounds.set(0, 0,
1473 SkIntToScalar(bitmap.width()),
1474 SkIntToScalar(bitmap.height()));
1475
1476 // Compute matrix from the two rectangles
1477 if (NULL != src) {
1478 tmpSrc = *src;
1479 } else {
1480 tmpSrc = bitmapBounds;
1481 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001482
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1484
1485 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1486 if (NULL != src) {
1487 if (!bitmapBounds.contains(tmpSrc)) {
1488 if (!tmpSrc.intersect(bitmapBounds)) {
1489 return; // nothing to draw
1490 }
1491 }
1492 }
1493
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001494 SkRect tmpDst;
1495 matrix.mapRect(&tmpDst, tmpSrc);
1496
1497 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1498 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1499 // Translate so that tempDst's top left is at the origin.
1500 matrix = *origDraw.fMatrix;
1501 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1502 draw.writable()->fMatrix = &matrix;
1503 }
1504 SkSize dstSize;
1505 dstSize.fWidth = tmpDst.width();
1506 dstSize.fHeight = tmpDst.height();
1507
1508 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001509}
1510
1511void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1512 int x, int y, const SkPaint& paint) {
1513 // clear of the source device must occur before CHECK_SHOULD_DRAW
1514 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1515 if (dev->fNeedClear) {
1516 // TODO: could check here whether we really need to draw at all
1517 dev->clear(0x0);
1518 }
1519
1520 // drawDevice is defined to be in device coords.
1521 CHECK_SHOULD_DRAW(draw, true);
1522
1523 GrRenderTarget* devRT = dev->accessRenderTarget();
1524 GrTexture* devTex;
1525 if (NULL == (devTex = devRT->asTexture())) {
1526 return;
1527 }
1528
1529 const SkBitmap& bm = dev->accessBitmap(false);
1530 int w = bm.width();
1531 int h = bm.height();
1532
1533 SkImageFilter* filter = paint.getImageFilter();
1534 // This bitmap will own the filtered result as a texture.
1535 SkBitmap filteredBitmap;
1536
1537 if (NULL != filter) {
1538 SkIPoint offset = SkIPoint::Make(0, 0);
1539 SkMatrix matrix(*draw.fMatrix);
1540 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001541 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001542 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1543 SkAutoUnref aur(cache);
1544 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001545 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546 &offset)) {
1547 devTex = filteredBitmap.getTexture();
1548 w = filteredBitmap.width();
1549 h = filteredBitmap.height();
1550 x += offset.fX;
1551 y += offset.fY;
1552 } else {
1553 return;
1554 }
1555 }
1556
1557 GrPaint grPaint;
1558 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1559
dandov9de5b512014-06-10 14:38:28 -07001560 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1561 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562
1563 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1564 SkIntToScalar(y),
1565 SkIntToScalar(w),
1566 SkIntToScalar(h));
1567
1568 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1569 // scratch texture).
1570 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1571 SK_Scalar1 * h / devTex->height());
1572
1573 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1574}
1575
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001576bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577 return filter->canFilterImageGPU();
1578}
1579
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001580bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001581 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582 SkBitmap* result, SkIPoint* offset) {
1583 // want explicitly our impl, so guard against a subclass of us overriding it
1584 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1585 return false;
1586 }
1587
1588 SkAutoLockPixels alp(src, !src.getTexture());
1589 if (!src.getTexture() && !src.readyToDraw()) {
1590 return false;
1591 }
1592
1593 GrTexture* texture;
1594 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1595 // must be pushed upstack.
1596 SkAutoCachedTexture act(this, src, NULL, &texture);
1597
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001598 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1599 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600}
1601
1602///////////////////////////////////////////////////////////////////////////////
1603
1604// must be in SkCanvas::VertexMode order
1605static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1606 kTriangles_GrPrimitiveType,
1607 kTriangleStrip_GrPrimitiveType,
1608 kTriangleFan_GrPrimitiveType,
1609};
1610
1611void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1612 int vertexCount, const SkPoint vertices[],
1613 const SkPoint texs[], const SkColor colors[],
1614 SkXfermode* xmode,
1615 const uint16_t indices[], int indexCount,
1616 const SkPaint& paint) {
1617 CHECK_SHOULD_DRAW(draw, false);
1618
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001619 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1620 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1621 texs = NULL;
1622 SkPaint copy(paint);
1623 copy.setStyle(SkPaint::kStroke_Style);
1624 copy.setStrokeWidth(0);
1625
1626 VertState state(vertexCount, indices, indexCount);
1627 VertState::Proc vertProc = state.chooseProc(vmode);
1628
1629 SkPoint* pts = new SkPoint[vertexCount * 6];
1630 int i = 0;
1631 while (vertProc(&state)) {
1632 pts[i] = vertices[state.f0];
1633 pts[i + 1] = vertices[state.f1];
1634 pts[i + 2] = vertices[state.f1];
1635 pts[i + 3] = vertices[state.f2];
1636 pts[i + 4] = vertices[state.f2];
1637 pts[i + 5] = vertices[state.f0];
1638 i += 6;
1639 }
1640 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1641 return;
1642 }
1643
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001644 GrPaint grPaint;
1645 // we ignore the shader if texs is null.
1646 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001647 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1648 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001649 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001650 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001651 }
1652
mtklein2583b622014-06-04 08:20:41 -07001653#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001654 if (NULL != xmode && NULL != texs && NULL != colors) {
1655 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1656 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001657 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001658 }
1659 }
mtklein2583b622014-06-04 08:20:41 -07001660#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001661
1662 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1663 if (NULL != colors) {
1664 // need to convert byte order and from non-PM to PM
1665 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001666 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001668 color = colors[i];
1669 if (paint.getAlpha() != 255) {
1670 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1671 }
1672 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 }
1674 colors = convertedColors.get();
1675 }
1676 fContext->drawVertices(grPaint,
1677 gVertexMode2PrimitiveType[vmode],
1678 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001679 vertices,
1680 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 colors,
1682 indices,
1683 indexCount);
1684}
1685
1686///////////////////////////////////////////////////////////////////////////////
1687
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1689 size_t byteLength, SkScalar x, SkScalar y,
1690 const SkPaint& paint) {
1691 CHECK_SHOULD_DRAW(draw, false);
1692
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001693 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001694 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001695 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001696
1697 SkDEBUGCODE(this->validate();)
1698
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001699 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1700 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001701 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001702 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001703
1704 SkDEBUGCODE(this->validate();)
1705
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001706 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001707 } else {
1708 // this guy will just call our drawPath()
1709 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001710 }
1711}
1712
1713void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1714 size_t byteLength, const SkScalar pos[],
1715 SkScalar constY, int scalarsPerPos,
1716 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001717 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718 CHECK_SHOULD_DRAW(draw, false);
1719
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001720 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001721 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001722 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001723
1724 SkDEBUGCODE(this->validate();)
1725
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001726 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001727 constY, scalarsPerPos);
1728 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001729 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001730 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001731
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001732 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001733
1734 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001735 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001736 } else {
1737 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1738 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001739 }
1740}
1741
1742void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1743 size_t len, const SkPath& path,
1744 const SkMatrix* m, const SkPaint& paint) {
1745 CHECK_SHOULD_DRAW(draw, false);
1746
1747 SkASSERT(draw.fDevice == this);
1748 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1749}
1750
1751///////////////////////////////////////////////////////////////////////////////
1752
1753bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1754 if (!paint.isLCDRenderText()) {
1755 // we're cool with the paint as is
1756 return false;
1757 }
1758
1759 if (paint.getShader() ||
1760 paint.getXfermode() || // unless its srcover
1761 paint.getMaskFilter() ||
1762 paint.getRasterizer() ||
1763 paint.getColorFilter() ||
1764 paint.getPathEffect() ||
1765 paint.isFakeBoldText() ||
1766 paint.getStyle() != SkPaint::kFill_Style) {
1767 // turn off lcd
1768 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1769 flags->fHinting = paint.getHinting();
1770 return true;
1771 }
1772 // we're cool with the paint as is
1773 return false;
1774}
1775
1776void SkGpuDevice::flush() {
1777 DO_DEFERRED_CLEAR();
1778 fContext->resolveRenderTarget(fRenderTarget);
1779}
1780
1781///////////////////////////////////////////////////////////////////////////////
1782
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001783SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001784 GrTextureDesc desc;
1785 desc.fConfig = fRenderTarget->config();
1786 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001787 desc.fWidth = info.width();
1788 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001789 desc.fSampleCnt = fRenderTarget->numSamples();
1790
1791 SkAutoTUnref<GrTexture> texture;
1792 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001793 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001794
1795#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1796 // layers are never draw in repeat modes, so we can request an approx
1797 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001798 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001799 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1800 GrContext::kApprox_ScratchTexMatch :
1801 GrContext::kExact_ScratchTexMatch;
1802 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1803#else
1804 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1805#endif
1806 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001807 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001808 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001809 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1810 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001811 return NULL;
1812 }
1813}
1814
reed@google.com76f10a32014-02-05 15:32:21 +00001815SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1816 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1817}
1818
robertphillips9b14f262014-06-04 05:40:44 -07001819void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001820 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001821
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001822 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1823 if (NULL != existing) {
1824 return;
1825 }
1826
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001827 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001828
1829 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001830
1831 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001832}
1833
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001834static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1835 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001836 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001837 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1838}
1839
robertphillips9b14f262014-06-04 05:40:44 -07001840void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001841
1842}
1843
robertphillips9b14f262014-06-04 05:40:44 -07001844bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001845
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001846 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001847
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001848 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001849 if (NULL == data) {
1850 return false;
1851 }
1852
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001853 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001854
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001855 if (0 == gpuData->numSaveLayers()) {
1856 return false;
1857 }
1858
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001859 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1860 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1861 pullForward[i] = false;
1862 }
1863
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001864 SkRect clipBounds;
1865 if (!canvas->getClipBounds(&clipBounds)) {
1866 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001867 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001868 SkIRect query;
1869 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001870
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001871 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001872
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001873 // This code pre-renders the entire layer since it will be cached and potentially
1874 // reused with different clips (e.g., in different tiles). Because of this the
1875 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1876 // is used to limit which clips are pre-rendered.
1877 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001878
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001879 if (ops.valid()) {
1880 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001881 // the BBH to limit the pre-rendering to just the layers needed to cover
1882 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001883 for (int i = 0; i < ops.numOps(); ++i) {
1884 uint32_t offset = ops.offset(i);
1885
1886 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001887 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001888 // their parent in the list.
1889 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1890 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1891
1892 if (pullForward[j]) {
1893 continue; // already pulling forward
1894 }
1895
1896 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1897 continue; // the op isn't in this range
1898 }
1899
1900 // TODO: once this code is more stable unsuitable layers can
1901 // just be omitted during the optimization stage
1902 if (!info.fValid ||
1903 kSaveLayerMaxSize < info.fSize.fWidth ||
1904 kSaveLayerMaxSize < info.fSize.fHeight ||
1905 info.fIsNested) {
1906 continue; // this layer is unsuitable
1907 }
1908
1909 pullForward[j] = true;
1910 }
1911 }
1912 } else {
1913 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001914 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001915 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1916 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1917
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001918 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1919 info.fOffset.fY,
1920 info.fSize.fWidth,
1921 info.fSize.fHeight);
1922
1923 if (!SkIRect::Intersects(query, layerRect)) {
1924 continue;
1925 }
1926
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001927 // TODO: once this code is more stable unsuitable layers can
1928 // just be omitted during the optimization stage
1929 if (!info.fValid ||
1930 kSaveLayerMaxSize < info.fSize.fWidth ||
1931 kSaveLayerMaxSize < info.fSize.fHeight ||
1932 info.fIsNested) {
1933 continue;
1934 }
1935
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001936 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001937 }
1938 }
1939
1940 SkPicturePlayback::PlaybackReplacements replacements;
1941
1942 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1943 if (pullForward[i]) {
1944 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1945
1946 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1947
1948 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001949 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001950 replacements.push();
1951 layerInfo->fStart = info.fSaveLayerOpID;
1952 layerInfo->fStop = info.fRestoreOpID;
1953 layerInfo->fPos = info.fOffset;
1954
1955 GrTextureDesc desc;
1956 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1957 desc.fWidth = info.fSize.fWidth;
1958 desc.fHeight = info.fSize.fHeight;
1959 desc.fConfig = kSkia8888_GrPixelConfig;
1960 // TODO: need to deal with sample count
1961
1962 bool bNeedsRendering = true;
1963
1964 // This just uses scratch textures and doesn't cache the texture.
1965 // This can yield a lot of re-rendering
1966 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001967 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001968 GrContext::kApprox_ScratchTexMatch));
1969 if (NULL == layer->getTexture()) {
1970 continue;
1971 }
1972 } else {
1973 bNeedsRendering = false;
1974 }
1975
1976 layerInfo->fBM = SkNEW(SkBitmap);
1977 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1978
1979 SkASSERT(info.fPaint);
1980 layerInfo->fPaint = info.fPaint;
1981
1982 if (bNeedsRendering) {
1983 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1984 layer->getTexture()->asRenderTarget()));
1985
1986 SkCanvas* canvas = surface->getCanvas();
1987
1988 canvas->setMatrix(info.fCTM);
1989 canvas->clear(SK_ColorTRANSPARENT);
1990
1991 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1992 picture->fPlayback->draw(*canvas, NULL);
1993 picture->fPlayback->setDrawLimits(0, 0);
1994 canvas->flush();
1995 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001996 }
1997 }
1998 }
1999
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002000 // Playback using new layers
2001 picture->fPlayback->setReplacements(&replacements);
2002 picture->fPlayback->draw(*canvas, NULL);
2003 picture->fPlayback->setReplacements(NULL);
2004
2005 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2006 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2007
2008 if (NULL != layer->getTexture()) {
2009 fContext->unlockScratchTexture(layer->getTexture());
2010 layer->setTexture(NULL);
2011 }
2012 }
2013
2014 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002015}