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 INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H
17 #define INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H
18 
19 #include <refbase.h>
20 
21 #include "surface.h"
22 
23 struct NativeSurfaceCallback;
24 
25 namespace OHOS {
26 
27 enum class SurfaceDelegateError : int32_t {
28     SURFACE_DELEGATE_ERROR_DEFAULT = -1,
29     SURFACE_DELEGATE_OK = 0,
30     SURFACE_DELEGATE_DO_NOTHING = 1,
31     SURFACE_DELEGATE_ERROR_SAMGR = 100,
32     SURFACE_DELEGATE_ERROR_IPC_FAILED = 101,
33     SURFACE_DELEGATE_ERROR_NO_MEM = 110,
34     SURFACE_DELEGATE_ERROR_NULLPTR = 120,
35     SURFACE_DELEGATE_ERROR_INVALID_PARAM = 130,
36     SURFACE_DELEGATE_ERROR_DESTROYED_OBJECT = 140,
37     SURFACE_DELEGATE_ERROR_DEATH_RECIPIENT = 150,
38     SURFACE_DELEGATE_ERROR_INVALID_WINDOW = 160,
39     SURFACE_DELEGATE_ERROR_INVALID_OPERATION = 170,
40     SURFACE_DELEGATE_ERROR_INVALID_TYPE = 180,
41     SURFACE_DELEGATE_ERROR_INVALID_PERMISSION = 190,
42     SURFACE_DELEGATE_ERROR_UNKNOWN,
43 };
44 
45 class SurfaceDelegate : public RefBase {
46 public:
47     /*
48      * Define callback for the surface lifecycle.
49      */
50     class ISurfaceCallback : virtual public RefBase {
51         public:
52             /*
53              * Callback when the surface was created.
54              *
55              * @param surface the surface.
56              */
57             virtual void OnSurfaceCreated(const sptr<Surface>& surface) = 0;
58 
59             /*
60              * Callback when the surface was changed.
61              *
62              * @param surface the surface.
63              * @param width the surface width.
64              * @param height the surface height.
65              */
66             virtual void OnSurfaceChanged(const sptr<Surface>& surface, int32_t width, int32_t height) = 0;
67 
68             /*
69              * Callback when the surface was destroyed.
70              */
71             virtual void OnSurfaceDestroyed() = 0;
72     };
73 
74     /*
75      * Add the surface callback.
76      *
77      * @param callback the ISurfaceCallback.
78      */
79     void AddSurfaceCallback(const sptr<ISurfaceCallback>& callback);
80 
81     /*
82      * Remove the surface callback.
83      *
84      * @param callback the ISurfaceCallback.
85      */
86     void RemoveSurfaceCallback(const sptr<ISurfaceCallback>& callback);
87 
88     /*
89      * Constructor of SurfaceDelegate.
90      *
91      * @param windowId the window id the surface will be on.
92      */
93     SurfaceDelegate(int windowId);
94 
95     /*
96      * Destructor of SurfaceDelegate.
97      */
98     ~SurfaceDelegate() = default;
99 
100     /*
101      * Create the surface.
102      *
103      * @param isWindowSurface if the surface is a window type surface.
104      * @return the SurfaceDelegateError code.
105      */
106     SurfaceDelegateError CreateSurface(bool isWindowSurface = false);
107 
108     /*
109      * Set the boundaries of the area where the surface is located.
110      *
111      * @param left the left of the boundaries.
112      * @param right the right of the boundaries.
113      * @param width the width of the boundaries.
114      * @param height the height of the boundaries.
115      * @return the SurfaceDelegateError code.
116      */
117     SurfaceDelegateError SetBounds(int32_t left, int32_t right, int32_t width, int32_t height);
118 
119     /*
120      * Release the surface.
121      *
122      * @return the SurfaceDelegateError code.
123      */
124     SurfaceDelegateError ReleaseSurface();
125 
126     /*
127      * Set the surface size.
128      *
129      * @param width the surface width.
130      * @param height the surface height.
131      * @return the SurfaceDelegateError code.
132      */
133     SurfaceDelegateError SetSurfaceSize(uint32_t width, uint32_t height);
134 
135     /*
136      * Get the surface.
137      *
138      * @return the surface.
139      */
140     sptr<Surface> GetSurface();
141 
142     /*
143      * Get the native window.
144      *
145      * @return the void pointer of the native window.
146      */
147     void* GetNativeWindow();
148 
149 private:
150     NativeSurfaceCallback CreateNativeSurfaceCallback();
151 
152     sptr<NativeSurface> GetNativeSurface();
153 
154     void OnSurfaceCreated(uint32_t windowId, uint64_t surfaceId);
155     void OnSurfaceChanged(uint32_t windowId, uint64_t surfaceId, int32_t width, int32_t height);
156     void OnSurfaceDestroyed(uint32_t windowId, uint64_t surfaceId);
157 
158     void OnNativeSurfaceCreated(void* nativeWindow);
159     void OnNativeSurfaceDestroyed();
160 
161     sptr<Surface> surface_ = nullptr;
162 
163     std::vector<sptr<ISurfaceCallback>> surfaceCallbacks_;
164     std::recursive_mutex mutex_;
165 
166     int windowId_ = 0;
167 };
168 } // namespace OHOS
169 #endif // INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H