blob: 69c35f7ddaaec9a94df50cf4d834329f5def9968 [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>
23#include <android/hardware/graphics/mapper/2.0/IMapper.h>
Chia-I Wudbbe33b2017-09-27 15:22:21 -070024#include <android/hardware/graphics/mapper/2.1/IMapper.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070025#include <utils/StrongPointer.h>
26
27namespace android {
28
29namespace Gralloc2 {
30
31using hardware::graphics::allocator::V2_0::IAllocator;
32using hardware::graphics::common::V1_0::BufferUsage;
33using hardware::graphics::common::V1_0::PixelFormat;
34using hardware::graphics::mapper::V2_0::BufferDescriptor;
35using hardware::graphics::mapper::V2_0::Error;
36using hardware::graphics::mapper::V2_0::IMapper;
37using hardware::graphics::mapper::V2_0::YCbCrLayout;
38
39// A wrapper to IMapper
40class Mapper {
41public:
Jesse Hall5dac7812017-07-06 14:02:29 -070042 static void preload();
43
Chia-I Wu5bac7f32017-04-06 12:34:32 -070044 Mapper();
45
Chia-I Wu5bac7f32017-04-06 12:34:32 -070046 Error createDescriptor(
47 const IMapper::BufferDescriptorInfo& descriptorInfo,
48 BufferDescriptor* outDescriptor) const;
49
50 // Import a buffer that is from another HAL, another process, or is
51 // cloned.
52 //
53 // The returned handle must be freed with freeBuffer.
54 Error importBuffer(const hardware::hidl_handle& rawHandle,
55 buffer_handle_t* outBufferHandle) const;
56
57 void freeBuffer(buffer_handle_t bufferHandle) const;
58
Chia-I Wudbbe33b2017-09-27 15:22:21 -070059 Error validateBufferSize(buffer_handle_t bufferHandle,
60 const IMapper::BufferDescriptorInfo& descriptorInfo,
61 uint32_t stride) const;
62
63 void getTransportSize(buffer_handle_t bufferHandle,
64 uint32_t* outNumFds, uint32_t* outNumInts) const;
65
Chia-I Wu5bac7f32017-04-06 12:34:32 -070066 // The ownership of acquireFence is always transferred to the callee, even
67 // on errors.
68 Error lock(buffer_handle_t bufferHandle, uint64_t usage,
69 const IMapper::Rect& accessRegion,
70 int acquireFence, void** outData) const;
71
72 // The ownership of acquireFence is always transferred to the callee, even
73 // on errors.
74 Error lock(buffer_handle_t bufferHandle, uint64_t usage,
75 const IMapper::Rect& accessRegion,
76 int acquireFence, YCbCrLayout* outLayout) const;
77
78 // unlock returns a fence sync object (or -1) and the fence sync object is
79 // owned by the caller
80 int unlock(buffer_handle_t bufferHandle) const;
81
82private:
83 sp<IMapper> mMapper;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070084 sp<hardware::graphics::mapper::V2_1::IMapper> mMapperV2_1;
Chia-I Wu5bac7f32017-04-06 12:34:32 -070085};
86
87// A wrapper to IAllocator
88class Allocator {
89public:
90 // An allocator relies on a mapper, and that mapper must be alive at all
91 // time.
92 Allocator(const Mapper& mapper);
93
Chia-I Wu5bac7f32017-04-06 12:34:32 -070094 std::string dumpDebugInfo() const;
95
96 /*
97 * The returned buffers are already imported and must not be imported
98 * again. outBufferHandles must point to a space that can contain at
99 * least "count" buffer_handle_t.
100 */
101 Error allocate(BufferDescriptor descriptor, uint32_t count,
102 uint32_t* outStride, buffer_handle_t* outBufferHandles) const;
103
104 Error allocate(BufferDescriptor descriptor,
105 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
106 {
107 return allocate(descriptor, 1, outStride, outBufferHandle);
108 }
109
110 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count,
111 uint32_t* outStride, buffer_handle_t* outBufferHandles) const
112 {
113 BufferDescriptor descriptor;
114 Error error = mMapper.createDescriptor(descriptorInfo, &descriptor);
115 if (error == Error::NONE) {
116 error = allocate(descriptor, count, outStride, outBufferHandles);
117 }
118 return error;
119 }
120
121 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
122 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
123 {
124 return allocate(descriptorInfo, 1, outStride, outBufferHandle);
125 }
126
127private:
128 const Mapper& mMapper;
129 sp<IAllocator> mAllocator;
130};
131
132} // namespace Gralloc2
133
134} // namespace android
135
136#endif // ANDROID_UI_GRALLOC2_H