blob: bc1bbf5fd44f19f72d29bab41187c0c93ec1b8da [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
14// This is an SkRecord visitor that will draw that SkRecord to an SkCanvas.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015class Draw : SkNoncopyable {
16public:
commit-bot@chromium.org0974a612014-04-28 19:49:00 +000017 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0) {}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000018
19 unsigned index() const { return fIndex; }
20 void next() { ++fIndex; }
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000021
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000022 template <typename T> void operator()(const T& r) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000023 if (!this->skip(r)) {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000024 this->draw(r);
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000025 }
26 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000027
28private:
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000029 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
30 template <typename T> void draw(const T&);
31
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000032 // skip() returns true if we can skip this command, false if not.
33 // Update fIndex directly to skip more than just this one command.
34
35 // If we're drawing into an empty clip, we can skip it. Otherwise, run the command.
36 template <typename T>
commit-bot@chromium.org0974a612014-04-28 19:49:00 +000037 SK_WHEN(SkRecords::IsDraw<T>, bool) skip(const T&) { return fCanvas->isClipEmpty(); }
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000038
39 template <typename T>
40 SK_WHEN(!SkRecords::IsDraw<T>, bool) skip(const T&) { return false; }
41
42 // Special versions for commands added by optimizations.
43 bool skip(const SkRecords::PairedPushCull& r) {
44 if (fCanvas->quickReject(r.base->rect)) {
45 fIndex += r.skip;
46 return true;
47 }
48 return this->skip(*r.base);
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000049 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000050
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000051 bool skip(const SkRecords::BoundedDrawPosTextH& r) {
52 return this->skip(*r.base) || fCanvas->quickRejectY(r.minY, r.maxY);
53 }
54
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000055 SkCanvas* fCanvas;
56 unsigned fIndex;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000057};
58
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000059// NoOps draw nothing.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000060template <> void Draw::draw(const SkRecords::NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000061
62#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
63DRAW(Restore, restore());
64DRAW(Save, save(r.flags));
65DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
66DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000067DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000068DRAW(Clear, clear(r.color));
69DRAW(Concat, concat(r.matrix));
70DRAW(SetMatrix, setMatrix(r.matrix));
71
72DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
73DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
74DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
75DRAW(ClipRegion, clipRegion(r.region, r.op));
76
77DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
78DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
79DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
80DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
81DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
82DRAW(DrawOval, drawOval(r.oval, r.paint));
83DRAW(DrawPaint, drawPaint(r.paint));
84DRAW(DrawPath, drawPath(r.path, r.paint));
85DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
86DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
87DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
88DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
89DRAW(DrawRect, drawRect(r.rect, r.paint));
90DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
91DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
92DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
93DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
94 r.xmode.get(), r.indices, r.indexCount, r.paint));
95#undef DRAW
96
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000097template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.base); }
98template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->draw(*r.base); }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000099
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000100} // namespace
101
102void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000103 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
104 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000105 }
106}