132a6e48fSopenharmony_ci/*
232a6e48fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
332a6e48fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
432a6e48fSopenharmony_ci * you may not use this file except in compliance with the License.
532a6e48fSopenharmony_ci * You may obtain a copy of the License at
632a6e48fSopenharmony_ci *
732a6e48fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
832a6e48fSopenharmony_ci *
932a6e48fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1032a6e48fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1132a6e48fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1232a6e48fSopenharmony_ci * See the License for the specific language governing permissions and
1332a6e48fSopenharmony_ci * limitations under the License.
1432a6e48fSopenharmony_ci */
1532a6e48fSopenharmony_ci
1632a6e48fSopenharmony_ci#ifndef INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H
1732a6e48fSopenharmony_ci#define INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H
1832a6e48fSopenharmony_ci
1932a6e48fSopenharmony_ci#include <refbase.h>
2032a6e48fSopenharmony_ci
2132a6e48fSopenharmony_ci#include "surface.h"
2232a6e48fSopenharmony_ci
2332a6e48fSopenharmony_cistruct NativeSurfaceCallback;
2432a6e48fSopenharmony_ci
2532a6e48fSopenharmony_cinamespace OHOS {
2632a6e48fSopenharmony_ci
2732a6e48fSopenharmony_cienum class SurfaceDelegateError : int32_t {
2832a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_DEFAULT = -1,
2932a6e48fSopenharmony_ci    SURFACE_DELEGATE_OK = 0,
3032a6e48fSopenharmony_ci    SURFACE_DELEGATE_DO_NOTHING = 1,
3132a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_SAMGR = 100,
3232a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_IPC_FAILED = 101,
3332a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_NO_MEM = 110,
3432a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_NULLPTR = 120,
3532a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_INVALID_PARAM = 130,
3632a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_DESTROYED_OBJECT = 140,
3732a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_DEATH_RECIPIENT = 150,
3832a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_INVALID_WINDOW = 160,
3932a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_INVALID_OPERATION = 170,
4032a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_INVALID_TYPE = 180,
4132a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_INVALID_PERMISSION = 190,
4232a6e48fSopenharmony_ci    SURFACE_DELEGATE_ERROR_UNKNOWN,
4332a6e48fSopenharmony_ci};
4432a6e48fSopenharmony_ci
4532a6e48fSopenharmony_ciclass SurfaceDelegate : public RefBase {
4632a6e48fSopenharmony_cipublic:
4732a6e48fSopenharmony_ci    /*
4832a6e48fSopenharmony_ci     * Define callback for the surface lifecycle.
4932a6e48fSopenharmony_ci     */
5032a6e48fSopenharmony_ci    class ISurfaceCallback : virtual public RefBase {
5132a6e48fSopenharmony_ci        public:
5232a6e48fSopenharmony_ci            /*
5332a6e48fSopenharmony_ci             * Callback when the surface was created.
5432a6e48fSopenharmony_ci             *
5532a6e48fSopenharmony_ci             * @param surface the surface.
5632a6e48fSopenharmony_ci             */
5732a6e48fSopenharmony_ci            virtual void OnSurfaceCreated(const sptr<Surface>& surface) = 0;
5832a6e48fSopenharmony_ci
5932a6e48fSopenharmony_ci            /*
6032a6e48fSopenharmony_ci             * Callback when the surface was changed.
6132a6e48fSopenharmony_ci             *
6232a6e48fSopenharmony_ci             * @param surface the surface.
6332a6e48fSopenharmony_ci             * @param width the surface width.
6432a6e48fSopenharmony_ci             * @param height the surface height.
6532a6e48fSopenharmony_ci             */
6632a6e48fSopenharmony_ci            virtual void OnSurfaceChanged(const sptr<Surface>& surface, int32_t width, int32_t height) = 0;
6732a6e48fSopenharmony_ci
6832a6e48fSopenharmony_ci            /*
6932a6e48fSopenharmony_ci             * Callback when the surface was destroyed.
7032a6e48fSopenharmony_ci             */
7132a6e48fSopenharmony_ci            virtual void OnSurfaceDestroyed() = 0;
7232a6e48fSopenharmony_ci    };
7332a6e48fSopenharmony_ci
7432a6e48fSopenharmony_ci    /*
7532a6e48fSopenharmony_ci     * Add the surface callback.
7632a6e48fSopenharmony_ci     *
7732a6e48fSopenharmony_ci     * @param callback the ISurfaceCallback.
7832a6e48fSopenharmony_ci     */
7932a6e48fSopenharmony_ci    void AddSurfaceCallback(const sptr<ISurfaceCallback>& callback);
8032a6e48fSopenharmony_ci
8132a6e48fSopenharmony_ci    /*
8232a6e48fSopenharmony_ci     * Remove the surface callback.
8332a6e48fSopenharmony_ci     *
8432a6e48fSopenharmony_ci     * @param callback the ISurfaceCallback.
8532a6e48fSopenharmony_ci     */
8632a6e48fSopenharmony_ci    void RemoveSurfaceCallback(const sptr<ISurfaceCallback>& callback);
8732a6e48fSopenharmony_ci
8832a6e48fSopenharmony_ci    /*
8932a6e48fSopenharmony_ci     * Constructor of SurfaceDelegate.
9032a6e48fSopenharmony_ci     *
9132a6e48fSopenharmony_ci     * @param windowId the window id the surface will be on.
9232a6e48fSopenharmony_ci     */
9332a6e48fSopenharmony_ci    SurfaceDelegate(int windowId);
9432a6e48fSopenharmony_ci
9532a6e48fSopenharmony_ci    /*
9632a6e48fSopenharmony_ci     * Destructor of SurfaceDelegate.
9732a6e48fSopenharmony_ci     */
9832a6e48fSopenharmony_ci    ~SurfaceDelegate() = default;
9932a6e48fSopenharmony_ci
10032a6e48fSopenharmony_ci    /*
10132a6e48fSopenharmony_ci     * Create the surface.
10232a6e48fSopenharmony_ci     *
10332a6e48fSopenharmony_ci     * @param isWindowSurface if the surface is a window type surface.
10432a6e48fSopenharmony_ci     * @return the SurfaceDelegateError code.
10532a6e48fSopenharmony_ci     */
10632a6e48fSopenharmony_ci    SurfaceDelegateError CreateSurface(bool isWindowSurface = false);
10732a6e48fSopenharmony_ci
10832a6e48fSopenharmony_ci    /*
10932a6e48fSopenharmony_ci     * Set the boundaries of the area where the surface is located.
11032a6e48fSopenharmony_ci     *
11132a6e48fSopenharmony_ci     * @param left the left of the boundaries.
11232a6e48fSopenharmony_ci     * @param right the right of the boundaries.
11332a6e48fSopenharmony_ci     * @param width the width of the boundaries.
11432a6e48fSopenharmony_ci     * @param height the height of the boundaries.
11532a6e48fSopenharmony_ci     * @return the SurfaceDelegateError code.
11632a6e48fSopenharmony_ci     */
11732a6e48fSopenharmony_ci    SurfaceDelegateError SetBounds(int32_t left, int32_t right, int32_t width, int32_t height);
11832a6e48fSopenharmony_ci
11932a6e48fSopenharmony_ci    /*
12032a6e48fSopenharmony_ci     * Release the surface.
12132a6e48fSopenharmony_ci     *
12232a6e48fSopenharmony_ci     * @return the SurfaceDelegateError code.
12332a6e48fSopenharmony_ci     */
12432a6e48fSopenharmony_ci    SurfaceDelegateError ReleaseSurface();
12532a6e48fSopenharmony_ci
12632a6e48fSopenharmony_ci    /*
12732a6e48fSopenharmony_ci     * Set the surface size.
12832a6e48fSopenharmony_ci     *
12932a6e48fSopenharmony_ci     * @param width the surface width.
13032a6e48fSopenharmony_ci     * @param height the surface height.
13132a6e48fSopenharmony_ci     * @return the SurfaceDelegateError code.
13232a6e48fSopenharmony_ci     */
13332a6e48fSopenharmony_ci    SurfaceDelegateError SetSurfaceSize(uint32_t width, uint32_t height);
13432a6e48fSopenharmony_ci
13532a6e48fSopenharmony_ci    /*
13632a6e48fSopenharmony_ci     * Get the surface.
13732a6e48fSopenharmony_ci     *
13832a6e48fSopenharmony_ci     * @return the surface.
13932a6e48fSopenharmony_ci     */
14032a6e48fSopenharmony_ci    sptr<Surface> GetSurface();
14132a6e48fSopenharmony_ci
14232a6e48fSopenharmony_ci    /*
14332a6e48fSopenharmony_ci     * Get the native window.
14432a6e48fSopenharmony_ci     *
14532a6e48fSopenharmony_ci     * @return the void pointer of the native window.
14632a6e48fSopenharmony_ci     */
14732a6e48fSopenharmony_ci    void* GetNativeWindow();
14832a6e48fSopenharmony_ci
14932a6e48fSopenharmony_ciprivate:
15032a6e48fSopenharmony_ci    NativeSurfaceCallback CreateNativeSurfaceCallback();
15132a6e48fSopenharmony_ci
15232a6e48fSopenharmony_ci    sptr<NativeSurface> GetNativeSurface();
15332a6e48fSopenharmony_ci
15432a6e48fSopenharmony_ci    void OnSurfaceCreated(uint32_t windowId, uint64_t surfaceId);
15532a6e48fSopenharmony_ci    void OnSurfaceChanged(uint32_t windowId, uint64_t surfaceId, int32_t width, int32_t height);
15632a6e48fSopenharmony_ci    void OnSurfaceDestroyed(uint32_t windowId, uint64_t surfaceId);
15732a6e48fSopenharmony_ci
15832a6e48fSopenharmony_ci    void OnNativeSurfaceCreated(void* nativeWindow);
15932a6e48fSopenharmony_ci    void OnNativeSurfaceDestroyed();
16032a6e48fSopenharmony_ci
16132a6e48fSopenharmony_ci    sptr<Surface> surface_ = nullptr;
16232a6e48fSopenharmony_ci
16332a6e48fSopenharmony_ci    std::vector<sptr<ISurfaceCallback>> surfaceCallbacks_;
16432a6e48fSopenharmony_ci    std::recursive_mutex mutex_;
16532a6e48fSopenharmony_ci
16632a6e48fSopenharmony_ci    int windowId_ = 0;
16732a6e48fSopenharmony_ci};
16832a6e48fSopenharmony_ci} // namespace OHOS
16932a6e48fSopenharmony_ci#endif // INTERFACES_INNERKITS_SURFACE_SURFACE_DELEGATE_H