blob: 861383bf6a03f48b8edeb3488d0f99a16dc05bf9 [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
170 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000171 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172
173 SkASSERT(NULL != renderTarget);
174 fRenderTarget = renderTarget;
175 fRenderTarget->ref();
176
177 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
178 // on the RT but not vice-versa.
179 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
180 // busting chrome (for a currently unknown reason).
181 GrSurface* surface = fRenderTarget->asTexture();
182 if (NULL == surface) {
183 surface = fRenderTarget;
184 }
reed@google.combf790232013-12-13 19:45:58 +0000185
reed6c225732014-06-09 19:52:07 -0700186 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
187 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188
reed@google.com672588b2014-01-08 15:42:01 +0000189 this->setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700190
191 bool useDFFonts = !!(flags & kDFFonts_Flag);
192 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
193 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000194}
195
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
197 int sampleCount) {
198 if (kUnknown_SkColorType == origInfo.colorType() ||
199 origInfo.width() < 0 || origInfo.height() < 0) {
200 return NULL;
201 }
202
203 SkImageInfo info = origInfo;
204 // TODO: perhas we can loosen this check now that colortype is more detailed
205 // e.g. can we support both RGBA and BGRA here?
206 if (kRGB_565_SkColorType == info.colorType()) {
207 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
208 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000209 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000210 if (kOpaque_SkAlphaType != info.alphaType()) {
211 info.fAlphaType = kPremul_SkAlphaType; // force this setting
212 }
213 }
214
215 GrTextureDesc desc;
216 desc.fFlags = kRenderTarget_GrTextureFlagBit;
217 desc.fWidth = info.width();
218 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000219 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000220 desc.fSampleCnt = sampleCount;
221
222 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
223 if (!texture.get()) {
224 return NULL;
225 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000226
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000227 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
228}
229
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230SkGpuDevice::~SkGpuDevice() {
231 if (fDrawProcs) {
232 delete fDrawProcs;
233 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000234
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000235 delete fMainTextContext;
236 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000237
238 // The GrContext takes a ref on the target. We don't want to cause the render
239 // target to be unnecessarily kept alive.
240 if (fContext->getRenderTarget() == fRenderTarget) {
241 fContext->setRenderTarget(NULL);
242 }
243
244 if (fContext->getClip() == &fClipData) {
245 fContext->setClip(NULL);
246 }
247
248 SkSafeUnref(fRenderTarget);
249 fContext->unref();
250}
251
252///////////////////////////////////////////////////////////////////////////////
253
254void SkGpuDevice::makeRenderTargetCurrent() {
255 DO_DEFERRED_CLEAR();
256 fContext->setRenderTarget(fRenderTarget);
257}
258
259///////////////////////////////////////////////////////////////////////////////
260
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000261bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
262 int x, int y) {
263 DO_DEFERRED_CLEAR();
264
265 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000266 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267 if (kUnknown_GrPixelConfig == config) {
268 return false;
269 }
270
271 uint32_t flags = 0;
272 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
273 flags = GrContext::kUnpremul_PixelOpsFlag;
274 }
275 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
276 config, dstPixels, dstRowBytes, flags);
277}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000278
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000279bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
280 int x, int y) {
281 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000282 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000283 if (kUnknown_GrPixelConfig == config) {
284 return false;
285 }
286 uint32_t flags = 0;
287 if (kUnpremul_SkAlphaType == info.alphaType()) {
288 flags = GrContext::kUnpremul_PixelOpsFlag;
289 }
290 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
291
292 // need to bump our genID for compatibility with clients that "know" we have a bitmap
293 this->onAccessBitmap().notifyPixelsChanged();
294
295 return true;
296}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000297
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000298const SkBitmap& SkGpuDevice::onAccessBitmap() {
299 DO_DEFERRED_CLEAR();
300 return INHERITED::onAccessBitmap();
301}
302
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
304 INHERITED::onAttachToCanvas(canvas);
305
306 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
307 fClipData.fClipStack = canvas->getClipStack();
308}
309
310void SkGpuDevice::onDetachFromCanvas() {
311 INHERITED::onDetachFromCanvas();
312 fClipData.fClipStack = NULL;
313}
314
315// call this every draw call, to ensure that the context reflects our state,
316// and not the state from some other canvas/device
317void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
318 SkASSERT(NULL != fClipData.fClipStack);
319
320 fContext->setRenderTarget(fRenderTarget);
321
322 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
323
324 if (forceIdentity) {
325 fContext->setIdentityMatrix();
326 } else {
327 fContext->setMatrix(*draw.fMatrix);
328 }
329 fClipData.fOrigin = this->getOrigin();
330
331 fContext->setClip(&fClipData);
332
333 DO_DEFERRED_CLEAR();
334}
335
336GrRenderTarget* SkGpuDevice::accessRenderTarget() {
337 DO_DEFERRED_CLEAR();
338 return fRenderTarget;
339}
340
341///////////////////////////////////////////////////////////////////////////////
342
343SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
346SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
347SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
348 shader_type_mismatch);
349SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
350 shader_type_mismatch);
351SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
352SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000354///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000355
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000356void SkGpuDevice::clear(SkColor color) {
357 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
358 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
359 fNeedClear = false;
360}
361
362void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
363 CHECK_SHOULD_DRAW(draw, false);
364
365 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000366 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000367
368 fContext->drawPaint(grPaint);
369}
370
371// must be in SkCanvas::PointMode order
372static const GrPrimitiveType gPointMode2PrimtiveType[] = {
373 kPoints_GrPrimitiveType,
374 kLines_GrPrimitiveType,
375 kLineStrip_GrPrimitiveType
376};
377
378void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
379 size_t count, const SkPoint pts[], const SkPaint& paint) {
380 CHECK_FOR_ANNOTATION(paint);
381 CHECK_SHOULD_DRAW(draw, false);
382
383 SkScalar width = paint.getStrokeWidth();
384 if (width < 0) {
385 return;
386 }
387
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000388 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700389 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
390 GrPaint grPaint;
391 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
392 SkPath path;
393 path.moveTo(pts[0]);
394 path.lineTo(pts[1]);
395 fContext->drawPath(grPaint, path, strokeInfo);
396 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000397 }
398
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000399 // we only handle hairlines and paints without path effects or mask filters,
400 // else we let the SkDraw call our drawPath()
401 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
402 draw.drawPoints(mode, count, pts, paint, true);
403 return;
404 }
405
406 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000407 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408
409 fContext->drawVertices(grPaint,
410 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000411 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000412 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000413 NULL,
414 NULL,
415 NULL,
416 0);
417}
418
419///////////////////////////////////////////////////////////////////////////////
420
421void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
422 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700423 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
424
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000425 CHECK_FOR_ANNOTATION(paint);
426 CHECK_SHOULD_DRAW(draw, false);
427
428 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
429 SkScalar width = paint.getStrokeWidth();
430
431 /*
432 We have special code for hairline strokes, miter-strokes, bevel-stroke
433 and fills. Anything else we just call our path code.
434 */
435 bool usePath = doStroke && width > 0 &&
436 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
437 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
438 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700439
440 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000441 usePath = true;
442 }
egdanield58a0ba2014-06-11 10:30:05 -0700443
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000444 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
445#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
446 if (doStroke) {
447#endif
448 usePath = true;
449#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
450 } else {
451 usePath = !fContext->getMatrix().preservesRightAngles();
452 }
453#endif
454 }
455 // until we can both stroke and fill rectangles
456 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
457 usePath = true;
458 }
459
egdanield58a0ba2014-06-11 10:30:05 -0700460 GrStrokeInfo strokeInfo(paint);
461
462 const SkPathEffect* pe = paint.getPathEffect();
463 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
464 usePath = true;
465 }
466
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000467 if (usePath) {
468 SkPath path;
469 path.addRect(rect);
470 this->drawPath(draw, path, paint, NULL, true);
471 return;
472 }
473
474 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000475 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400476
egdanield58a0ba2014-06-11 10:30:05 -0700477 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000478}
479
480///////////////////////////////////////////////////////////////////////////////
481
482void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
483 const SkPaint& paint) {
484 CHECK_FOR_ANNOTATION(paint);
485 CHECK_SHOULD_DRAW(draw, false);
486
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000487 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000488 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400489
egdanield58a0ba2014-06-11 10:30:05 -0700490 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000491 if (paint.getMaskFilter()) {
492 // try to hit the fast path for drawing filtered round rects
493
494 SkRRect devRRect;
495 if (rect.transform(fContext->getMatrix(), &devRRect)) {
496 if (devRRect.allCornersCircular()) {
497 SkRect maskRect;
498 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
499 draw.fClip->getBounds(),
500 fContext->getMatrix(),
501 &maskRect)) {
502 SkIRect finalIRect;
503 maskRect.roundOut(&finalIRect);
504 if (draw.fClip->quickReject(finalIRect)) {
505 // clipped out
506 return;
507 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000508 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700509 strokeInfo.getStrokeRec(),
510 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000511 return;
512 }
513 }
514
515 }
516 }
517
518 }
519
egdanield58a0ba2014-06-11 10:30:05 -0700520 bool usePath = false;
521
522 if (paint.getMaskFilter()) {
523 usePath = true;
524 } else {
525 const SkPathEffect* pe = paint.getPathEffect();
526 if (NULL != pe && !strokeInfo.isDashed()) {
527 usePath = true;
528 }
529 }
530
531
532 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000533 SkPath path;
534 path.addRRect(rect);
535 this->drawPath(draw, path, paint, NULL, true);
536 return;
537 }
Mike Klein744fb732014-06-23 15:13:26 -0400538
egdanield58a0ba2014-06-11 10:30:05 -0700539 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540}
541
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000542void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
543 const SkRRect& inner, const SkPaint& paint) {
544 SkStrokeRec stroke(paint);
545 if (stroke.isFillStyle()) {
546
547 CHECK_FOR_ANNOTATION(paint);
548 CHECK_SHOULD_DRAW(draw, false);
549
550 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000551 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000552
553 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
554 fContext->drawDRRect(grPaint, outer, inner);
555 return;
556 }
557 }
558
559 SkPath path;
560 path.addRRect(outer);
561 path.addRRect(inner);
562 path.setFillType(SkPath::kEvenOdd_FillType);
563
564 this->drawPath(draw, path, paint, NULL, true);
565}
566
567
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000568/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000569
570void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
571 const SkPaint& paint) {
572 CHECK_FOR_ANNOTATION(paint);
573 CHECK_SHOULD_DRAW(draw, false);
574
egdanield58a0ba2014-06-11 10:30:05 -0700575 GrStrokeInfo strokeInfo(paint);
576
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577 bool usePath = false;
578 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700579 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000580 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700581 } else {
582 const SkPathEffect* pe = paint.getPathEffect();
583 if (NULL != pe && !strokeInfo.isDashed()) {
584 usePath = true;
585 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000586 }
587
588 if (usePath) {
589 SkPath path;
590 path.addOval(oval);
591 this->drawPath(draw, path, paint, NULL, true);
592 return;
593 }
594
595 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000596 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000597
egdanield58a0ba2014-06-11 10:30:05 -0700598 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000599}
600
601#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602
603///////////////////////////////////////////////////////////////////////////////
604
605// helpers for applying mask filters
606namespace {
607
608// Draw a mask using the supplied paint. Since the coverage/geometry
609// is already burnt into the mask this boils down to a rect draw.
610// Return true if the mask was successfully drawn.
611bool draw_mask(GrContext* context, const SkRect& maskRect,
612 GrPaint* grp, GrTexture* mask) {
613 GrContext::AutoMatrix am;
614 if (!am.setIdentity(context, grp)) {
615 return false;
616 }
617
618 SkMatrix matrix;
619 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
620 matrix.postIDiv(mask->width(), mask->height());
621
622 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
623 context->drawRect(*grp, maskRect);
624 return true;
625}
626
627bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700628 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000629 GrPaint* grp, SkPaint::Style style) {
630 SkMask srcM, dstM;
631
632 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
633 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
634 return false;
635 }
636 SkAutoMaskFreeImage autoSrc(srcM.fImage);
637
638 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
639 return false;
640 }
641 // this will free-up dstM when we're done (allocated in filterMask())
642 SkAutoMaskFreeImage autoDst(dstM.fImage);
643
644 if (clip.quickReject(dstM.fBounds)) {
645 return false;
646 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000647
648 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
649 // the current clip (and identity matrix) and GrPaint settings
650 GrTextureDesc desc;
651 desc.fWidth = dstM.fBounds.width();
652 desc.fHeight = dstM.fBounds.height();
653 desc.fConfig = kAlpha_8_GrPixelConfig;
654
655 GrAutoScratchTexture ast(context, desc);
656 GrTexture* texture = ast.texture();
657
658 if (NULL == texture) {
659 return false;
660 }
661 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
662 dstM.fImage, dstM.fRowBytes);
663
664 SkRect maskRect = SkRect::Make(dstM.fBounds);
665
666 return draw_mask(context, maskRect, grp, texture);
667}
668
669// Create a mask of 'devPath' and place the result in 'mask'. Return true on
670// success; false otherwise.
671bool create_mask_GPU(GrContext* context,
672 const SkRect& maskRect,
673 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700674 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000675 bool doAA,
676 GrAutoScratchTexture* mask) {
677 GrTextureDesc desc;
678 desc.fFlags = kRenderTarget_GrTextureFlagBit;
679 desc.fWidth = SkScalarCeilToInt(maskRect.width());
680 desc.fHeight = SkScalarCeilToInt(maskRect.height());
681 // We actually only need A8, but it often isn't supported as a
682 // render target so default to RGBA_8888
683 desc.fConfig = kRGBA_8888_GrPixelConfig;
684 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
685 desc.fConfig = kAlpha_8_GrPixelConfig;
686 }
687
688 mask->set(context, desc);
689 if (NULL == mask->texture()) {
690 return false;
691 }
692
693 GrTexture* maskTexture = mask->texture();
694 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
695
696 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
697 GrContext::AutoClip ac(context, clipRect);
698
699 context->clear(NULL, 0x0, true);
700
701 GrPaint tempPaint;
702 if (doAA) {
703 tempPaint.setAntiAlias(true);
704 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
705 // blend coeff of zero requires dual source blending support in order
706 // to properly blend partially covered pixels. This means the AA
707 // code path may not be taken. So we use a dst blend coeff of ISA. We
708 // could special case AA draws to a dst surface with known alpha=0 to
709 // use a zero dst coeff when dual source blending isn't available.
710 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
711 }
712
713 GrContext::AutoMatrix am;
714
715 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
716 SkMatrix translate;
717 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
718 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700719 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000720 return true;
721}
722
723SkBitmap wrap_texture(GrTexture* texture) {
724 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700725 result.setInfo(texture->info());
726 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727 return result;
728}
729
730};
731
732void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
733 const SkPaint& paint, const SkMatrix* prePathMatrix,
734 bool pathIsMutable) {
735 CHECK_FOR_ANNOTATION(paint);
736 CHECK_SHOULD_DRAW(draw, false);
737
738 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000739 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000740
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000741 // If we have a prematrix, apply it to the path, optimizing for the case
742 // where the original path can in fact be modified in place (even though
743 // its parameter type is const).
744 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000745 SkTLazy<SkPath> tmpPath;
746 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000747
748 if (prePathMatrix) {
749 SkPath* result = pathPtr;
750
751 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000752 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000753 pathIsMutable = true;
754 }
755 // should I push prePathMatrix on our MV stack temporarily, instead
756 // of applying it here? See SkDraw.cpp
757 pathPtr->transform(*prePathMatrix, result);
758 pathPtr = result;
759 }
760 // at this point we're done with prePathMatrix
761 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
762
egdanield58a0ba2014-06-11 10:30:05 -0700763 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000764 SkPathEffect* pathEffect = paint.getPathEffect();
765 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700766 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
767 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000768 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000769 pathPtr = effectPath.get();
770 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700771 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000772 }
773
egdanield58a0ba2014-06-11 10:30:05 -0700774 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000775 if (paint.getMaskFilter()) {
776 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000777 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
778 if (stroke.applyToPath(strokedPath, *pathPtr)) {
779 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700781 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000782 }
783 }
784
785 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000786 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
788 // transform the path into device space
789 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000790
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 SkRect maskRect;
792 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
793 draw.fClip->getBounds(),
794 fContext->getMatrix(),
795 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000796 // The context's matrix may change while creating the mask, so save the CTM here to
797 // pass to filterMaskGPU.
798 const SkMatrix ctm = fContext->getMatrix();
799
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000800 SkIRect finalIRect;
801 maskRect.roundOut(&finalIRect);
802 if (draw.fClip->quickReject(finalIRect)) {
803 // clipped out
804 return;
805 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000806
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000807 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000808 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000809 // the mask filter was able to draw itself directly, so there's nothing
810 // left to do.
811 return;
812 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000813
814 GrAutoScratchTexture mask;
815
egdanield58a0ba2014-06-11 10:30:05 -0700816 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000817 grPaint.isAntiAlias(), &mask)) {
818 GrTexture* filtered;
819
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000820 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000821 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000822 // filterMaskGPU gives us ownership of a ref to the result
823 SkAutoTUnref<GrTexture> atu(filtered);
824
825 // If the scratch texture that we used as the filter src also holds the filter
826 // result then we must detach so that this texture isn't recycled for a later
827 // draw.
828 if (filtered == mask.texture()) {
829 mask.detach();
830 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
831 }
832
833 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
834 // This path is completely drawn
835 return;
836 }
837 }
838 }
839 }
840
841 // draw the mask on the CPU - this is a fallthrough path in case the
842 // GPU path fails
843 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
844 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700845 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
846 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000847 return;
848 }
849
egdanield58a0ba2014-06-11 10:30:05 -0700850 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000851}
852
853static const int kBmpSmallTileSize = 1 << 10;
854
855static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
856 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
857 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
858 return tilesX * tilesY;
859}
860
861static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
862 if (maxTileSize <= kBmpSmallTileSize) {
863 return maxTileSize;
864 }
865
866 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
867 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
868
869 maxTileTotalTileSize *= maxTileSize * maxTileSize;
870 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
871
872 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
873 return kBmpSmallTileSize;
874 } else {
875 return maxTileSize;
876 }
877}
878
879// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
880// pixels from the bitmap are necessary.
881static void determine_clipped_src_rect(const GrContext* context,
882 const SkBitmap& bitmap,
883 const SkRect* srcRectPtr,
884 SkIRect* clippedSrcIRect) {
885 const GrClipData* clip = context->getClip();
886 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
887 SkMatrix inv;
888 if (!context->getMatrix().invert(&inv)) {
889 clippedSrcIRect->setEmpty();
890 return;
891 }
892 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
893 inv.mapRect(&clippedSrcRect);
894 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000895 // we've setup src space 0,0 to map to the top left of the src rect.
896 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000897 if (!clippedSrcRect.intersect(*srcRectPtr)) {
898 clippedSrcIRect->setEmpty();
899 return;
900 }
901 }
902 clippedSrcRect.roundOut(clippedSrcIRect);
903 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
904 if (!clippedSrcIRect->intersect(bmpBounds)) {
905 clippedSrcIRect->setEmpty();
906 }
907}
908
909bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
910 const GrTextureParams& params,
911 const SkRect* srcRectPtr,
912 int maxTileSize,
913 int* tileSize,
914 SkIRect* clippedSrcRect) const {
915 // if bitmap is explictly texture backed then just use the texture
916 if (NULL != bitmap.getTexture()) {
917 return false;
918 }
919
920 // if it's larger than the max tile size, then we have no choice but tiling.
921 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
922 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
923 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
924 return true;
925 }
926
927 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
928 return false;
929 }
930
931 // if the entire texture is already in our cache then no reason to tile it
932 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
933 return false;
934 }
935
936 // At this point we know we could do the draw by uploading the entire bitmap
937 // as a texture. However, if the texture would be large compared to the
938 // cache size and we don't require most of it for this draw then tile to
939 // reduce the amount of upload and cache spill.
940
941 // assumption here is that sw bitmap size is a good proxy for its size as
942 // a texture
943 size_t bmpSize = bitmap.getSize();
944 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000945 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000946 if (bmpSize < cacheSize / 2) {
947 return false;
948 }
949
950 // Figure out how much of the src we will need based on the src rect and clipping.
951 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
952 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
953 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
954 kBmpSmallTileSize * kBmpSmallTileSize;
955
956 return usedTileBytes < 2 * bmpSize;
957}
958
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000959void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000960 const SkBitmap& bitmap,
961 const SkMatrix& m,
962 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000963 SkMatrix concat;
964 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
965 if (!m.isIdentity()) {
966 concat.setConcat(*draw->fMatrix, m);
967 draw.writable()->fMatrix = &concat;
968 }
969 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970}
971
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000972// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000973// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
974// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000975static inline void clamped_outset_with_offset(SkIRect* iRect,
976 int outset,
977 SkPoint* offset,
978 const SkIRect& clamp) {
979 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000981 int leftClampDelta = clamp.fLeft - iRect->fLeft;
982 if (leftClampDelta > 0) {
983 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000984 iRect->fLeft = clamp.fLeft;
985 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000986 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000987 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000988
989 int topClampDelta = clamp.fTop - iRect->fTop;
990 if (topClampDelta > 0) {
991 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000992 iRect->fTop = clamp.fTop;
993 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000994 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000995 }
996
997 if (iRect->fRight > clamp.fRight) {
998 iRect->fRight = clamp.fRight;
999 }
1000 if (iRect->fBottom > clamp.fBottom) {
1001 iRect->fBottom = clamp.fBottom;
1002 }
1003}
1004
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001005static bool has_aligned_samples(const SkRect& srcRect,
1006 const SkRect& transformedRect) {
1007 // detect pixel disalignment
1008 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1009 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1010 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1011 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1012 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1013 COLOR_BLEED_TOLERANCE &&
1014 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1015 COLOR_BLEED_TOLERANCE) {
1016 return true;
1017 }
1018 return false;
1019}
1020
1021static bool may_color_bleed(const SkRect& srcRect,
1022 const SkRect& transformedRect,
1023 const SkMatrix& m) {
1024 // Only gets called if has_aligned_samples returned false.
1025 // So we can assume that sampling is axis aligned but not texel aligned.
1026 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1027 SkRect innerSrcRect(srcRect), innerTransformedRect,
1028 outerTransformedRect(transformedRect);
1029 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1030 m.mapRect(&innerTransformedRect, innerSrcRect);
1031
1032 // The gap between outerTransformedRect and innerTransformedRect
1033 // represents the projection of the source border area, which is
1034 // problematic for color bleeding. We must check whether any
1035 // destination pixels sample the border area.
1036 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1037 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1038 SkIRect outer, inner;
1039 outerTransformedRect.round(&outer);
1040 innerTransformedRect.round(&inner);
1041 // If the inner and outer rects round to the same result, it means the
1042 // border does not overlap any pixel centers. Yay!
1043 return inner != outer;
1044}
1045
1046static bool needs_texture_domain(const SkBitmap& bitmap,
1047 const SkRect& srcRect,
1048 GrTextureParams &params,
1049 const SkMatrix& contextMatrix,
1050 bool bicubic) {
1051 bool needsTextureDomain = false;
1052
1053 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1054 // Need texture domain if drawing a sub rect
1055 needsTextureDomain = srcRect.width() < bitmap.width() ||
1056 srcRect.height() < bitmap.height();
1057 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1058 // sampling is axis-aligned
1059 SkRect transformedRect;
1060 contextMatrix.mapRect(&transformedRect, srcRect);
1061
1062 if (has_aligned_samples(srcRect, transformedRect)) {
1063 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1064 needsTextureDomain = false;
1065 } else {
1066 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1067 }
1068 }
1069 }
1070 return needsTextureDomain;
1071}
1072
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001073void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1074 const SkBitmap& bitmap,
1075 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001076 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001077 const SkPaint& paint,
1078 SkCanvas::DrawBitmapRectFlags flags) {
1079 CHECK_SHOULD_DRAW(draw, false);
1080
1081 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001082 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001083 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1084 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001085 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086 SkScalar w = SkIntToScalar(bitmap.width());
1087 SkScalar h = SkIntToScalar(bitmap.height());
1088 dstSize.fWidth = w;
1089 dstSize.fHeight = h;
1090 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001091 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001093 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001094 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001095 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001096 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1097 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1098 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1099 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001100 }
1101
1102 if (paint.getMaskFilter()){
1103 // Convert the bitmap to a shader so that the rect can be drawn
1104 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 SkBitmap tmp; // subset of bitmap, if necessary
1106 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001109 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1110 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1111 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001112 // In bleed mode we position and trim the bitmap based on the src rect which is
1113 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1114 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1115 // compensate.
1116 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1117 SkIRect iSrc;
1118 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001119
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001120 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1121 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001122
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001123 if (!bitmap.extractSubset(&tmp, iSrc)) {
1124 return; // extraction failed
1125 }
1126 bitmapPtr = &tmp;
1127 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001129 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001130 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001131 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001132 } else {
1133 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001134 }
1135
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001136 SkPaint paintWithShader(paint);
1137 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001138 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001139 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1140 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141
1142 return;
1143 }
1144
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001145 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1146 // the view matrix rather than a local matrix.
1147 SkMatrix m;
1148 m.setScale(dstSize.fWidth / srcRect.width(),
1149 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001150 fContext->concatMatrix(m);
1151
1152 GrTextureParams params;
1153 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1154 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001155
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001156 bool doBicubic = false;
1157
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001158 switch(paintFilterLevel) {
1159 case SkPaint::kNone_FilterLevel:
1160 textureFilterMode = GrTextureParams::kNone_FilterMode;
1161 break;
1162 case SkPaint::kLow_FilterLevel:
1163 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1164 break;
1165 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001166 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001167 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1168 } else {
1169 // Don't trigger MIP level generation unnecessarily.
1170 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1171 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001173 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001174 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001175 doBicubic =
1176 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001177 break;
1178 default:
1179 SkErrorInternals::SetError( kInvalidPaint_SkError,
1180 "Sorry, I don't understand the filtering "
1181 "mode you asked for. Falling back to "
1182 "MIPMaps.");
1183 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1184 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001185 }
1186
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001187 int tileFilterPad;
1188 if (doBicubic) {
1189 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1190 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1191 tileFilterPad = 0;
1192 } else {
1193 tileFilterPad = 1;
1194 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001195 params.setFilterMode(textureFilterMode);
1196
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001197 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 int tileSize;
1199
1200 SkIRect clippedSrcRect;
1201 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1202 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001203 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1204 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 } else {
1206 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001207 bool needsTextureDomain = needs_texture_domain(bitmap,
1208 srcRect,
1209 params,
1210 fContext->getMatrix(),
1211 doBicubic);
1212 this->internalDrawBitmap(bitmap,
1213 srcRect,
1214 params,
1215 paint,
1216 flags,
1217 doBicubic,
1218 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 }
1220}
1221
1222// Break 'bitmap' into several tiles to draw it since it has already
1223// been determined to be too large to fit in VRAM
1224void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1225 const SkRect& srcRect,
1226 const SkIRect& clippedSrcIRect,
1227 const GrTextureParams& params,
1228 const SkPaint& paint,
1229 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001230 int tileSize,
1231 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001232 // The following pixel lock is technically redundant, but it is desirable
1233 // to lock outside of the tile loop to prevent redecoding the whole image
1234 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1235 // is larger than the limit of the discardable memory pool.
1236 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001237 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1238
1239 int nx = bitmap.width() / tileSize;
1240 int ny = bitmap.height() / tileSize;
1241 for (int x = 0; x <= nx; x++) {
1242 for (int y = 0; y <= ny; y++) {
1243 SkRect tileR;
1244 tileR.set(SkIntToScalar(x * tileSize),
1245 SkIntToScalar(y * tileSize),
1246 SkIntToScalar((x + 1) * tileSize),
1247 SkIntToScalar((y + 1) * tileSize));
1248
1249 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1250 continue;
1251 }
1252
1253 if (!tileR.intersect(srcRect)) {
1254 continue;
1255 }
1256
1257 SkBitmap tmpB;
1258 SkIRect iTileR;
1259 tileR.roundOut(&iTileR);
1260 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1261 SkIntToScalar(iTileR.fTop));
1262
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001263 // Adjust the context matrix to draw at the right x,y in device space
1264 SkMatrix tmpM;
1265 GrContext::AutoMatrix am;
1266 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1267 am.setPreConcat(fContext, tmpM);
1268
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001269 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001270 SkIRect iClampRect;
1271
1272 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1273 // In bleed mode we want to always expand the tile on all edges
1274 // but stay within the bitmap bounds
1275 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1276 } else {
1277 // In texture-domain/clamp mode we only want to expand the
1278 // tile on edges interior to "srcRect" (i.e., we want to
1279 // not bleed across the original clamped edges)
1280 srcRect.roundOut(&iClampRect);
1281 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001282 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1283 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284 }
1285
1286 if (bitmap.extractSubset(&tmpB, iTileR)) {
1287 // now offset it to make it "local" to our tmp bitmap
1288 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001289 GrTextureParams paramsTemp = params;
1290 bool needsTextureDomain = needs_texture_domain(bitmap,
1291 srcRect,
1292 paramsTemp,
1293 fContext->getMatrix(),
1294 bicubic);
1295 this->internalDrawBitmap(tmpB,
1296 tileR,
1297 paramsTemp,
1298 paint,
1299 flags,
1300 bicubic,
1301 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001302 }
1303 }
1304 }
1305}
1306
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001307
1308/*
1309 * This is called by drawBitmap(), which has to handle images that may be too
1310 * large to be represented by a single texture.
1311 *
1312 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1313 * and that non-texture portion of the GrPaint has already been setup.
1314 */
1315void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1316 const SkRect& srcRect,
1317 const GrTextureParams& params,
1318 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001319 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001320 bool bicubic,
1321 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001322 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1323 bitmap.height() <= fContext->getMaxTextureSize());
1324
1325 GrTexture* texture;
1326 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1327 if (NULL == texture) {
1328 return;
1329 }
1330
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001331 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001332 SkRect paintRect;
1333 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1334 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1335 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1336 SkScalarMul(srcRect.fTop, hInv),
1337 SkScalarMul(srcRect.fRight, wInv),
1338 SkScalarMul(srcRect.fBottom, hInv));
1339
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001340 SkRect textureDomain = SkRect::MakeEmpty();
1341 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001342 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001343 // Use a constrained texture domain to avoid color bleeding
1344 SkScalar left, top, right, bottom;
1345 if (srcRect.width() > SK_Scalar1) {
1346 SkScalar border = SK_ScalarHalf / texture->width();
1347 left = paintRect.left() + border;
1348 right = paintRect.right() - border;
1349 } else {
1350 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1351 }
1352 if (srcRect.height() > SK_Scalar1) {
1353 SkScalar border = SK_ScalarHalf / texture->height();
1354 top = paintRect.top() + border;
1355 bottom = paintRect.bottom() - border;
1356 } else {
1357 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1358 }
1359 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001360 if (bicubic) {
1361 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1362 } else {
1363 effect.reset(GrTextureDomainEffect::Create(texture,
1364 SkMatrix::I(),
1365 textureDomain,
1366 GrTextureDomain::kClamp_Mode,
1367 params.filterMode()));
1368 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001369 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001370 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1371 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1372 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001373 } else {
1374 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1375 }
1376
1377 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1378 // the rest from the SkPaint.
1379 GrPaint grPaint;
1380 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001381 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
dandov9de5b512014-06-10 14:38:28 -07001382 GrColor grColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1383 SkColor2GrColor(paint.getColor());
1384 SkPaint2GrPaintNoShader(this->context(), paint, grColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385
1386 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1387}
1388
1389static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001390 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001391 int w, int h, const SkImageFilter::Context& ctx,
1392 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001393 SkASSERT(filter);
1394 SkDeviceImageFilterProxy proxy(device);
1395
1396 if (filter->canFilterImageGPU()) {
1397 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1398 // filter. Also set the clip wide open and the matrix to identity.
1399 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001400 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001401 } else {
1402 return false;
1403 }
1404}
1405
1406void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1407 int left, int top, const SkPaint& paint) {
1408 // drawSprite is defined to be in device coords.
1409 CHECK_SHOULD_DRAW(draw, true);
1410
1411 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1412 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1413 return;
1414 }
1415
1416 int w = bitmap.width();
1417 int h = bitmap.height();
1418
1419 GrTexture* texture;
1420 // draw sprite uses the default texture params
1421 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1422
1423 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001424 // This bitmap will own the filtered result as a texture.
1425 SkBitmap filteredBitmap;
1426
1427 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001428 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001429 SkMatrix matrix(*draw.fMatrix);
1430 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001431 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001432 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1433 SkAutoUnref aur(cache);
1434 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001435 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001436 &offset)) {
1437 texture = (GrTexture*) filteredBitmap.getTexture();
1438 w = filteredBitmap.width();
1439 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001440 left += offset.x();
1441 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001442 } else {
1443 return;
1444 }
1445 }
1446
1447 GrPaint grPaint;
1448 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1449
dandov9de5b512014-06-10 14:38:28 -07001450 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1451 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001452
1453 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001454 SkRect::MakeXYWH(SkIntToScalar(left),
1455 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001456 SkIntToScalar(w),
1457 SkIntToScalar(h)),
1458 SkRect::MakeXYWH(0,
1459 0,
1460 SK_Scalar1 * w / texture->width(),
1461 SK_Scalar1 * h / texture->height()));
1462}
1463
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001464void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001465 const SkRect* src, const SkRect& dst,
1466 const SkPaint& paint,
1467 SkCanvas::DrawBitmapRectFlags flags) {
1468 SkMatrix matrix;
1469 SkRect bitmapBounds, tmpSrc;
1470
1471 bitmapBounds.set(0, 0,
1472 SkIntToScalar(bitmap.width()),
1473 SkIntToScalar(bitmap.height()));
1474
1475 // Compute matrix from the two rectangles
1476 if (NULL != src) {
1477 tmpSrc = *src;
1478 } else {
1479 tmpSrc = bitmapBounds;
1480 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001481
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001482 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1483
1484 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1485 if (NULL != src) {
1486 if (!bitmapBounds.contains(tmpSrc)) {
1487 if (!tmpSrc.intersect(bitmapBounds)) {
1488 return; // nothing to draw
1489 }
1490 }
1491 }
1492
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001493 SkRect tmpDst;
1494 matrix.mapRect(&tmpDst, tmpSrc);
1495
1496 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1497 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1498 // Translate so that tempDst's top left is at the origin.
1499 matrix = *origDraw.fMatrix;
1500 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1501 draw.writable()->fMatrix = &matrix;
1502 }
1503 SkSize dstSize;
1504 dstSize.fWidth = tmpDst.width();
1505 dstSize.fHeight = tmpDst.height();
1506
1507 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001508}
1509
1510void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1511 int x, int y, const SkPaint& paint) {
1512 // clear of the source device must occur before CHECK_SHOULD_DRAW
1513 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1514 if (dev->fNeedClear) {
1515 // TODO: could check here whether we really need to draw at all
1516 dev->clear(0x0);
1517 }
1518
1519 // drawDevice is defined to be in device coords.
1520 CHECK_SHOULD_DRAW(draw, true);
1521
1522 GrRenderTarget* devRT = dev->accessRenderTarget();
1523 GrTexture* devTex;
1524 if (NULL == (devTex = devRT->asTexture())) {
1525 return;
1526 }
1527
1528 const SkBitmap& bm = dev->accessBitmap(false);
1529 int w = bm.width();
1530 int h = bm.height();
1531
1532 SkImageFilter* filter = paint.getImageFilter();
1533 // This bitmap will own the filtered result as a texture.
1534 SkBitmap filteredBitmap;
1535
1536 if (NULL != filter) {
1537 SkIPoint offset = SkIPoint::Make(0, 0);
1538 SkMatrix matrix(*draw.fMatrix);
1539 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001540 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001541 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1542 SkAutoUnref aur(cache);
1543 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001544 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001545 &offset)) {
1546 devTex = filteredBitmap.getTexture();
1547 w = filteredBitmap.width();
1548 h = filteredBitmap.height();
1549 x += offset.fX;
1550 y += offset.fY;
1551 } else {
1552 return;
1553 }
1554 }
1555
1556 GrPaint grPaint;
1557 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1558
dandov9de5b512014-06-10 14:38:28 -07001559 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1560 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001561
1562 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1563 SkIntToScalar(y),
1564 SkIntToScalar(w),
1565 SkIntToScalar(h));
1566
1567 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1568 // scratch texture).
1569 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1570 SK_Scalar1 * h / devTex->height());
1571
1572 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1573}
1574
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001575bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576 return filter->canFilterImageGPU();
1577}
1578
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001579bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001580 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001581 SkBitmap* result, SkIPoint* offset) {
1582 // want explicitly our impl, so guard against a subclass of us overriding it
1583 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1584 return false;
1585 }
1586
1587 SkAutoLockPixels alp(src, !src.getTexture());
1588 if (!src.getTexture() && !src.readyToDraw()) {
1589 return false;
1590 }
1591
1592 GrTexture* texture;
1593 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1594 // must be pushed upstack.
1595 SkAutoCachedTexture act(this, src, NULL, &texture);
1596
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001597 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1598 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001599}
1600
1601///////////////////////////////////////////////////////////////////////////////
1602
1603// must be in SkCanvas::VertexMode order
1604static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1605 kTriangles_GrPrimitiveType,
1606 kTriangleStrip_GrPrimitiveType,
1607 kTriangleFan_GrPrimitiveType,
1608};
1609
1610void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1611 int vertexCount, const SkPoint vertices[],
1612 const SkPoint texs[], const SkColor colors[],
1613 SkXfermode* xmode,
1614 const uint16_t indices[], int indexCount,
1615 const SkPaint& paint) {
1616 CHECK_SHOULD_DRAW(draw, false);
1617
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001618 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1619 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
1620 texs = NULL;
1621 SkPaint copy(paint);
1622 copy.setStyle(SkPaint::kStroke_Style);
1623 copy.setStrokeWidth(0);
1624
1625 VertState state(vertexCount, indices, indexCount);
1626 VertState::Proc vertProc = state.chooseProc(vmode);
1627
1628 SkPoint* pts = new SkPoint[vertexCount * 6];
1629 int i = 0;
1630 while (vertProc(&state)) {
1631 pts[i] = vertices[state.f0];
1632 pts[i + 1] = vertices[state.f1];
1633 pts[i + 2] = vertices[state.f1];
1634 pts[i + 3] = vertices[state.f2];
1635 pts[i + 4] = vertices[state.f2];
1636 pts[i + 5] = vertices[state.f0];
1637 i += 6;
1638 }
1639 draw.drawPoints(SkCanvas::kLines_PointMode, i, pts, copy, true);
1640 return;
1641 }
1642
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001643 GrPaint grPaint;
1644 // we ignore the shader if texs is null.
1645 if (NULL == texs) {
dandov9de5b512014-06-10 14:38:28 -07001646 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1647 NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001648 } else {
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001649 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001650 }
1651
mtklein2583b622014-06-04 08:20:41 -07001652#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001653 if (NULL != xmode && NULL != texs && NULL != colors) {
1654 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1655 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001656 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 }
1658 }
mtklein2583b622014-06-04 08:20:41 -07001659#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001660
1661 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1662 if (NULL != colors) {
1663 // need to convert byte order and from non-PM to PM
1664 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001665 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001667 color = colors[i];
1668 if (paint.getAlpha() != 255) {
1669 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1670 }
1671 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001672 }
1673 colors = convertedColors.get();
1674 }
1675 fContext->drawVertices(grPaint,
1676 gVertexMode2PrimitiveType[vmode],
1677 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001678 vertices,
1679 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001680 colors,
1681 indices,
1682 indexCount);
1683}
1684
1685///////////////////////////////////////////////////////////////////////////////
1686
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1688 size_t byteLength, SkScalar x, SkScalar y,
1689 const SkPaint& paint) {
1690 CHECK_SHOULD_DRAW(draw, false);
1691
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001692 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001693 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001694 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001695
1696 SkDEBUGCODE(this->validate();)
1697
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001698 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1699 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001700 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001701 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001702
1703 SkDEBUGCODE(this->validate();)
1704
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001705 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001706 } else {
1707 // this guy will just call our drawPath()
1708 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001709 }
1710}
1711
1712void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1713 size_t byteLength, const SkScalar pos[],
1714 SkScalar constY, int scalarsPerPos,
1715 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001716 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717 CHECK_SHOULD_DRAW(draw, false);
1718
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001719 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001720 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001721 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001722
1723 SkDEBUGCODE(this->validate();)
1724
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001725 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001726 constY, scalarsPerPos);
1727 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001728 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001729 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001730
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001731 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001732
1733 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001734 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001735 } else {
1736 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1737 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001738 }
1739}
1740
1741void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1742 size_t len, const SkPath& path,
1743 const SkMatrix* m, const SkPaint& paint) {
1744 CHECK_SHOULD_DRAW(draw, false);
1745
1746 SkASSERT(draw.fDevice == this);
1747 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1748}
1749
1750///////////////////////////////////////////////////////////////////////////////
1751
1752bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1753 if (!paint.isLCDRenderText()) {
1754 // we're cool with the paint as is
1755 return false;
1756 }
1757
1758 if (paint.getShader() ||
1759 paint.getXfermode() || // unless its srcover
1760 paint.getMaskFilter() ||
1761 paint.getRasterizer() ||
1762 paint.getColorFilter() ||
1763 paint.getPathEffect() ||
1764 paint.isFakeBoldText() ||
1765 paint.getStyle() != SkPaint::kFill_Style) {
1766 // turn off lcd
1767 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1768 flags->fHinting = paint.getHinting();
1769 return true;
1770 }
1771 // we're cool with the paint as is
1772 return false;
1773}
1774
1775void SkGpuDevice::flush() {
1776 DO_DEFERRED_CLEAR();
1777 fContext->resolveRenderTarget(fRenderTarget);
1778}
1779
1780///////////////////////////////////////////////////////////////////////////////
1781
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001782SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001783 GrTextureDesc desc;
1784 desc.fConfig = fRenderTarget->config();
1785 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001786 desc.fWidth = info.width();
1787 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 desc.fSampleCnt = fRenderTarget->numSamples();
1789
1790 SkAutoTUnref<GrTexture> texture;
1791 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001792 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001793
1794#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1795 // layers are never draw in repeat modes, so we can request an approx
1796 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001797 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001798 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1799 GrContext::kApprox_ScratchTexMatch :
1800 GrContext::kExact_ScratchTexMatch;
1801 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1802#else
1803 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1804#endif
1805 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001806 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001807 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001808 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1809 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001810 return NULL;
1811 }
1812}
1813
reed@google.com76f10a32014-02-05 15:32:21 +00001814SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1815 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1816}
1817
robertphillips9b14f262014-06-04 05:40:44 -07001818void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001819 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001820
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001821 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1822 if (NULL != existing) {
1823 return;
1824 }
1825
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001826 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001827
1828 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001829
1830 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001831}
1832
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001833static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1834 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001835 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001836 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1837}
1838
robertphillips9b14f262014-06-04 05:40:44 -07001839void SkGpuDevice::EXPERIMENTAL_purge(const SkPicture* picture) {
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001840
1841}
1842
robertphillips9b14f262014-06-04 05:40:44 -07001843bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001844
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001845 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001846
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001847 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001848 if (NULL == data) {
1849 return false;
1850 }
1851
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001852 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001853
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001854 if (0 == gpuData->numSaveLayers()) {
1855 return false;
1856 }
1857
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001858 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1859 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1860 pullForward[i] = false;
1861 }
1862
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001863 SkRect clipBounds;
1864 if (!canvas->getClipBounds(&clipBounds)) {
1865 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001866 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001867 SkIRect query;
1868 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001869
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001870 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001871
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001872 // This code pre-renders the entire layer since it will be cached and potentially
1873 // reused with different clips (e.g., in different tiles). Because of this the
1874 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1875 // is used to limit which clips are pre-rendered.
1876 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001877
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001878 if (ops.valid()) {
1879 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001880 // the BBH to limit the pre-rendering to just the layers needed to cover
1881 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001882 for (int i = 0; i < ops.numOps(); ++i) {
1883 uint32_t offset = ops.offset(i);
1884
1885 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001886 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001887 // their parent in the list.
1888 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1889 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1890
1891 if (pullForward[j]) {
1892 continue; // already pulling forward
1893 }
1894
1895 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1896 continue; // the op isn't in this range
1897 }
1898
1899 // TODO: once this code is more stable unsuitable layers can
1900 // just be omitted during the optimization stage
1901 if (!info.fValid ||
1902 kSaveLayerMaxSize < info.fSize.fWidth ||
1903 kSaveLayerMaxSize < info.fSize.fHeight ||
1904 info.fIsNested) {
1905 continue; // this layer is unsuitable
1906 }
1907
1908 pullForward[j] = true;
1909 }
1910 }
1911 } else {
1912 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001913 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001914 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1915 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1916
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001917 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1918 info.fOffset.fY,
1919 info.fSize.fWidth,
1920 info.fSize.fHeight);
1921
1922 if (!SkIRect::Intersects(query, layerRect)) {
1923 continue;
1924 }
1925
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001926 // TODO: once this code is more stable unsuitable layers can
1927 // just be omitted during the optimization stage
1928 if (!info.fValid ||
1929 kSaveLayerMaxSize < info.fSize.fWidth ||
1930 kSaveLayerMaxSize < info.fSize.fHeight ||
1931 info.fIsNested) {
1932 continue;
1933 }
1934
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001935 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001936 }
1937 }
1938
1939 SkPicturePlayback::PlaybackReplacements replacements;
1940
1941 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1942 if (pullForward[i]) {
1943 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1944
1945 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1946
Mike Klein744fb732014-06-23 15:13:26 -04001947 if (NULL != picture->fPlayback.get()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001948 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001949 replacements.push();
1950 layerInfo->fStart = info.fSaveLayerOpID;
1951 layerInfo->fStop = info.fRestoreOpID;
1952 layerInfo->fPos = info.fOffset;
1953
1954 GrTextureDesc desc;
1955 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1956 desc.fWidth = info.fSize.fWidth;
1957 desc.fHeight = info.fSize.fHeight;
1958 desc.fConfig = kSkia8888_GrPixelConfig;
1959 // TODO: need to deal with sample count
1960
1961 bool bNeedsRendering = true;
1962
1963 // This just uses scratch textures and doesn't cache the texture.
1964 // This can yield a lot of re-rendering
1965 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001966 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001967 GrContext::kApprox_ScratchTexMatch));
1968 if (NULL == layer->getTexture()) {
1969 continue;
1970 }
1971 } else {
1972 bNeedsRendering = false;
1973 }
1974
1975 layerInfo->fBM = SkNEW(SkBitmap);
1976 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1977
1978 SkASSERT(info.fPaint);
1979 layerInfo->fPaint = info.fPaint;
1980
1981 if (bNeedsRendering) {
1982 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1983 layer->getTexture()->asRenderTarget()));
1984
1985 SkCanvas* canvas = surface->getCanvas();
1986
1987 canvas->setMatrix(info.fCTM);
1988 canvas->clear(SK_ColorTRANSPARENT);
1989
1990 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1991 picture->fPlayback->draw(*canvas, NULL);
1992 picture->fPlayback->setDrawLimits(0, 0);
1993 canvas->flush();
1994 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001995 }
1996 }
1997 }
1998
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001999 // Playback using new layers
2000 picture->fPlayback->setReplacements(&replacements);
2001 picture->fPlayback->draw(*canvas, NULL);
2002 picture->fPlayback->setReplacements(NULL);
2003
2004 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
2005 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
2006
2007 if (NULL != layer->getTexture()) {
2008 fContext->unlockScratchTexture(layer->getTexture());
2009 layer->setTexture(NULL);
2010 }
2011 }
2012
2013 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002014}