1 /*
2  * Copyright (c) 2023-2024 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 VERTICES_H
17 #define VERTICES_H
18 
19 #include "drawing/engine_adapter/impl_interface/vertices_impl.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 
25 enum class VertexMode {
26     TRIANGLES_VERTEXMODE,
27     TRIANGLESSTRIP_VERTEXMODE,
28     TRIANGLEFAN_VERTEXMODE,
29 
30     LAST_VERTEXMODE = TRIANGLEFAN_VERTEXMODE,
31 };
32 
33 enum BuilderFlags {
34     HAS_TEXCOORDS_BUILDER_FLAG = 1 << 0,
35     HAS_COLORS_BUILDER_FLAG = 1 << 1,
36 };
37 
38 class DRAWING_API Vertices {
39 public:
40     Vertices() noexcept;
41     explicit Vertices(std::shared_ptr<VerticesImpl>) noexcept;
~Vertices()42     virtual ~Vertices() {};
43 
44     /**
45      * @brief Make a copy from vertices data with index.
46      * @param mode         vertex mode.
47      * @param vertexCount  Vertex count.
48      * @param positions    Positions data pointer.
49      * @param tex          Texture coordinate data pointer.
50      * @param colors       Color data pointer.
51      * @param indexCount   Index count.
52      * @param indices      Index data pointer.
53      * @return             Returns true if MakeCopy succeed.
54      */
55     bool MakeCopy(VertexMode mode,
56         int vertexCount, const Point positions[], const Point texs[], const ColorQuad colors[],
57         int indexCount, const uint16_t indices[]);
58 
59     /**
60      * @brief Make a copy from vertices data.
61      * @param mode         vertex mode.
62      * @param vertexCount  Vertex count.
63      * @param positions    Positions data pointer.
64      * @param tex          Texture coordinate data pointer.
65      * @param colors       Color data pointer.
66      * @return             Returns true if MakeCopy succeed.
67      */
68     bool MakeCopy(VertexMode mode,
69         int vertexCount, const Point positions[], const Point texs[], const ColorQuad colors[]);
70 
71     /**
72      * @brief Get the adaptation layer instance, called in the adaptation layer.
73      * @return  Adaptation Layer instance.
74      */
75     template<typename T>
GetImpl()76     T* GetImpl() const
77     {
78         if (verticesImplPtr_ == nullptr) {
79             return nullptr;
80         }
81         return verticesImplPtr_->DowncastingTo<T>();
82     }
83     std::shared_ptr<Data> Serialize() const;
84     bool Deserialize(std::shared_ptr<Data> data);
85     class DRAWING_API Builder {
86     public:
87         Builder(VertexMode mode, int vertexCount, int indexCount, uint32_t flags);
~Builder()88         virtual ~Builder() {};
89 
90         /**
91          * @brief Return if builder is valid.
92          */
93         bool IsValid();
94 
95         /**
96          * @brief Return positions data pointer in the builder.
97          */
98         Point* Positions();
99 
100         /**
101          * @brief Return indices data pointer in the builder.
102          */
103         uint16_t* Indices();
104 
105         /**
106          * @brief Return texture coordinate data pointer in the builder.
107          */
108         Point* TexCoords();
109 
110         /**
111          * @brief Return color data pointer in theBuilder.
112          */
113         ColorQuad* Colors();
114 
115         /**
116          * @brief Detach the built vertices object. After the first call, this will always return null.
117          * @return Return a shared pointer of Vertices object or nullptr.
118          */
119         std::shared_ptr<Vertices> Detach();
120 
121     private:
122         std::shared_ptr<VerticesImpl::BuilderImpl> builderImplPtr_;
123     };
124 
125 private:
126     std::shared_ptr<VerticesImpl> verticesImplPtr_;
127 };
128 } // namespace Drawing
129 } // namespace Rosen
130 } // namespace OHOS
131 #endif
132