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 #ifndef PRODUCER_SURFACE_DELEGATOR_H
16 #define PRODUCER_SURFACE_DELEGATOR_H
17 
18 #include <map>
19 #include <vector>
20 #include <mutex>
21 #include <unordered_set>
22 
23 #include "transact_surface_delegator_stub.h"
24 
25 namespace OHOS {
26 class ProducerSurfaceDelegator : public TransactSurfaceDelegatorStub {
27 public:
28     ~ProducerSurfaceDelegator();
Create()29     static sptr<ProducerSurfaceDelegator> Create()
30     {
31         return sptr<ProducerSurfaceDelegator>(new ProducerSurfaceDelegator());
32     }
33     GSError DequeueBuffer(int32_t slot, sptr<SurfaceBuffer> buffer);
34     GSError QueueBuffer(int32_t slot, int32_t acquireFence);
35     GSError ReleaseBuffer(const sptr<SurfaceBuffer> &buffer, const sptr<SyncFence> &fence);
36     GSError ClearBufferSlot(int32_t slot);
37     GSError ClearAllBuffers();
38     GSError CancelBuffer(int32_t slot, int32_t fenceFd);
39     GSError DetachBuffer(int32_t slot);
40     int OnSetBufferQueueSize(MessageParcel& data, MessageParcel& reply);
41     int OnSetDataspace(MessageParcel& data, MessageParcel& reply);
42     int OnDequeueBuffer(MessageParcel &data, MessageParcel &reply);
43     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
44 
45     static void SetDisplayRotation(int32_t rotation);
46 
47 private:
48     std::map<int32_t, std::vector<sptr<SurfaceBuffer>>> map_;
49     std::vector<sptr<SurfaceBuffer>> pendingReleaseBuffer_;
50     std::unordered_set<int> dequeueFailedSet_;
51     std::mutex dequeueFailedSetMutex_;
52     std::mutex mapMutex_;
53     std::mutex mstate_;
54     uint32_t mTransform_ = 0;
55     GraphicTransformType mLastTransform_ = GraphicTransformType::GRAPHIC_ROTATE_BUTT;
56     uint32_t mAncoDataspace = 0;
57 
58     static std::atomic<int32_t> mDisplayRotation_;
59 
60     void AddBufferLocked(const sptr<SurfaceBuffer>& buffer, int32_t slot);
61     sptr<SurfaceBuffer> GetBufferLocked(int32_t slot);
62     int32_t GetSlotLocked(const sptr<SurfaceBuffer>& buffer);
63     GSError RetryFlushBuffer(sptr<SurfaceBuffer>& buffer, int32_t fence, BufferFlushConfig& config);
64     bool HasSlotInSet(int32_t slot);
65     void InsertSlotIntoSet(int32_t slot);
66     void EraseSlotFromSet(int32_t slot);
67     void UpdateBufferTransform();
68     GraphicTransformType ConvertTransformToHmos(uint32_t transform);
69 };
70 } // namespace OHOS
71 #endif // PRODUCER_SURFACE_DELEGATOR_H
72