1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RECORDING_CANVAS_H
17 #define RECORDING_CANVAS_H
18 
19 #include <optional>
20 #include <stack>
21 
22 #include "draw/canvas.h"
23 #include "image/gpu_context.h"
24 #include "recording/draw_cmd_list.h"
25 #include "recording/recording_handle.h"
26 #ifdef ROSEN_OHOS
27 #include "surface_buffer.h"
28 #endif
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33 /**
34  * @brief RecordingCanvas is an empty canvas, which does not act on any surface,
35  * and is used to record the sequence of draw calls for the canvas.
36  * Draw calls are kept in linear memory in DrawCmdList, Subsequent playback can be performed through DrawCmdList.
37  */
38 class DRAWING_API RecordingCanvas : public NoDrawCanvas {
39 public:
40     RecordingCanvas(int32_t width, int32_t height, bool addDrawOpImmediate = true);
41     ~RecordingCanvas() override = default;
42 
43     std::shared_ptr<DrawCmdList> GetDrawCmdList() const;
44 
GetDrawingType()45     DrawingType GetDrawingType() const override
46     {
47         return DrawingType::RECORDING;
48     }
49 
SetGrRecordingContext(std::shared_ptr<GPUContext> gpuContext)50     void SetGrRecordingContext(std::shared_ptr<GPUContext> gpuContext)
51     {
52         gpuContext_ = gpuContext;
53     }
54 
55 #ifdef RS_ENABLE_GPU
GetGPUContext()56     std::shared_ptr<GPUContext> GetGPUContext() override
57     {
58         return gpuContext_;
59     }
60 #endif
61 
62     void Clear() const;
63     void Reset(int32_t width, int32_t height, bool addDrawOpImmediate = true);
64 
65     void DrawPoint(const Point& point) override;
66     void DrawPoints(PointMode mode, size_t count, const Point pts[]) override;
67     void DrawLine(const Point& startPt, const Point& endPt) override;
68     void DrawRect(const Rect& rect) override;
69     void DrawRoundRect(const RoundRect& roundRect) override;
70     void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner) override;
71     void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle) override;
72     void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle) override;
73     void DrawOval(const Rect& oval) override;
74     void DrawCircle(const Point& centerPt, scalar radius) override;
75     void DrawPath(const Path& path) override;
76     void DrawBackground(const Brush& brush) override;
77     void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
78         Color ambientColor, Color spotColor, ShadowFlags flag) override;
79     void DrawShadowStyle(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
80         Color ambientColor, Color spotColor, ShadowFlags flag, bool isLimitElevation) override;
81     void DrawRegion(const Region& region) override;
82     void DrawPatch(const Point cubics[12], const ColorQuad colors[4],
83         const Point texCoords[4], BlendMode mode) override;
84     void DrawVertices(const Vertices& vertices, BlendMode mode) override;
85 
86     void DrawImageNine(const Image* image, const RectI& center, const Rect& dst,
87         FilterMode filterMode, const Brush* brush = nullptr) override;
88     void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst,
89         FilterMode filterMode) override;
90 
91     void DrawColor(ColorQuad color, BlendMode mode = BlendMode::SRC_OVER) override;
92 
93     void DrawAtlas(const Image* atlas, const RSXform xform[], const Rect tex[], const ColorQuad colors[],
94         int count, BlendMode mode, const SamplingOptions& sampling, const Rect* cullRect) override;
95     void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py) override;
96     void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling) override;
97     void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling,
98         SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) override;
99     void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling) override;
100     void DrawRecordCmd(const std::shared_ptr<RecordCmd> recordCmd, const Matrix* matrix, const Brush* brush) override;
101     void DrawPicture(const Picture& picture) override;
102     void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y) override;
103     void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate) override;
104 
105     void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias) override;
106     void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT) override;
107     void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias) override;
108     void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias) override;
109     void ClipPath(const Path& path, ClipOp op, bool doAntiAlias) override;
110     void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT) override;
111 
112     void SetMatrix(const Matrix& matrix) override;
113     void ResetMatrix() override;
114     void ConcatMatrix(const Matrix& matrix) override;
115     void Translate(scalar dx, scalar dy) override;
116     void Scale(scalar sx, scalar sy) override;
117     void Rotate(scalar deg, scalar sx, scalar sy) override;
118     void Shear(scalar sx, scalar sy) override;
119 
120     void Flush() override;
121     void Clear(ColorQuad color) override;
122     uint32_t Save() override;
123     void SaveLayer(const SaveLayerOps& saveLayerOps) override;
124     void Restore() override;
125     uint32_t GetSaveCount() const override;
126     void Discard() override;
127 
128     void ClipAdaptiveRoundRect(const std::vector<Point>& radius);
129 
130     void SetIsCustomTextType(bool isCustomTextType);
131     bool IsCustomTextType() const;
132 
133     void SetIsCustomTypeface(bool isCustomTypeface);
134     bool IsCustomTypeface() const;
135 
136     void SetIsRecordCmd(bool isRecordCmd);
137 
138     using DrawFunc = std::function<void(Drawing::Canvas* canvas, const Drawing::Rect* rect)>;
139 protected:
140     bool addDrawOpImmediate_ = true;
141     std::shared_ptr<DrawCmdList> cmdList_ = nullptr;
142 
143 private:
144     template<typename T, typename... Args>
145     void AddDrawOpImmediate(Args&&... args);
146     template<typename T, typename... Args>
147     void AddDrawOpDeferred(Args&&... args);
148 
149     enum SaveOpState { LazySaveOp, RealSaveOp };
150     void CheckForLazySave();
151     void GenerateCachedOpForTextblob(const TextBlob* blob, const scalar x, const scalar y);
152     void GenerateCachedOpForTextblob(const TextBlob* blob, const scalar x, const scalar y, Paint& paint);
153     bool isCustomTextType_ = false;
154     bool isCustomTypeface_ = false;
155     bool isRecordCmd_ = false;
156     std::optional<Brush> customTextBrush_ = std::nullopt;
157     std::optional<Pen> customTextPen_ = std::nullopt;
158     std::stack<SaveOpState> saveOpStateStack_;
159     std::shared_ptr<GPUContext> gpuContext_ = nullptr;
160     uint32_t opCount = 0;
161 };
162 } // namespace Drawing
163 } // namespace Rosen
164 } // namespace OHOS
165 #endif
166