blob: 666cfc9000e5ce37abdfd1fa99e76c5274523e00 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
2 * Copyright 2014 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
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +00008#include "SkRecordDraw.h"
9
10namespace {
11
12// This is an SkRecord visitor that will draw that SkRecord to an SkCanvas.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000013class Draw : SkNoncopyable {
14public:
commit-bot@chromium.org0974a612014-04-28 19:49:00 +000015 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0) {}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000016
17 unsigned index() const { return fIndex; }
18 void next() { ++fIndex; }
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000019
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000020 template <typename T> void operator()(const T& r) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000021 if (!this->skip(r)) {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000022 this->draw(r);
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000023 }
24 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000025
26private:
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000027 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
28 template <typename T> void draw(const T&);
29
commit-bot@chromium.org705b1ff2014-04-29 15:34:03 +000030 // skip() should return true if we can skip this command, false if not.
31 // It may update fIndex directly to skip more than just this one command.
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000032
commit-bot@chromium.org705b1ff2014-04-29 15:34:03 +000033 // Mostly we just blindly call fCanvas and let it handle quick rejects itself.
34 template <typename T> bool skip(const T&) { return false; }
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000035
commit-bot@chromium.org705b1ff2014-04-29 15:34:03 +000036 // We add our own quick rejects for commands added by optimizations.
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000037 bool skip(const SkRecords::PairedPushCull& r) {
38 if (fCanvas->quickReject(r.base->rect)) {
39 fIndex += r.skip;
40 return true;
41 }
commit-bot@chromium.org705b1ff2014-04-29 15:34:03 +000042 return false;
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000043 }
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000044 bool skip(const SkRecords::BoundedDrawPosTextH& r) {
commit-bot@chromium.org705b1ff2014-04-29 15:34:03 +000045 return fCanvas->quickRejectY(r.minY, r.maxY);
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000046 }
47
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000048 SkCanvas* fCanvas;
49 unsigned fIndex;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000050};
51
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000052// NoOps draw nothing.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000053template <> void Draw::draw(const SkRecords::NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000054
55#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
56DRAW(Restore, restore());
57DRAW(Save, save(r.flags));
58DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
59DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000060DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000061DRAW(Clear, clear(r.color));
62DRAW(Concat, concat(r.matrix));
63DRAW(SetMatrix, setMatrix(r.matrix));
64
65DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
66DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
67DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
68DRAW(ClipRegion, clipRegion(r.region, r.op));
69
70DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
71DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
72DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
73DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
74DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
75DRAW(DrawOval, drawOval(r.oval, r.paint));
76DRAW(DrawPaint, drawPaint(r.paint));
77DRAW(DrawPath, drawPath(r.path, r.paint));
78DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
79DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
80DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
81DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
82DRAW(DrawRect, drawRect(r.rect, r.paint));
83DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
84DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
85DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
86DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
87 r.xmode.get(), r.indices, r.indexCount, r.paint));
88#undef DRAW
89
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000090template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.base); }
91template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->draw(*r.base); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000092
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000093} // namespace
94
95void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000096 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000097 record.visit<void>(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000098 }
99}