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