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 DRAWING_SURFACE_H
17 #define DRAWING_SURFACE_H
18 
19 #include "impl_interface/surface_impl.h"
20 
21 #include "draw/canvas.h"
22 #include "image/bitmap.h"
23 #include "image/image.h"
24 #include "utils/drawing_macros.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 
30 #ifdef RS_ENABLE_GPU
31 struct FrameBuffer {
32     int width;
33     int height;
34     int FBOID;
35     int Format;
36     ColorType colorType = Drawing::COLORTYPE_RGBA_8888;
37     std::shared_ptr<GPUContext> gpuContext;
38     std::shared_ptr<ColorSpace> colorSpace;
39 };
40 #endif
41 
42 struct FlushInfo {
43     bool backendSurfaceAccess = false;
44     size_t numSemaphores = 0;
45     void* backendSemaphore = nullptr;
46     void (*finishedProc)(void* finishedContext) = nullptr;
47     void* finishedContext = nullptr;
48     void (*submittedProc)(void* finishedContext, bool success) = nullptr;
49     void* submittedContext = nullptr;
50 };
51 
52 enum class BackendAccess {
53     FLUSH_READ,
54     FLUSH_WRITE,
55     DISCARD_WRITE
56 };
57 
58 class DRAWING_API Surface {
59 public:
60     Surface();
~Surface()61     ~Surface() {}
62 
63     /**
64      * @brief         Bind raster Surface.
65      * @param bitmap  Raster pixel array.
66      * @return        true if Bind success.
67      */
68     bool Bind(const Bitmap& bitmap);
69 
70 #ifdef RS_ENABLE_GPU
71     /**
72      * @brief         Bind GPU texture Surface.
73      * @param image   In GPU memory as a GPU texture.
74      * @return        true if Bind success.
75      */
76     bool Bind(const Image& image);
77 
78     /**
79      * @brief       Bind
80      * @param info  FrameBuffer object info.
81      * @return      true if Bind success.
82      */
83     bool Bind(const FrameBuffer& frameBuffer);
84 
85 #ifdef RS_ENABLE_VK
86     static std::shared_ptr<Surface> MakeFromBackendRenderTarget(GPUContext* gpuContext, const TextureInfo& info,
87         TextureOrigin origin, ColorType colorType, std::shared_ptr<ColorSpace> colorSpace,
88         void (*deleteVkImage)(void *), void* cleanHelper);
89 #endif
90     static std::shared_ptr<Surface> MakeFromBackendTexture(GPUContext* gpuContext, const TextureInfo& info,
91         TextureOrigin origin, int sampleCnt, ColorType colorType,
92         std::shared_ptr<ColorSpace> colorSpace, void (*deleteVkImage)(void *), void* cleanHelper);
93 
94     /**
95      * @brief              Create Surface from gpuContext and imageInfo.
96      * @param gpuContext   GPU texture.
97      * @param budgeted     Texture count.
98      * @param imageInfo    image Info.
99      * @return             A shared pointer to Surface.
100      */
101     static std::shared_ptr<Surface> MakeRenderTarget(GPUContext* gpuContext, bool budgeted, const ImageInfo& imageInfo);
102 #endif
103 
104     /**
105      * @brief              Allocates raster Surface.
106      * @param imageInfo    image info.
107      * @return             A shared pointer to Surface.
108      */
109     static std::shared_ptr<Surface> MakeRaster(const ImageInfo& imageInfo);
110 
111     /**
112      * @brief              Allocates raster direct Surface.
113      * @param imageInfo    image info.
114      * @param pixels       Pointer to destination pixels buffer.
115      * @param rowBytes     Interval from one Surface row to the next.
116      * @return             A shared pointer to Surface.
117      */
118     static std::shared_ptr<Surface> MakeRasterDirect(const ImageInfo& imageInfo, void* pixels, size_t rowBytes);
119 
120     /**
121      * @brief          Create Surface using width and height.
122      * @param width    Pixel column count.
123      * @param height   Pixel row count.
124      * @return         A shared pointer to Surface.
125      */
126     static std::shared_ptr<Surface> MakeRasterN32Premul(int32_t width, int32_t height);
127 
128     /**
129      * @brief   Gets Canvas that draws into Surface.
130      */
131     std::shared_ptr<Canvas> GetCanvas();
132 
133     /**
134      * @brief   Gets Image capturing Surface contents.
135      * @return  A shared pointer to Image
136      */
137     std::shared_ptr<Image> GetImageSnapshot() const;
138 
139     /**
140      * @brief         Gets Image capturing Surface contents.
141      * @param bounds  Bounds.
142      *                If bounds extends beyond the Surface, it will be trimmed to just the intersection of it
143      *                and the Surface.
144      *                If bounds does not intersect the surface, then this returns nullptr.
145      *                If bounds == the surface, then this is the same as calling the no-parameter variant.
146      * @return        A shared pointer to Image
147      */
148     std::shared_ptr<Image> GetImageSnapshot(const RectI& bounds) const;
149 
150     /**
151      * @brief         Returns a compatible Surface, with the specified width and height.
152      * @param width   surface width
153      * @param height  surface height
154      * @return        A shared pointer to Surface
155      */
156     std::shared_ptr<Surface> MakeSurface(int width, int height) const;
157 
158     /**
159      * @brief         Returns a compatible Surface, with the specified width and height.
160      * @param imageinfo   surface imageinfo
161      * @return        A shared pointer to Surface
162      */
163     std::shared_ptr<Surface> MakeSurface(const ImageInfo& imageinfo) const;
164 
165     /**
166      * @brief   Gets ImageInfo of Surface.
167      * @return  ImageInfo
168      */
169     ImageInfo GetImageInfo();
170 
171     /**
172      * @brief         Gets BackendTexture of Surface.
173      * @param access  backend access mode, default as FLUSH_READ
174      * @return        BackendTexture
175      */
176     BackendTexture GetBackendTexture(BackendAccess access = BackendAccess::FLUSH_READ) const;
177 
178     /**
179      * @brief   Call to ensure all reads/writes of surface have been issue to the underlying 3D API.
180      */
181     void FlushAndSubmit(bool syncCpu = false);
182 
183     /**
184      * @brief   Call to ensure all reads/writes of surface have been issue to the underlying 3D API.
185      */
186     void Flush(FlushInfo *drawingflushInfo = nullptr);
187 
188     int Width() const;
189     int Height() const;
190 
191     template<typename T>
GetImpl() const192     T* GetImpl() const
193     {
194         return impl_->DowncastingTo<T>();
195     }
196 
197 #ifdef RS_ENABLE_GL
198     void Wait(const std::vector<GrGLsync>& syncs);
199 #endif
200 
201 #ifdef RS_ENABLE_VK
202     void Wait(int32_t time, const VkSemaphore& semaphore);
203     void SetDrawingArea(const std::vector<RectI>& rects);
204     void ClearDrawingArea();
205 #endif
206 
207 private:
208     std::shared_ptr<SurfaceImpl> impl_;
209     std::shared_ptr<Canvas> cachedCanvas_;
210 };
211 } // namespace Drawing
212 } // namespace Rosen
213 } // namespace OHOS
214 #endif
215