blob: c59d327210c15a32c4306b9dd2dd776e5c2a0745 [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>
24#include <system/window.h>
25#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:
42 Mapper();
43
44 // this will be removed and Mapper will be always valid
45 bool valid() const { return (mMapper != nullptr); }
46
47 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
60 // The ownership of acquireFence is always transferred to the callee, even
61 // on errors.
62 Error lock(buffer_handle_t bufferHandle, uint64_t usage,
63 const IMapper::Rect& accessRegion,
64 int acquireFence, void** outData) const;
65
66 // 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, YCbCrLayout* outLayout) const;
71
72 // unlock returns a fence sync object (or -1) and the fence sync object is
73 // owned by the caller
74 int unlock(buffer_handle_t bufferHandle) const;
75
76private:
77 sp<IMapper> mMapper;
78};
79
80// A wrapper to IAllocator
81class Allocator {
82public:
83 // An allocator relies on a mapper, and that mapper must be alive at all
84 // time.
85 Allocator(const Mapper& mapper);
86
87 // this will be removed and Allocator will be always valid
88 bool valid() const { return (mAllocator != nullptr); }
89
90 std::string dumpDebugInfo() const;
91
92 /*
93 * The returned buffers are already imported and must not be imported
94 * again. outBufferHandles must point to a space that can contain at
95 * least "count" buffer_handle_t.
96 */
97 Error allocate(BufferDescriptor descriptor, uint32_t count,
98 uint32_t* outStride, buffer_handle_t* outBufferHandles) const;
99
100 Error allocate(BufferDescriptor descriptor,
101 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
102 {
103 return allocate(descriptor, 1, outStride, outBufferHandle);
104 }
105
106 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo, uint32_t count,
107 uint32_t* outStride, buffer_handle_t* outBufferHandles) const
108 {
109 BufferDescriptor descriptor;
110 Error error = mMapper.createDescriptor(descriptorInfo, &descriptor);
111 if (error == Error::NONE) {
112 error = allocate(descriptor, count, outStride, outBufferHandles);
113 }
114 return error;
115 }
116
117 Error allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
118 uint32_t* outStride, buffer_handle_t* outBufferHandle) const
119 {
120 return allocate(descriptorInfo, 1, outStride, outBufferHandle);
121 }
122
123private:
124 const Mapper& mMapper;
125 sp<IAllocator> mAllocator;
126};
127
128} // namespace Gralloc2
129
130} // namespace android
131
132#endif // ANDROID_UI_GRALLOC2_H