1/*
2 * Copyright (c) 2021-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 FRAMEWORKS_SURFACE_INCLUDE_SURFACE_BUFFER_IMPL_H
17#define FRAMEWORKS_SURFACE_INCLUDE_SURFACE_BUFFER_IMPL_H
18
19#include <buffer_extra_data.h>
20#include <buffer_handle_parcel.h>
21#include <buffer_handle_utils.h>
22#include <surface_buffer.h>
23#include "egl_data.h"
24#include "native_buffer.h"
25#include "stdint.h"
26
27struct BufferWrapper {};
28
29namespace OHOS {
30class SurfaceBufferImpl : public SurfaceBuffer {
31public:
32    SurfaceBufferImpl();
33    SurfaceBufferImpl(uint32_t seqNum);
34    virtual ~SurfaceBufferImpl();
35
36    GSError Alloc(const BufferRequestConfig &config) override;
37    GSError Map() override;
38    GSError Unmap() override;
39    GSError FlushCache() override;
40    GSError InvalidateCache() override;
41
42    BufferHandle *GetBufferHandle() const override;
43    int32_t GetWidth() const override;
44    int32_t GetHeight() const override;
45    int32_t GetStride() const override;
46    int32_t GetFormat() const override;
47    uint64_t GetUsage() const override;
48    uint64_t GetPhyAddr() const override;
49    void* GetVirAddr() override;
50    int32_t GetFileDescriptor() const override;
51    uint32_t GetSize() const override;
52
53    GraphicColorGamut GetSurfaceBufferColorGamut() const override;
54    GraphicTransformType GetSurfaceBufferTransform() const override;
55    void SetSurfaceBufferColorGamut(const GraphicColorGamut& colorGamut) override;
56    void SetSurfaceBufferTransform(const GraphicTransformType& transform) override;
57
58    int32_t GetSurfaceBufferWidth() const override;
59    int32_t GetSurfaceBufferHeight() const override;
60    void SetSurfaceBufferWidth(int32_t width) override;
61    void SetSurfaceBufferHeight(int32_t width) override;
62
63    uint32_t GetSeqNum() const override;
64
65    void SetExtraData(sptr<BufferExtraData> bedata) override;
66    sptr<BufferExtraData> GetExtraData() const override;
67
68    void SetBufferHandle(BufferHandle *handle) override;
69    GSError WriteToMessageParcel(MessageParcel &parcel) override;
70    GSError ReadFromMessageParcel(MessageParcel &parcel) override;
71
72    OH_NativeBuffer* SurfaceBufferToNativeBuffer() override;
73
74    static GSError CheckBufferConfig(int32_t width, int32_t height,
75                                     int32_t format, uint64_t usage);
76
77    BufferWrapper GetBufferWrapper() override;
78    void SetBufferWrapper(BufferWrapper wrapper) override;
79
80    // metadata
81    GSError SetMetadata(uint32_t key, const std::vector<uint8_t>& value) override;
82    GSError GetMetadata(uint32_t key, std::vector<uint8_t>& value) override;
83    GSError ListMetadataKeys(std::vector<uint32_t>& keys) override;
84    GSError EraseMetadataKey(uint32_t key) override;
85
86    GSError WriteBufferRequestConfig(MessageParcel &parcel) override;
87    GSError ReadBufferRequestConfig(MessageParcel &parcel) override;
88    BufferRequestConfig GetBufferRequestConfig() const override;
89    void SetBufferRequestConfig(const BufferRequestConfig &config) override;
90    void SetConsumerAttachBufferFlag(bool value) override;
91    bool GetConsumerAttachBufferFlag() override;
92    GSError GetPlanesInfo(void **planesInfo) override;
93
94private:
95    void FreeBufferHandleLocked();
96    bool MetaDataCachedLocked(const uint32_t key, const std::vector<uint8_t>& value);
97    GSError GetImageLayout(void *layout);
98
99    BufferHandle *handle_ = nullptr;
100    uint32_t sequenceNumber_ = UINT32_MAX;
101    sptr<BufferExtraData> bedata_ = nullptr;
102    sptr<EglData> eglData_ = nullptr;
103    GraphicColorGamut surfaceBufferColorGamut_ = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
104    GraphicTransformType transform_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
105    int32_t surfaceBufferWidth_ = 0;
106    int32_t surfaceBufferHeight_ = 0;
107    mutable std::mutex mutex_;
108    OH_NativeBuffer_Planes planesInfo_ = {0, {}};
109    BufferRequestConfig bufferRequestConfig_ = {0, 0, 0, 0, 0, 0};
110    bool isConsumerAttachBufferFlag_ = false;
111    std::map<uint32_t, std::vector<uint8_t>> metaDataCache_;
112};
113} // namespace OHOS
114
115#endif // FRAMEWORKS_SURFACE_INCLUDE_SURFACE_BUFFER_IMPL_H
116