blob: 74549db66f50ecbfbab1c0af41cf65db1d59aadd [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
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000010#include "SkRecordTraits.h"
11
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000012namespace {
13
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000014// All clip commands, Restore, and SaveLayer may change the clip.
15template <typename T> struct ChangesClip { static const bool value = SkRecords::IsClip<T>::value; };
16template <> struct ChangesClip<SkRecords::Restore> { static const bool value = true; };
17template <> struct ChangesClip<SkRecords::SaveLayer> { static const bool value = true; };
18
19
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020// This is an SkRecord visitor that will draw that SkRecord to an SkCanvas.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000021class Draw : SkNoncopyable {
22public:
23 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(false) {}
24
25 unsigned index() const { return fIndex; }
26 void next() { ++fIndex; }
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000027
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000028 template <typename T> void operator()(const T& r) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000029 if (!this->skip(r)) {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000030 this->draw(r);
31 this->updateClip<T>();
32 }
33 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000034
35private:
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000036 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
37 template <typename T> void draw(const T&);
38
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000039 // skip() returns true if we can skip this command, false if not.
40 // Update fIndex directly to skip more than just this one command.
41
42 // If we're drawing into an empty clip, we can skip it. Otherwise, run the command.
43 template <typename T>
44 SK_WHEN(SkRecords::IsDraw<T>, bool) skip(const T&) { return fClipEmpty; }
45
46 template <typename T>
47 SK_WHEN(!SkRecords::IsDraw<T>, bool) skip(const T&) { return false; }
48
49 // Special versions for commands added by optimizations.
50 bool skip(const SkRecords::PairedPushCull& r) {
51 if (fCanvas->quickReject(r.base->rect)) {
52 fIndex += r.skip;
53 return true;
54 }
55 return this->skip(*r.base);
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000056 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000057
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000058 bool skip(const SkRecords::BoundedDrawPosTextH& r) {
59 return this->skip(*r.base) || fCanvas->quickRejectY(r.minY, r.maxY);
60 }
61
62 // If we might have changed the clip, update it, else do nothing.
63 template <typename T>
64 SK_WHEN(ChangesClip<T>, void) updateClip() { fClipEmpty = fCanvas->isClipEmpty(); }
65 template <typename T>
66 SK_WHEN(!ChangesClip<T>, void) updateClip() {}
67
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000068 SkCanvas* fCanvas;
69 unsigned fIndex;
70 bool fClipEmpty;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000071};
72
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000073// NoOps draw nothing.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000074template <> void Draw::draw(const SkRecords::NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000075
76#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
77DRAW(Restore, restore());
78DRAW(Save, save(r.flags));
79DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
80DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000081DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000082DRAW(Clear, clear(r.color));
83DRAW(Concat, concat(r.matrix));
84DRAW(SetMatrix, setMatrix(r.matrix));
85
86DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
87DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
88DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
89DRAW(ClipRegion, clipRegion(r.region, r.op));
90
91DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
92DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
93DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
94DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
95DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
96DRAW(DrawOval, drawOval(r.oval, r.paint));
97DRAW(DrawPaint, drawPaint(r.paint));
98DRAW(DrawPath, drawPath(r.path, r.paint));
99DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
100DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
101DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
102DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
103DRAW(DrawRect, drawRect(r.rect, r.paint));
104DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
105DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
106DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
107DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
108 r.xmode.get(), r.indices, r.indexCount, r.paint));
109#undef DRAW
110
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +0000111template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.base); }
112template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->draw(*r.base); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000113
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000114} // namespace
115
116void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000117 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
118 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000119 }
120}