• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 GRAPHIC_ADAPTER_H
17 #define GRAPHIC_ADAPTER_H
18 
19 #include <functional>
20 #include <memory>
21 
22 namespace OHOS::NWeb {
23 
24 enum class VSyncErrorCode {
25     SUCCESS,
26     ERROR,
27 };
28 
29 typedef void (*NWebVSyncCb)(int64_t, void*);
30 
31 class VSyncAdapter {
32 public:
33     VSyncAdapter() = default;
34 
35     virtual ~VSyncAdapter() = default;
36 
37     virtual VSyncErrorCode RequestVsync(void* data, NWebVSyncCb cb) = 0;
38 
39     virtual int64_t GetVSyncPeriod() = 0;
40 
41     virtual void SetFrameRateLinkerEnable(bool enabled) = 0;
42 
43     virtual void SetFramePreferredRate(int32_t preferredRate) = 0;
44 
45     virtual void SetOnVsyncCallback(void (*callback)()) = 0;
46 
SetOnVsyncEndCallback(void (* onVsyncEndCallback)())47     virtual void SetOnVsyncEndCallback(void (*onVsyncEndCallback)()) {}
48 
49     virtual void SetScene(const std::string& sceneName, uint32_t state) = 0;
50 
51     virtual void SetDVSyncSwitch(bool dvsyncSwitch) = 0;
52 };
53 
54 // be consistent with rom/foundation/graphic/graphic_2d/interfaces/inner_api/common/graphic_common_c.h:GSError
55 enum GSErrorCode {
56     GSERROR_OK = 0,
57 };
58 
59 // be consistent with rom/drivers/peripheral/display/interfaces/include/display_type.h:PixelFormat
60 enum PixelFormatAdapter {
61     PIXEL_FMT_CLUT8 = 0,                /**< CLUT8 format */
62     PIXEL_FMT_CLUT1,                    /**< CLUT1 format */
63     PIXEL_FMT_CLUT4,                    /**< CLUT4 format */
64     PIXEL_FMT_RGB_565,                  /**< RGB565 format */
65     PIXEL_FMT_RGBA_5658,                /**< RGBA5658 format */
66     PIXEL_FMT_RGBX_4444,                /**< RGBX4444 format */
67     PIXEL_FMT_RGBA_4444,                /**< RGBA4444 format */
68     PIXEL_FMT_RGB_444,                  /**< RGB444 format */
69     PIXEL_FMT_RGBX_5551,                /**< RGBX5551 format */
70     PIXEL_FMT_RGBA_5551,                /**< RGBA5551 format */
71     PIXEL_FMT_RGB_555,                  /**< RGB555 format */
72     PIXEL_FMT_RGBX_8888,                /**< RGBX8888 format */
73     PIXEL_FMT_RGBA_8888,                /**< RGBA8888 format */
74     PIXEL_FMT_RGB_888,                  /**< RGB888 format */
75     PIXEL_FMT_BGR_565,                  /**< BGR565 format */
76     PIXEL_FMT_BGRX_4444,                /**< BGRX4444 format */
77     PIXEL_FMT_BGRA_4444,                /**< BGRA4444 format */
78     PIXEL_FMT_BGRX_5551,                /**< BGRX5551 format */
79     PIXEL_FMT_BGRA_5551,                /**< BGRA5551 format */
80     PIXEL_FMT_BGRX_8888,                /**< BGRX8888 format */
81     PIXEL_FMT_BGRA_8888,                /**< BGRA8888 format */
82     PIXEL_FMT_YUV_422_I,                /**< YUV422 interleaved format */
83     PIXEL_FMT_YCBCR_422_SP,             /**< YCBCR422 semi-planar format */
84     PIXEL_FMT_YCRCB_422_SP,             /**< YCRCB422 semi-planar format */
85     PIXEL_FMT_YCBCR_420_SP,             /**< YCBCR420 semi-planar format */
86     PIXEL_FMT_YCRCB_420_SP,             /**< YCRCB420 semi-planar format */
87     PIXEL_FMT_YCBCR_422_P,              /**< YCBCR422 planar format */
88     PIXEL_FMT_YCRCB_422_P,              /**< YCRCB422 planar format */
89     PIXEL_FMT_YCBCR_420_P,              /**< YCBCR420 planar format */
90     PIXEL_FMT_YCRCB_420_P,              /**< YCRCB420 planar format */
91     PIXEL_FMT_YUYV_422_PKG,             /**< YUYV422 packed format */
92     PIXEL_FMT_UYVY_422_PKG,             /**< UYVY422 packed format */
93     PIXEL_FMT_YVYU_422_PKG,             /**< YVYU422 packed format */
94     PIXEL_FMT_VYUY_422_PKG,             /**< VYUY422 packed format */
95     PIXEL_FMT_VENDER_MASK = 0X7FFF0000, /**< vendor mask format */
96     PIXEL_FMT_BUTT = 0X7FFFFFFF         /**< Invalid pixel format */
97 };
98 
99 enum class ColorGamutAdapter : int32_t {
100     INVALID = -1,        /**< Invalid */
101     NATIVE = 0,          /**< Native or default */
102     STANDARD_BT601 = 1,  /**< Standard BT601 */
103     STANDARD_BT709 = 2,  /**< Standard BT709 */
104     DCI_P3 = 3,          /**< DCI P3 */
105     SRGB = 4,            /**< SRGB */
106     ADOBE_RGB = 5,       /**< Adobe RGB */
107     DISPLAY_P3 = 6,      /**< display P3 */
108     BT2020 = 7,          /**< BT2020 */
109     BT2100_PQ = 8,       /**< BT2100 PQ */
110     BT2100_HLG = 9,      /**< BT2100 HLG */
111     DISPLAY_BT2020 = 10, /**< Display BT2020 */
112 };
113 
114 enum class TransformTypeAdapter : int32_t {
115     ROTATE_NONE = 0, /**< No rotation */
116     ROTATE_90,       /**< Rotation by 90 degrees */
117     ROTATE_180,      /**< Rotation by 180 degrees */
118     ROTATE_270,      /**< Rotation by 270 degrees */
119     FLIP_H,          /**< Flip horizontally */
120     FLIP_V,          /**< Flip vertically */
121     FLIP_H_ROT90,    /**< Flip horizontally and rotate 90 degrees */
122     FLIP_V_ROT90,    /**< Flip vertically and rotate 90 degrees */
123     FLIP_H_ROT180,   /**< Flip horizontally and rotate 180 degrees */
124     FLIP_V_ROT180,   /**< Flip vertically and rotate 180 degrees */
125     FLIP_H_ROT270,   /**< Flip horizontally and rotate 270 degrees */
126     FLIP_V_ROT270,   /**< Flip vertically and rotate 270 degrees */
127     ROTATE_BUTT      /**< Invalid operation */
128 };
129 
130 class BufferRequestConfigAdapter {
131 public:
132     BufferRequestConfigAdapter() = default;
133 
134     virtual ~BufferRequestConfigAdapter() = default;
135 
136     virtual int32_t GetWidth() = 0;
137 
138     virtual int32_t GetHeight() = 0;
139 
140     virtual int32_t GetStrideAlignment() = 0;
141 
142     virtual int32_t GetFormat() = 0;
143 
144     virtual uint64_t GetUsage() = 0;
145 
146     virtual int32_t GetTimeout() = 0;
147 
148     virtual ColorGamutAdapter GetColorGamut() = 0;
149 
150     virtual TransformTypeAdapter GetTransformType() = 0;
151 };
152 
153 class BufferFlushConfigAdapter {
154 public:
155     BufferFlushConfigAdapter() = default;
156 
157     virtual ~BufferFlushConfigAdapter() = default;
158 
159     virtual int32_t GetX() = 0;
160 
161     virtual int32_t GetY() = 0;
162 
163     virtual int32_t GetW() = 0;
164 
165     virtual int32_t GetH() = 0;
166 
167     virtual int64_t GetTimestamp() = 0;
168 };
169 
170 class SurfaceBufferAdapter {
171 public:
172     SurfaceBufferAdapter() = default;
173 
174     virtual ~SurfaceBufferAdapter() = default;
175 
176     virtual int32_t GetFileDescriptor() = 0;
177 
178     virtual int32_t GetWidth() = 0;
179 
180     virtual int32_t GetHeight() = 0;
181 
182     virtual int32_t GetStride() = 0;
183 
184     virtual int32_t GetFormat() = 0;
185 
186     virtual uint32_t GetSize() = 0;
187 
188     virtual void* GetVirAddr() = 0;
189 
190 protected:
191     SurfaceBufferAdapter(const SurfaceBufferAdapter&) = delete;
192 
193     SurfaceBufferAdapter& operator=(const SurfaceBufferAdapter&) = delete;
194 };
195 
196 class IBufferConsumerListenerAdapter {
197 public:
198     virtual ~IBufferConsumerListenerAdapter() = default;
199 
200     virtual void OnBufferAvailable(std::shared_ptr<SurfaceBufferAdapter> buffer) = 0;
201 };
202 
203 class IConsumerSurfaceAdapter {
204 public:
205     IConsumerSurfaceAdapter() = default;
206 
207     virtual ~IConsumerSurfaceAdapter() = default;
208 
209     virtual int32_t RegisterConsumerListener(std::shared_ptr<IBufferConsumerListenerAdapter> listener) = 0;
210 
211     virtual int32_t ReleaseBuffer(std::shared_ptr<SurfaceBufferAdapter> buffer, int32_t fence) = 0;
212 
213     virtual int32_t SetUserData(const std::string& key, const std::string& val) = 0;
214 
215     virtual int32_t SetQueueSize(uint32_t queueSize) = 0;
216 };
217 
218 using NWebNativeWindow = void*;
219 
220 class WindowAdapter {
221 public:
222     WindowAdapter() = default;
223 
224     virtual ~WindowAdapter() = default;
225 
226     virtual NWebNativeWindow CreateNativeWindowFromSurface(void* pSurface) = 0;
227 
228     virtual void DestroyNativeWindow(NWebNativeWindow window) = 0;
229 
230     virtual int32_t NativeWindowSetBufferGeometry(NWebNativeWindow window, int32_t width, int32_t height) = 0;
231 
232     virtual void NativeWindowSurfaceCleanCache(NWebNativeWindow window) = 0;
233 
234     virtual void NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window, bool cleanAll) = 0;
235 
236     virtual void SetTransformHint(uint32_t rotation, NWebNativeWindow window) = 0;
237 
238     virtual void AddNativeWindowRef(NWebNativeWindow window) = 0;
239 
240     virtual void NativeWindowUnRef(NWebNativeWindow window) = 0;
241 };
242 
243 class AshmemAdapter {
244 public:
245     static int AshmemCreate(const char* name, size_t size);
246 };
247 
248 typedef void (*OnFrameAvailableCb)(void* ctx);
249 
250 class FrameAvailableListener {
251 public:
252     FrameAvailableListener() = default;
253 
254     virtual ~FrameAvailableListener() = default;
255 
256     virtual void* GetContext() = 0;
257 
258     virtual OnFrameAvailableCb GetOnFrameAvailableCb() = 0;
259 };
260 
261 class NativeImageAdapter {
262 public:
263     NativeImageAdapter() = default;
264 
265     virtual ~NativeImageAdapter() = default;
266 
267     virtual void CreateNativeImage(uint32_t textureId, uint32_t textureTarget) = 0;
268 
269     virtual NWebNativeWindow AquireNativeWindowFromNativeImage() = 0;
270 
271     virtual int32_t AttachContext(uint32_t textureId) = 0;
272 
273     virtual int32_t DetachContext() = 0;
274 
275     virtual int32_t UpdateSurfaceImage() = 0;
276 
277     virtual int64_t GetTimestamp() = 0;
278 
279     virtual int32_t GetTransformMatrix(float matrix[16]) = 0;
280 
281     virtual int32_t GetSurfaceId(uint64_t* surfaceId) = 0;
282 
283     virtual int32_t SetOnFrameAvailableListener(std::shared_ptr<FrameAvailableListener> listener) = 0;
284 
285     virtual int32_t UnsetOnFrameAvailableListener() = 0;
286 
287     virtual void DestroyNativeImage() = 0;
288 
289     virtual void NewNativeImage() = 0;
290 
291     virtual int32_t AcquireNativeWindowBuffer(
292         void** windowBuffer,
293         int* acquireFenceFd) = 0;
294 
295     virtual int32_t GetNativeBuffer(
296         void* windowBuffer,
297         void** nativeBuffer) = 0;
298 
299     virtual int32_t ReleaseNativeWindowBuffer(void* windowBuffer, int fenceFd) = 0;
300 
301     virtual void GetNativeWindowBufferSize(
302         void* windowBuffer,
303         uint32_t* width,
304         uint32_t* height) = 0;
305 };
306 
307 class ProducerSurfaceAdapter {
308 public:
309     ProducerSurfaceAdapter() = default;
310 
311     virtual ~ProducerSurfaceAdapter() = default;
312 
313     virtual std::shared_ptr<SurfaceBufferAdapter> RequestBuffer(
314         int32_t& fence, std::shared_ptr<BufferRequestConfigAdapter> config) = 0;
315 
316     virtual int32_t FlushBuffer(std::shared_ptr<SurfaceBufferAdapter> buffer, int32_t fence,
317         std::shared_ptr<BufferFlushConfigAdapter> config) = 0;
318 };
319 } // namespace OHOS::NWeb
320 
321 #endif // GRAPHIC_ADAPTER_H
322