1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "SkiaDisplayList.h"
20
21namespace android {
22namespace uirenderer {
23namespace skiapipeline {
24
25/**
26 * DumpOpsCanvas prints drawing ops from a SkiaDisplayList into a std::ostream. Children render
27 * nodes are walked recursively and their drawing ops are printed as well.
28 */
29class DumpOpsCanvas : public SkCanvas {
30public:
31    DumpOpsCanvas(std::ostream& output, int level, SkiaDisplayList& displayList)
32            : mOutput(output)
33            , mLevel(level)
34            , mDisplayList(displayList)
35            , mIdent((level + 1) * 2, ' ') {
36    }
37
38protected:
39    void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override {
40        mOutput << mIdent << "clipRect" << std::endl;
41    }
42
43    void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override {
44        mOutput << mIdent << "clipRRect" << std::endl;
45    }
46
47    void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override {
48        mOutput << mIdent << "clipPath" << std::endl;
49    }
50
51    void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override {
52        mOutput << mIdent << "clipRegion" << std::endl;
53    }
54
55    void onDrawPaint(const SkPaint&) override {
56        mOutput << mIdent << "drawPaint" << std::endl;
57    }
58
59    void onDrawPath(const SkPath&, const SkPaint&) override {
60        mOutput << mIdent << "drawPath" << std::endl;
61    }
62
63    void onDrawRect(const SkRect&, const SkPaint&) override {
64        mOutput << mIdent << "drawRect" << std::endl;
65    }
66
67    void onDrawRegion(const SkRegion&, const SkPaint&) override {
68        mOutput << mIdent << "drawRegion" << std::endl;
69    }
70
71    void onDrawOval(const SkRect&, const SkPaint&) override {
72        mOutput << mIdent << "drawOval" << std::endl;
73    }
74
75    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override {
76        mOutput << mIdent << "drawArc" << std::endl;
77    }
78
79    void onDrawRRect(const SkRRect&, const SkPaint&) override {
80        mOutput << mIdent << "drawRRect" << std::endl;
81    }
82
83    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override {
84        mOutput << mIdent << "drawDRRect" << std::endl;
85    }
86
87    void onDrawText(const void*, size_t, SkScalar, SkScalar, const SkPaint&) override {
88        mOutput << mIdent << "drawText" << std::endl;
89    }
90
91    void onDrawPosText(const void*, size_t, const SkPoint[], const SkPaint&) override {
92        mOutput << mIdent << "drawPosText" << std::endl;
93    }
94
95    void onDrawPosTextH(const void*, size_t, const SkScalar[], SkScalar,
96            const SkPaint&) override {
97        mOutput << mIdent << "drawPosTextH" << std::endl;
98    }
99
100    void onDrawTextOnPath(const void*, size_t, const SkPath&, const SkMatrix*,
101            const SkPaint&) override {
102        mOutput << mIdent << "drawTextOnPath" << std::endl;
103    }
104
105    void onDrawTextRSXform(const void*, size_t, const SkRSXform[], const SkRect*,
106            const SkPaint&) override {
107        mOutput << mIdent << "drawTextRSXform" << std::endl;
108    }
109
110    void onDrawTextBlob(const SkTextBlob*, SkScalar,SkScalar, const SkPaint&) override {
111        mOutput << mIdent << "drawTextBlob" << std::endl;
112    }
113
114    void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) override {
115        mOutput << mIdent << "drawImage" << std::endl;
116    }
117
118    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
119            const SkPaint*) override {
120        mOutput << mIdent << "drawImageNine" << std::endl;
121    }
122
123    void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*,
124            SrcRectConstraint) override {
125        mOutput << mIdent << "drawImageRect" << std::endl;
126    }
127
128    void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
129            const SkPaint*) override {
130        mOutput << mIdent << "drawImageLattice" << std::endl;
131    }
132
133    void onDrawPoints(SkCanvas::PointMode, size_t, const SkPoint[], const SkPaint&) override {
134        mOutput << mIdent << "drawPoints" << std::endl;
135    }
136
137    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override {
138        mOutput << mIdent << "drawPicture" << std::endl;
139    }
140
141    void onDrawDrawable(SkDrawable* drawable, const SkMatrix*) override {
142        mOutput << mIdent;
143        auto renderNodeDrawable = getRenderNodeDrawable(drawable);
144        if (nullptr != renderNodeDrawable) {
145            mOutput << std::string(mLevel * 2, ' ') << "drawRenderNode";
146            renderNodeDrawable->getRenderNode()->output(mOutput, mLevel + 1);
147            return;
148        }
149        auto glFunctorDrawable = getGLFunctorDrawable(drawable);
150        if (nullptr != glFunctorDrawable) {
151            mOutput << std::string(mLevel * 2, ' ') << "drawGLFunctorDrawable" << std::endl;
152            return;
153        }
154
155        mOutput << std::string(mLevel * 2, ' ') << "drawDrawable" << std::endl;
156    }
157
158private:
159    RenderNodeDrawable* getRenderNodeDrawable(SkDrawable* drawable) {
160         for (auto& child : mDisplayList.mChildNodes) {
161            if (drawable == &child) {
162                return &child;
163            }
164         }
165         return nullptr;
166    }
167
168    GLFunctorDrawable* getGLFunctorDrawable(SkDrawable* drawable) {
169         for (auto& child : mDisplayList.mChildFunctors) {
170            if (drawable == &child) {
171                return &child;
172            }
173         }
174         return nullptr;
175    }
176
177    std::ostream& mOutput;
178    int mLevel;
179    SkiaDisplayList& mDisplayList;
180    std::string mIdent;
181};
182
183}; // namespace skiapipeline
184}; // namespace uirenderer
185}; // namespace android
186