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