blob: 5a8dbda5d37710ad278059773c5870d22a027b6e [file] [log] [blame]
Chia-I Wu5bac7f32017-04-06 12:34:32 -07001/*
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>
Craig Donnere6ecb922017-12-27 14:59:29 -080023#include <android/hardware/graphics/common/1.1/types.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070024#include <android/hardware/graphics/mapper/2.0/IMapper.h>
Chia-I Wudbbe33b2017-09-27 15:22:21 -070025#include <android/hardware/graphics/mapper/2.1/IMapper.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070026#include <utils/StrongPointer.h>
27
28namespace android {
29
30namespace Gralloc2 {
31
32using hardware::graphics::allocator::V2_0::IAllocator;
Craig Donnere6ecb922017-12-27 14:59:29 -080033using hardware::graphics::common::V1_1::BufferUsage;
Chia-I Wu4f55f162018-01-16 21:58:18 -080034using hardware::graphics::common::V1_1::PixelFormat;
35using hardware::graphics::mapper::V2_1::IMapper;
Chia-I Wu5bac7f32017-04-06 12:34:32 -070036using hardware::graphics::mapper::V2_0::BufferDescriptor;
37using hardware::graphics::mapper::V2_0::Error;
Chia-I Wu5bac7f32017-04-06 12:34:32 -070038using hardware::graphics::mapper::V2_0::YCbCrLayout;
39
40// A wrapper to IMapper
41class Mapper {
42public:
Jesse Hall5dac7812017-07-06 14:02:29 -070043 static void preload();
44
Chia-I Wu5bac7f32017-04-06 12:34:32 -070045 Mapper();
46
Chia-I Wu5bac7f32017-04-06 12:34:32 -070047 Error createDescriptor(
48 const IMapper::BufferDescriptorInfo& descriptorInfo,
49 BufferDescriptor* outDescriptor) const;
50
51 // Import a buffer that is from another HAL, another process, or is
52 // cloned.
53 //
54 // The returned handle must be freed with freeBuffer.
55 Error importBuffer(const hardware::hidl_handle& rawHandle,
56 buffer_handle_t* outBufferHandle) const;
57
58 void freeBuffer(buffer_handle_t bufferHandle) const;
59
Chia-I Wudbbe33b2017-09-27 15:22:21 -070060 Error validateBufferSize(buffer_handle_t bufferHandle,
61 const IMapper::BufferDescriptorInfo& descriptorInfo,
62 uint32_t stride) const;
63
64 void getTransportSize(buffer_handle_t bufferHandle,
65 uint32_t* outNumFds, uint32_t* outNumInts) const;
66
Chia-I Wu5bac7f32017-04-06 12:34:32 -070067 // The ownership of acquireFence is always transferred to the callee, even
68 // on errors.
69 Error lock(buffer_handle_t bufferHandle, uint64_t usage,
70 const IMapper::Rect& accessRegion,
71 int acquireFence, void** outData) const;
72
73 // The ownership of acquireFence is always transferred to the callee, even
74 // on errors.
75 Error lock(buffer_handle_t bufferHandle, uint64_t usage,
76 const IMapper::Rect& accessRegion,
77 int acquireFence, YCbCrLayout* outLayout) const;
78
79 // unlock returns a fence sync object (or -1) and the fence sync object is
80 // owned by the caller
81 int unlock(buffer_handle_t bufferHandle) const;
82
83private:
Craig Donnere6ecb922017-12-27 14:59:29 -080084 // Determines whether the passed info is compatible with the mapper.
85 Error validateBufferDescriptorInfo(
86 const IMapper::BufferDescriptorInfo& descriptorInfo) const;
87
Chia-I Wu4f55f162018-01-16 21:58:18 -080088 sp<hardware::graphics::mapper::V2_0::IMapper> mMapper;
89 sp<IMapper> mMapperV2_1;
Chia-I Wu5bac7f32017-04-06 12:34:32 -070090};
91
92// A wrapper to IAllocator
93class Allocator {
94public:
95 // An allocator relies on a mapper, and that mapper must be alive at all
96 // time.
97 Allocator(const Mapper& mapper);
98
Chia-I Wu5bac7f32017-04-06 12:34:32 -070099 std::string dumpDebugInfo() const;
100
101 /*
102 * The returned buffers are already imported and must not be imported
103 * again. outBufferHandles must point to a space that can contain at
104 * least "count" buffer_handle_t.
105 */
106 Error allocate(BufferDescriptor descriptor, uint32_t count,
107 uint32_t* outStride, buffer_handle_t* outBufferHandles) const;
108
109 Error allocate(BufferDescriptor descriptor,
110 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
111 {
112 return allocate(descriptor, 1, outStride, outBufferHandle);
113 }
114
115 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count,
116 uint32_t* outStride, buffer_handle_t* outBufferHandles) const
117 {
118 BufferDescriptor descriptor;
119 Error error = mMapper.createDescriptor(descriptorInfo, &descriptor);
120 if (error == Error::NONE) {
121 error = allocate(descriptor, count, outStride, outBufferHandles);
122 }
123 return error;
124 }
125
126 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
127 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
128 {
129 return allocate(descriptorInfo, 1, outStride, outBufferHandle);
130 }
131
132private:
133 const Mapper& mMapper;
134 sp<IAllocator> mAllocator;
135};
136
137} // namespace Gralloc2
138
139} // namespace android
140
141#endif // ANDROID_UI_GRALLOC2_H