1 /*
2 * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3 * This file contains confidential and proprietary information of
4 * OSWare Technology Co., Ltd
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #ifndef ALLOCATOR_H
19 #define ALLOCATOR_H
20 #include <cerrno>
21 #include <cstdint>
22 #include <sys/mman.h>
23 #include <linux/dma-buf.h>
24 #include "securec.h"
25 #include "display_type.h"
26 namespace OHOS {
27 namespace HDI {
28 namespace DISPLAY {
29 template <typename T>
AlignUp(T value,T align)30 T AlignUp(T value, T align)
31 {
32 if (align == 0) {
33 return 0;
34 }
35 return (value + align - 1) / align * align;
36 }
37
38 template <typename T>
DivRoundUp(T numerator,T denominator)39 T DivRoundUp(T numerator, T denominator)
40 {
41 if (denominator == 0) {
42 return 0;
43 }
44 return (numerator + denominator - 1) / denominator;
45 }
46
47 struct BufferInfo {
48 uint32_t width_ = 0;
49 uint32_t height_ = 0;
50 uint32_t widthStride_ = 0;
51 uint32_t heightStride_ = 0;
52 uint32_t bitsPerPixel_ = 0;
53 uint32_t bytesPerPixel_ = 0;
54 uint32_t size_ = 0;
55 uint64_t usage_ = 0;
56 PixelFormat format_ = PIXEL_FMT_BUTT;
57 };
58
59 class Allocator {
60 public:
61 virtual int32_t AllocMem(const AllocInfo &info, BufferHandle **handle);
62 virtual int32_t Allocate(const BufferInfo &bufferInfo, BufferHandle &handle);
63 virtual int32_t Init();
64 virtual int32_t FreeMem(BufferHandle *handle);
65 virtual void *Mmap(BufferHandle &handle);
66 virtual int32_t Unmap(BufferHandle &handle);
67 virtual int32_t InvalidateCache(BufferHandle &handle);
68 virtual int32_t FlushCache(BufferHandle &handle);
~Allocator()69 virtual ~Allocator() {}
70 static constexpr uint32_t BITS_PER_BYTES = 8;
71 static constexpr uint32_t HEIGHT_ALIGN = 2;
72 static constexpr uint32_t WIDTH_ALIGN = 8;
73
74 private:
75 int32_t DmaBufferSync(const BufferHandle &handle, uint64_t flag);
76 uint32_t UpdatePixelInfo(BufferInfo &bufferInfo);
77 int32_t UpdateStrideAndSize(BufferInfo &bufferInfo);
78 int32_t UpdateRGBStrideAndSize(BufferInfo &bufferInfo);
79 int32_t UpdateYuvStrideAndSize(BufferInfo &bufferInfo);
80 int32_t ConvertToBufferInfo(BufferInfo &bufferInfo, const AllocInfo &info);
81 bool IsYuv(PixelFormat format);
82 void DumpBufferHandle(BufferHandle &handle) const;
83 };
84 } // namespace DISPLAY
85 } // namespace HDI
86 } // namespace OHOS
87 #endif // ALLOCATOR_H