1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_UI_GRALLOC2_H
18 #define ANDROID_UI_GRALLOC2_H
19 
20 #include <string>
21 
22 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
23 #include <android/hardware/graphics/common/1.1/types.h>
24 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
25 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
26 #include <ui/Gralloc.h>
27 #include <ui/PixelFormat.h>
28 #include <ui/Rect.h>
29 #include <utils/StrongPointer.h>
30 
31 namespace android {
32 
33 class Gralloc2Mapper : public GrallocMapper {
34 public:
35     static void preload();
36 
37     Gralloc2Mapper();
38 
39     bool isLoaded() const override;
40 
41     status_t createDescriptor(void* bufferDescriptorInfo, void* outBufferDescriptor) const override;
42 
43     status_t importBuffer(const hardware::hidl_handle& rawHandle,
44                           buffer_handle_t* outBufferHandle) const override;
45 
46     void freeBuffer(buffer_handle_t bufferHandle) const override;
47 
48     status_t validateBufferSize(buffer_handle_t bufferHandle, uint32_t width, uint32_t height,
49                                 android::PixelFormat format, uint32_t layerCount, uint64_t usage,
50                                 uint32_t stride) const override;
51 
52     void getTransportSize(buffer_handle_t bufferHandle, uint32_t* outNumFds,
53                           uint32_t* outNumInts) const override;
54 
55     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
56                   int acquireFence, void** outData, int32_t* outBytesPerPixel,
57                   int32_t* outBytesPerStride) const override;
58 
59     status_t lock(buffer_handle_t bufferHandle, uint64_t usage, const Rect& bounds,
60                   int acquireFence, android_ycbcr* ycbcr) const override;
61 
62     int unlock(buffer_handle_t bufferHandle) const override;
63 
64     status_t isSupported(uint32_t width, uint32_t height, android::PixelFormat format,
65                          uint32_t layerCount, uint64_t usage, bool* outSupported) const override;
66 
67 private:
68     // Determines whether the passed info is compatible with the mapper.
69     status_t validateBufferDescriptorInfo(
70             hardware::graphics::mapper::V2_1::IMapper::BufferDescriptorInfo* descriptorInfo) const;
71 
72     sp<hardware::graphics::mapper::V2_0::IMapper> mMapper;
73     sp<hardware::graphics::mapper::V2_1::IMapper> mMapperV2_1;
74 };
75 
76 class Gralloc2Allocator : public GrallocAllocator {
77 public:
78     // An allocator relies on a mapper, and that mapper must be alive at all
79     // time.
80     Gralloc2Allocator(const Gralloc2Mapper& mapper);
81 
82     bool isLoaded() const override;
83 
84     std::string dumpDebugInfo() const override;
85 
86     status_t allocate(uint32_t width, uint32_t height, PixelFormat format, uint32_t layerCount,
87                       uint64_t usage, uint32_t bufferCount, uint32_t* outStride,
88                       buffer_handle_t* outBufferHandles) const override;
89 
90 private:
91     const Gralloc2Mapper& mMapper;
92     sp<hardware::graphics::allocator::V2_0::IAllocator> mAllocator;
93 };
94 
95 } // namespace android
96 
97 #endif // ANDROID_UI_GRALLOC2_H
98