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 IMAGE_CODEC_BUFFER_H
17 #define IMAGE_CODEC_BUFFER_H
18 
19 #include "surface_type.h" // foundation/graphic/graphic_surface/interfaces/inner_api/surface/surface_type.h
20 #include "surface_buffer.h" // foundation/graphic/graphic_surface/interfaces/inner_api/surface/surface_buffer.h
21 
22 namespace OHOS::ImagePlugin {
23 enum YuvSemiPlanarArrangement {
24     PLANE_Y = 0,
25     PLANE_U,
26     PLANE_V,
27     PLANE_BUTT
28 };
29 
30 class ImageCodecBuffer {
31 public:
32     static std::shared_ptr<ImageCodecBuffer> CreateDmaBuffer(int fd, int32_t capacity, int32_t stride);
33     static std::shared_ptr<ImageCodecBuffer> CreateSurfaceBuffer(const BufferRequestConfig &config);
34     static std::shared_ptr<ImageCodecBuffer> CreateSurfaceBuffer(sptr<SurfaceBuffer> surface);
35 
36     virtual ~ImageCodecBuffer() = default;
GetBufferCirculateInfo(int64_t & pts,uint32_t & flag,uint32_t & size,uint32_t & offset)37     void GetBufferCirculateInfo(int64_t& pts, uint32_t& flag, uint32_t& size, uint32_t& offset) const
38     {
39         pts = pts_;
40         flag = flag_;
41         size = size_;
42         offset = offset_;
43     }
SetBufferCirculateInfo(int64_t pts,uint32_t flag,uint32_t size,uint32_t offset)44     void SetBufferCirculateInfo(int64_t pts, uint32_t flag, uint32_t size, uint32_t offset)
45     {
46         pts_ = pts;
47         flag_ = flag;
48         size_ = size;
49         offset_ = offset;
50     }
GetBufferFlag()51     uint32_t GetBufferFlag() const { return flag_; }
GetPts()52     int64_t GetPts() const { return pts_; }
GetCapacity()53     int32_t GetCapacity() const { return capacity_; }
GetStride()54     int32_t GetStride() const { return stride_; }
55     virtual bool Init() = 0;
56     virtual int32_t GetFileDescriptor() = 0;
57     virtual sptr<SurfaceBuffer> GetSurfaceBuffer() = 0;
58     virtual uint8_t* GetAddr() = 0;
59 protected:
60     ImageCodecBuffer() = default;
61 protected:
62     int64_t pts_ = 0;
63     uint32_t size_ = 0;
64     uint32_t offset_ = 0;
65     uint32_t flag_; // OMX_BUFFERFLAG_X defined in OMX_Core.h
66     int32_t capacity_ = 0;
67     int32_t stride_ = 0;
68 };
69 
70 class ImageDmaBuffer : public ImageCodecBuffer {
71 public:
72     ImageDmaBuffer(int fd, int32_t capacity, int32_t stride);
73     ~ImageDmaBuffer();
Init()74     bool Init() override { return true; }
GetFileDescriptor()75     int32_t GetFileDescriptor() override { return fd_; }
GetSurfaceBuffer()76     sptr<SurfaceBuffer> GetSurfaceBuffer() override { return nullptr; }
77     uint8_t* GetAddr() override;
78 private:
79     int32_t fd_ = -1;
80     uint8_t* addr_ = nullptr;
81 };
82 
83 class ImageSurfaceBuffer : public ImageCodecBuffer {
84 public:
85     explicit ImageSurfaceBuffer(const BufferRequestConfig &config);
86     explicit ImageSurfaceBuffer(sptr<SurfaceBuffer> surface);
87     ~ImageSurfaceBuffer();
88     bool Init() override;
89     int32_t GetFileDescriptor() override;
90     uint8_t* GetAddr() override;
91     sptr<SurfaceBuffer> GetSurfaceBuffer() override;
92 private:
93     BufferRequestConfig config_{};
94     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
95     uint8_t* addr_ = nullptr;
96 };
97 
98 } // namespace OHOS::ImagePlugin
99 #endif