• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef ALLOCATOR_H
16 #define ALLOCATOR_H
17 #include <cerrno>
18 #include <cstdint>
19 #include <sys/mman.h>
20 #include <linux/dma-buf.h>
21 #include "securec.h"
22 #include "display_type.h"
23 namespace OHOS {
24 namespace HDI {
25 namespace DISPLAY {
26 template <typename T>
AlignUp(T value,T align)27 T AlignUp(T value, T align)
28 {
29     if (align == 0) {
30         return 0;
31     }
32     return (value + align - 1) / align * align;
33 }
34 
35 template <typename T>
DivRoundUp(T numerator,T denominator)36 T DivRoundUp(T numerator, T denominator)
37 {
38     if (denominator == 0) {
39         return 0;
40     }
41     return (numerator + denominator - 1) / denominator;
42 }
43 
44 struct BufferInfo {
45     uint32_t width_ = 0;
46     uint32_t height_ = 0;
47     uint32_t widthStride_ = 0;
48     uint32_t heightStride_ = 0;
49     uint32_t bitsPerPixel_ = 0;
50     uint32_t bytesPerPixel_ = 0;
51     uint32_t size_ = 0;
52     uint64_t usage_ = 0;
53     PixelFormat format_ = PIXEL_FMT_BUTT;
54 };
55 
56 class Allocator {
57 public:
58     virtual int32_t AllocMem(const AllocInfo &info, BufferHandle **handle);
59     virtual int32_t Allocate(const BufferInfo &bufferInfo, BufferHandle &handle);
60     virtual int32_t Init();
61     virtual int32_t FreeMem(BufferHandle *handle);
62     virtual void *Mmap(BufferHandle &handle);
63     virtual int32_t Unmap(BufferHandle &handle);
64     virtual int32_t InvalidateCache(BufferHandle &handle);
65     virtual int32_t FlushCache(BufferHandle &handle);
~Allocator()66     virtual ~Allocator() {}
67     static constexpr uint32_t BITS_PER_BYTES = 8;
68     static constexpr uint32_t HEIGHT_ALIGN = 2;
69     static constexpr uint32_t WIDTH_ALIGN = 8;
70 
71 private:
72     int32_t DmaBufferSync(const BufferHandle &handle, uint64_t flag);
73     uint32_t UpdatePixelInfo(BufferInfo &bufferInfo);
74     int32_t UpdateStrideAndSize(BufferInfo &bufferInfo);
75     int32_t UpdateRGBStrideAndSize(BufferInfo &bufferInfo);
76     int32_t UpdateYuvStrideAndSize(BufferInfo &bufferInfo);
77     int32_t ConvertToBufferInfo(BufferInfo &bufferInfo, const AllocInfo &info);
78     bool IsYuv(PixelFormat format);
79     void DumpBufferHandle(BufferHandle &handle) const;
80 };
81 } // namespace DISPLAY
82 } // namespace HDI
83 } // namespace OHOS
84 #endif // ALLOCATOR_H