1 /*
2  * Copyright (c) 2024 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 ROSEN_RENDER_SERVICE_BASE_ISURFACE_BUFFER_CALLBACK_H
17 #define ROSEN_RENDER_SERVICE_BASE_ISURFACE_BUFFER_CALLBACK_H
18 
19 #include <vector>
20 #include <iremote_broker.h>
21 
22 #ifdef ROSEN_OHOS
23 #include "sync_fence.h"
24 #endif
25 #include "common/rs_common_def.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 struct FinishCallbackRet {
30     uint64_t uid;
31     std::vector<uint32_t> surfaceBufferIds;
32     std::vector<uint8_t> isRenderedFlags;
33 #ifdef ROSEN_OHOS
34     std::vector<sptr<SyncFence>> releaseFences;
35 #endif
36     bool isUniRender;
37 };
38 
39 struct AfterAcquireBufferRet {
40     uint64_t uid;
41     bool isUniRender;
42 };
43 
44 class RSISurfaceBufferCallback : public IRemoteBroker {
45 public:
46     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.rosen.SurfaceBufferListener");
47 
48     RSISurfaceBufferCallback() = default;
49     virtual ~RSISurfaceBufferCallback() noexcept = default;
50 
51     // Uid and BufferQueue are a one-to-one mapping relationship. This API is used
52     // when an application has multiple XComponent components, and it notifies ArkUI
53     // which BufferQueue's Buffer can be released after it has been consumed.
54     virtual void OnFinish(const FinishCallbackRet& ret) = 0;
55 
56     // We send a callback to Arkui after Acquire-Buffer succeeds.
57     virtual void OnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) = 0;
58 };
59 
60 struct DefaultSurfaceBufferCallbackFuncs {
61     std::function<void(const FinishCallbackRet&)> OnFinish;
62     std::function<void(const AfterAcquireBufferRet&)> OnAfterAcquireBuffer;
63 };
64 
65 class RSB_EXPORT RSDefaultSurfaceBufferCallback : public RSISurfaceBufferCallback {
66 public:
67     RSDefaultSurfaceBufferCallback(DefaultSurfaceBufferCallbackFuncs funcs);
68     ~RSDefaultSurfaceBufferCallback() noexcept override = default;
69 
70     void OnFinish(const FinishCallbackRet& ret) override;
71     void OnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) override;
72     sptr<IRemoteObject> AsObject() override;
73 private:
74     std::function<void(const FinishCallbackRet&)> finishCallback_;
75     std::function<void(const AfterAcquireBufferRet&)> afterAcquireBufferCallback_;
76 };
77 } // namespace Rosen
78 } // namespace OHOS
79 
80 #endif // ROSEN_RENDER_SERVICE_BASE_ISURFACE_BUFFER_CALLBACK_H
81