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 SurfaceControl { 39 sptr<Surface> surface = nullptr; 40 BufferRequestConfig requestConfig = {.width = 0, 41 .height = 0, 42 .strideAlignment = SURFACE_STRIDE_ALIGN, 43 .format = 0, 44 .usage = USAGE, 45 .timeout = TIMEOUT}; 46 ScalingMode scalingMode = ScalingMode::SCALING_MODE_SCALE_TO_WINDOW; 47 }; 48 49 class FSurfaceMemory { 50 public: FSurfaceMemory(SurfaceControl * sInfo)51 FSurfaceMemory(SurfaceControl *sInfo) : sInfo_(sInfo) 52 { 53 isAttached = false; 54 } 55 ~FSurfaceMemory(); 56 int32_t AllocSurfaceBuffer(); 57 void ReleaseSurfaceBuffer(); 58 sptr<SurfaceBuffer> GetSurfaceBuffer(); 59 int32_t GetSurfaceBufferStride(); 60 sptr<SyncFence> GetFence(); 61 void UpdateSurfaceBufferScaleMode(); 62 uint8_t *GetBase() const; 63 int32_t GetSize() const; 64 bool isAttached = false; 65 Owner owner = Owner::OWNED_BY_US; 66 67 private: 68 void SetSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer, Owner toChangeOwner); 69 int32_t RequestSurfaceBuffer(); 70 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 71 sptr<SyncFence> fence_ = nullptr; 72 int32_t stride_ = 0; 73 SurfaceControl *sInfo_ = nullptr; 74 }; 75 } // namespace MediaAVCodec 76 } // namespace OHOS 77 #endif 78