• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.. All rights reserved.
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 CORE_CANVAS_H
17 #define CORE_CANVAS_H
18 
19 #include <memory>
20 
21 #include "drawing/engine_adapter/impl_interface/core_canvas_impl.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 enum class SrcRectConstraint {
27     STRICT_SRC_RECT_CONSTRAINT,
28     FAST_SRC_RECT_CONSTRAINT,
29 };
30 /*
31  * @brief  Contains the option used to create the layer.
32  */
33 class SaveLayerOps {
34 public:
35     // How to allocate layer
36     enum Flags {
37         INIT_WITH_PREVIOUS = 1 << 1,    // create with previous contents
38     };
39 
SaveLayerOps()40     SaveLayerOps() : bounds_(nullptr), brush_(nullptr), imageFilter_(nullptr), saveLayerFlags_(0) {}
41     SaveLayerOps(const Rect* bounds, const Brush* brush, uint32_t saveLayerFlags = 0)
SaveLayerOps(bounds,brush,nullptr,saveLayerFlags)42         : SaveLayerOps(bounds, brush, nullptr, saveLayerFlags) {}
43 
44     /*
45      * @param bounds          The bounds of layer, may be nullptr.
46      * @param brush           When restoring the current layer, attach this brush, may be nullptr.
47      * @param imageFilter     Use this to filter the current layer as the new layer background, may be nullptr.
48      * @param saveLayerFlags  How to allocate layer.
49      */
50     SaveLayerOps(const Rect* bounds, const Brush* brush, const ImageFilter* imageFilter, uint32_t saveLayerFlags = 0)
bounds_(bounds)51         : bounds_(bounds), brush_(brush), imageFilter_(imageFilter), saveLayerFlags_(saveLayerFlags) {}
~SaveLayerOps()52     ~SaveLayerOps() {}
53 
54     /*
55      * @brief  Gets the bounds of layer, may be nullptr.
56      */
GetBounds()57     const Rect* GetBounds() const
58     {
59         return bounds_;
60     }
61 
62     /*
63      * @brief  Gets the brush of layer, may be nullptr.
64      */
GetBrush()65     const Brush* GetBrush() const
66     {
67         return brush_;
68     }
69 
70     /*
71      * @brief  Gets the image filter of layer, may be nullptr.
72      */
GetImageFilter()73     const ImageFilter* GetImageFilter() const
74     {
75         return imageFilter_;
76     }
77 
78     /*
79      * @brief  Gets the options to modify layer.
80      */
GetSaveLayerFlags()81     uint32_t GetSaveLayerFlags() const
82     {
83         return saveLayerFlags_;
84     }
85 
86 private:
87     const Rect* bounds_;
88     const Brush* brush_;
89     const ImageFilter* imageFilter_;
90     uint32_t saveLayerFlags_;
91 };
92 
93 class CoreCanvas {
94 public:
95     CoreCanvas();
96     explicit CoreCanvas(void* rawCanvas);
~CoreCanvas()97     virtual ~CoreCanvas() {}
98     void Bind(const Bitmap& bitmap);
99 
GetDrawingType()100     virtual DrawingType GetDrawingType() const
101     {
102         return DrawingType::COMMON;
103     }
104 
105     /*
106      * @brief  Gets the total matrix of Canvas to device.
107      */
108     Matrix GetTotalMatrix() const;
109 
110     /*
111      * @brief  Gets bounds of clip in local coordinates.
112      */
113     Rect GetLocalClipBounds() const;
114 
115     /*
116      * @brief  Gets bounds of clip in device corrdinates.
117      */
118     RectI GetDeviceClipBounds() const;
119 
120     /*
121      * @brief  Gets GPU context of the GPU surface associated with Canvas.
122      */
123 #ifdef ACE_ENABLE_GPU
124     virtual std::shared_ptr<GPUContext> GetGPUContext() const;
125 #endif
126 
127     /*
128      * @brief  Gets width of Canvas.
129      */
130     int32_t GetWidth() const;
131 
132     /*
133      * @brief  Gets height of Canvas.
134      */
135     int32_t GetHeight() const;
136 
137     // shapes
138     virtual void DrawPoint(const Point& point);
139     virtual void DrawLine(const Point& startPt, const Point& endPt);
140     virtual void DrawRect(const Rect& rect);
141     virtual void DrawRoundRect(const RoundRect& roundRect);
142     virtual void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner);
143     virtual void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle);
144     virtual void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle);
145     virtual void DrawOval(const Rect& oval);
146     virtual void DrawCircle(const Point& centerPt, scalar radius);
147     virtual void DrawPath(const Path& path);
148     virtual void DrawBackground(const Brush& brush);
149     virtual void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius,
150         Color ambientColor, Color spotColor, ShadowFlags flag);
151 
152     /*
153      * @brief         Draws Region on the Canvas.
154      * @param region  Region to draw.
155      */
156     virtual void DrawRegion(const Region& region);
157 
158     // image
159     virtual void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py);
160     virtual void DrawBitmap(Media::PixelMap& pixelMap, const scalar px, const scalar py);
161     virtual void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling);
162     virtual void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling,
163         SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
164     virtual void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling);
165     virtual void DrawPicture(const Picture& picture);
166 
167     // temporary interface. Support drawing of SkSVGDOM
168     virtual void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom);
169 
170     // clip
171     /*
172      * @brief              Replace the clipping area with the intersection or difference between the
173                            current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased.
174      * @param rect         To combine with clipping area.
175      * @param op           To apply to clip.
176      * @param doAntiAlias  true if clip is to be anti-aliased. The default value is false.
177      */
178     virtual void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias = false);
179 
180     /*
181      * @brief              Replace the clipping area with the intersection or difference of the
182                            current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased.
183      * @param roundRect    To combine with clip.
184      * @param op           To apply to clip.
185      * @param doAntiAlias  true if clip is to be anti-aliased. The default value is false.
186      */
187     virtual void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias = false);
188 
189     /*
190      * @brief              Replace the clipping area with the intersection or difference of the
191                            current clipping area and Path, and use a clipping edge that is aliased or anti-aliased.
192      * @param path         To combine with clip.
193      * @param op           To apply to clip.
194      * @param doAntiAlias  true if clip is to be anti-aliased. The default value is false.
195      */
196     virtual void ClipPath(const Path& path, ClipOp op, bool doAntiAlias = false);
197 
198     // transform
199     virtual void SetMatrix(const Matrix& matrix);
200     virtual void ResetMatrix();
201     virtual void ConcatMatrix(const Matrix& matrix);
202     virtual void Translate(scalar dx, scalar dy);
203     virtual void Scale(scalar sx, scalar sy);
204 
205     /*
206      * @brief      Rotates Matrix by degrees.
207      * @param deg  Amount to rotate, in degrees.
208      */
Rotate(scalar deg)209     void Rotate(scalar deg)
210     {
211         Rotate(deg, 0, 0);
212     }
213     virtual void Rotate(scalar deg, scalar sx, scalar sy);
214     virtual void Shear(scalar sx, scalar sy);
215 
216     // state
217     virtual void Flush();
218     virtual void Clear(ColorQuad color);
219     virtual void Save();
220 
221     /*
222      * @brief               Saves Matrix and clipping area, and allocates Surface for subsequent drawing.
223      * @param saveLayerOps  Contains the option used to create the layer.
224      */
225     virtual void SaveLayer(const SaveLayerOps& saveLayerOps);
226     virtual void Restore();
227 
228     /*
229      * @brief  Returns the number of saved states, each containing Matrix and clipping area.
230      */
231     uint32_t GetSaveCount() const;
232 
233     // paint
234     virtual CoreCanvas& AttachPen(const Pen& pen);
235     virtual CoreCanvas& AttachBrush(const Brush& brush);
236     virtual CoreCanvas& DetachPen();
237     virtual CoreCanvas& DetachBrush();
238 
239     template<typename T>
GetImpl()240     const std::shared_ptr<T> GetImpl() const
241     {
242         return impl_->DowncastingTo<T>();
243     }
244     std::shared_ptr<CoreCanvasImpl> GetCanvasData() const;
245 
246 protected:
247     CoreCanvas(int32_t width, int32_t height);
248 
249 private:
250     std::shared_ptr<CoreCanvasImpl> impl_;
251 };
252 } // namespace Drawing
253 } // namespace Rosen
254 } // namespace OHOS
255 #endif
256