• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ACE_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      */
67     bool Bind(const Bitmap& bitmap);
68 
69 #ifdef ACE_ENABLE_GPU
70     /*
71      * @brief         Bind GPU texture Surface.
72      * @param image   In GPU memory as a GPU texture.
73      */
74     bool Bind(const Image& image);
75 
76     /*
77      * @brief       Bind
78      * @param info  FrameBuffer object info.
79      */
80     bool Bind(const FrameBuffer& frameBuffer);
81 
82 #ifdef RS_ENABLE_VK
83     static std::shared_ptr<Surface> MakeFromBackendRenderTarget(GPUContext* gpuContext, const TextureInfo& info,
84         TextureOrigin origin, ColorType colorType, std::shared_ptr<ColorSpace> colorSpace,
85         void (*deleteVkImage)(void *), void* cleanHelper);
86     static std::shared_ptr<Surface> MakeFromBackendTexture(GPUContext* gpuContext, const TextureInfo& info,
87         TextureOrigin origin, int sampleCnt, ColorType colorType,
88         std::shared_ptr<ColorSpace> colorSpace, void (*deleteVkImage)(void *), void* cleanHelper);
89 #endif
90 
91     /*
92      * @brief              Create Surface from gpuContext and imageInfo.
93      * @param gpuContext   GPU texture.
94      * @param budgeted     Texture count.
95      * @param imageInfo    image Info.
96      * @return             A shared point to Surface.
97      */
98     static std::shared_ptr<Surface> MakeRenderTarget(GPUContext* gpuContext, bool budgeted, const ImageInfo& imageInfo);
99 #endif
100 
101     /*
102      * @brief              Allocates raster Surface.
103      * @param imageInfo    image info.
104      * @return             A shared point to Surface.
105      */
106     static std::shared_ptr<Surface> MakeRaster(const ImageInfo& imageInfo);
107 
108     /*
109      * @brief              Allocates raster direct Surface.
110      * @param imageInfo    image info.
111      * @param pixels       Pointer to destination pixels buffer.
112      * @param rowBytes     Interval from one Surface row to the next.
113      * @return             A shared point to Surface.
114      */
115     static std::shared_ptr<Surface> MakeRasterDirect(const ImageInfo& imageInfo, void* pixels, size_t rowBytes);
116 
117     /*
118      * @brief          Create Surface using width and height.
119      * @param width    Pixel column count.
120      * @param height   Pixel row count.
121      * @return         A shared point to Surface.
122      */
123     static std::shared_ptr<Surface> MakeRasterN32Premul(int32_t width, int32_t height);
124 
125     /*
126      * @brief   Gets Canvas that draws into Surface.
127      */
128     std::shared_ptr<Canvas> GetCanvas();
129 
130     /*
131      * @brief   Gets Image capturing Surface contents.
132      */
133     std::shared_ptr<Image> GetImageSnapshot() const;
134 
135     /*
136      * @brief         Gets Image capturing Surface contents.
137      * @param bounds  Bounds.
138      *                If bounds extends beyond the Surface, it will be trimmed to just the intersection of it
139      *                and the Surface.
140      *                If bounds does not intersect the surface, then this returns nullptr.
141      *                If bounds == the surface, then this is the same as calling the no-parameter variant.
142      */
143     std::shared_ptr<Image> GetImageSnapshot(const RectI& bounds) const;
144 
145     /*
146      * @brief   Returns a compatible Surface, with the specified widht and height
147      */
148     std::shared_ptr<Surface> MakeSurface(int width, int height) const;
149 
150     /*
151      * @brief   Gets ImageInfo of Surface
152      */
153     ImageInfo GetImageInfo();
154 
155     /*
156      * @brief   Gets BackendTexture of Surface
157      */
158     BackendTexture GetBackendTexture(BackendAccess access = BackendAccess::FLUSH_READ) const;
159 
160     /*
161      * @brief   Call to ensure all reads/writes of surface have been issue to the underlying 3D API.
162      */
163     void FlushAndSubmit(bool syncCpu = false);
164 
165     /*
166      * @brief   Call to ensure all reads/writes of surface have been issue to the underlying 3D API.
167      */
168     void Flush(FlushInfo *drawingflushInfo = nullptr);
169 
170     int Width() const;
171     int Height() const;
172 
173     template<typename T>
GetImpl()174     T* GetImpl() const
175     {
176         return impl_->DowncastingTo<T>();
177     }
178 
179 #ifdef RS_ENABLE_VK
180     void Wait(int32_t time, const VkSemaphore& semaphore);
181     void SetDrawingArea(const std::vector<RectI>& rects);
182     void ClearDrawingArea();
183 #endif
184 
185 private:
186     std::shared_ptr<SurfaceImpl> impl_;
187     std::shared_ptr<Canvas> cachedCanvas_;
188 };
189 } // namespace Drawing
190 } // namespace Rosen
191 } // namespace OHOS
192 #endif
193