1 /* 2 * Copyright (C) 2023 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 AV_CODEC_FSURFACE_MEMORY_H 17 #define AV_CODEC_FSURFACE_MEMORY_H 18 19 #include "refbase.h" 20 #include "surface.h" 21 #include "sync_fence.h" 22 23 namespace OHOS { 24 namespace MediaAVCodec { 25 namespace { 26 constexpr uint64_t USAGE = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA; 27 constexpr int32_t SURFACE_STRIDE_ALIGN = 8; 28 constexpr int32_t TIMEOUT = 0; 29 } // namespace 30 31 enum class Owner { 32 OWNED_BY_US, 33 OWNED_BY_CODEC, 34 OWNED_BY_USER, 35 OWNED_BY_SURFACE, 36 }; 37 38 struct CallerInfo { 39 int32_t pid = -1; 40 std::string instanceId = ""; 41 std::string processName = ""; 42 std::string_view mimeType = ""; 43 bool calledByAvcodec = true; 44 }; 45 46 struct SurfaceControl { 47 sptr<Surface> surface = nullptr; 48 BufferRequestConfig requestConfig = {.width = 0, 49 .height = 0, 50 .strideAlignment = SURFACE_STRIDE_ALIGN, 51 .format = 0, 52 .usage = USAGE, 53 .timeout = TIMEOUT}; 54 int32_t scalingMode = -1; 55 }; 56 57 class FSurfaceMemory { 58 public: FSurfaceMemory(SurfaceControl * sInfo,CallerInfo & decInfo)59 FSurfaceMemory(SurfaceControl *sInfo, CallerInfo &decInfo) : decInfo_(decInfo), sInfo_(sInfo) 60 { 61 isAttached = false; 62 } 63 ~FSurfaceMemory(); 64 int32_t AllocSurfaceBuffer(int32_t width, int32_t height); 65 void ReleaseSurfaceBuffer(); 66 sptr<SurfaceBuffer> GetSurfaceBuffer(); 67 int32_t GetSurfaceBufferStride(); 68 sptr<SyncFence> GetFence(); 69 uint8_t *GetBase() const; 70 int32_t GetSize() const; 71 std::atomic<bool> isAttached = false; 72 std::atomic<Owner> owner = Owner::OWNED_BY_US; 73 74 private: 75 void SetSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer, Owner toChangeOwner); 76 int32_t RequestSurfaceBuffer(); 77 void SetCallerToBuffer(int32_t w, int32_t h); 78 CallerInfo decInfo_; 79 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 80 sptr<SyncFence> fence_ = nullptr; 81 int32_t stride_ = 0; 82 SurfaceControl *sInfo_ = nullptr; 83 }; 84 } // namespace MediaAVCodec 85 } // namespace OHOS 86 #endif 87