blob: 24e217eefd23b6ccbb7dee30ce5931c83131c892 [file] [log] [blame]
Craig Donnerc7e8dae2017-01-03 10:24:58 -08001/*
2 * Copyright 2017 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/**
18 * @file hardware_buffer.h
19 */
20
21#ifndef ANDROID_HARDWARE_BUFFER_H
22#define ANDROID_HARDWARE_BUFFER_H
23
24#include <inttypes.h>
25
26#include <sys/cdefs.h>
27
28#include <android/rect.h>
29
30__BEGIN_DECLS
31
32/**
33 * Buffer pixel formats.
34 */
35enum {
36 /**
37 * Corresponding formats:
38 * Vulkan: VK_FORMAT_R8G8B8A8_UNORM
39 * OpenGL ES: GL_RGBA8
40 */
41 AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1,
42
43 /**
44 * Corresponding formats:
45 * Vulkan: VK_FORMAT_R8G8B8A8_UNORM
46 * OpenGL ES: GL_RGBA8
47 */
48 AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM = 2,
49
50 /**
51 * Corresponding formats:
52 * Vulkan: VK_FORMAT_R8G8B8_UNORM
53 * OpenGL ES: GL_RGB8
54 */
55 AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM = 3,
56
57 /**
58 * Corresponding formats:
59 * Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16
60 * OpenGL ES: GL_RGB565
61 */
62 AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM = 4,
63
64 /**
65 * Corresponding formats:
66 * Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT
67 * OpenGL ES: GL_RGBA16F
68 */
Craig Donnere54e2382017-01-24 18:16:10 -080069 AHARDWAREBUFFER_FORMAT_R16G16B16A16_SFLOAT = 0x16,
70
71 /**
72 * An opaque binary blob format that must have height 1, with width equal to
73 * the buffer size in bytes.
74 */
75 AHARDWAREBUFFER_FORMAT_BLOB = 0x21,
Craig Donnerc7e8dae2017-01-03 10:24:58 -080076};
77
78enum {
79 /* The buffer will sometimes be read by the CPU */
80 AHARDWAREBUFFER_USAGE0_CPU_READ = 1ULL << 1,
81 /* The buffer will often be read by the CPU*/
82 AHARDWAREBUFFER_USAGE0_CPU_READ_OFTEN = 1ULL << 2 |
83 AHARDWAREBUFFER_USAGE0_CPU_READ,
84 /* The buffer will sometimes be written to by the CPU */
85 AHARDWAREBUFFER_USAGE0_CPU_WRITE = 1ULL << 5,
86 /* The buffer will often be written to by the CPU */
87 AHARDWAREBUFFER_USAGE0_CPU_WRITE_OFTEN = 1ULL << 6 |
88 AHARDWAREBUFFER_USAGE0_CPU_WRITE,
89 /* The buffer will be read from by the GPU */
90 AHARDWAREBUFFER_USAGE0_GPU_SAMPLED_IMAGE = 1ULL << 10,
91 /* The buffer will be written to by the GPU */
92 AHARDWAREBUFFER_USAGE0_GPU_COLOR_OUTPUT = 1ULL << 11,
93 /* The buffer will be read from and written to by the GPU */
94 AHARDWAREBUFFER_USAGE0_GPU_STORAGE_IMAGE =
95 AHARDWAREBUFFER_USAGE0_GPU_SAMPLED_IMAGE |
96 AHARDWAREBUFFER_USAGE0_GPU_COLOR_OUTPUT,
97 /* The buffer will be used as a cubemap texture */
98 AHARDWAREBUFFER_USAGE0_GPU_CUBEMAP = 1ULL << 13,
99 /* The buffer will be used as a shader storage or uniform buffer object*/
100 AHARDWAREBUFFER_USAGE0_GPU_DATA_BUFFER = 1ULL << 14,
101 /* The buffer must not be used outside of a protected hardware path */
102 AHARDWAREBUFFER_USAGE0_PROTECTED_CONTENT = 1ULL << 18,
103 /** The buffer will be used for sensor direct data */
104 AHARDWAREBUFFER_USAGE0_SENSOR_DIRECT_DATA = 1ULL << 29,
105 /* The buffer will be read by a hardware video encoder */
106 AHARDWAREBUFFER_USAGE0_VIDEO_ENCODE = 1ULL << 21,
107};
108
109typedef struct AHardwareBuffer_Desc {
110 uint32_t width;
111 uint32_t height;
112 uint32_t layers;
113 uint64_t usage0; // Combination of AHARDWAREBUFFER_USAGE0_*
114 uint64_t usage1; // Initialize to zero, reserved for future use
115 uint32_t format; // One of AHARDWAREBUFFER_FORMAT_*
116} AHardwareBuffer_Desc;
117
118typedef struct AHardwareBuffer AHardwareBuffer;
119
120/**
121 * Allocates a buffer that backs an AHardwareBuffer using the passed
122 * AHardwareBuffer_Desc.
123 *
124 * Returns NO_ERROR on success, or an error number of the allocation fails for
125 * any reason.
126 */
127int AHardwareBuffer_allocate(const AHardwareBuffer_Desc* desc,
128 AHardwareBuffer** outBuffer);
129/**
130 * Acquire a reference on the given AHardwareBuffer object. This prevents the
131 * object from being deleted until the last reference is removed.
132 */
133void AHardwareBuffer_acquire(AHardwareBuffer* buffer);
134
135/**
136 * Remove a reference that was previously acquired with
137 * AHardwareBuffer_acquire().
138 */
139void AHardwareBuffer_release(AHardwareBuffer* buffer);
140
141/**
142 * Return a description of the AHardwareBuffer in the passed
143 * AHardwareBuffer_Desc struct.
144 */
145void AHardwareBuffer_describe(const AHardwareBuffer* buffer,
146 AHardwareBuffer_Desc* outDesc);
147
148/*
149 * Lock the AHardwareBuffer for reading or writing, depending on the usage flags
150 * passed. This call may block if the hardware needs to finish rendering or if
151 * CPU caches need to be synchronized, or possibly for other implementation-
152 * specific reasons. If fence is not negative, then it specifies a fence file
153 * descriptor that will be signaled when the buffer is locked, otherwise the
154 * caller will block until the buffer is available.
155 *
156 * If rect is not NULL, the caller promises to modify only data in the area
157 * specified by rect. If rect is NULL, the caller may modify the contents of the
158 * entire buffer.
159 *
160 * The content of the buffer outside of the specified rect is NOT modified
161 * by this call.
162 *
163 * The buffer usage may only specify AHARDWAREBUFFER_USAGE0_CPU_*. If set, then
164 * outVirtualAddress is filled with the address of the buffer in virtual memory,
165 * otherwise this function will fail.
166 *
167 * THREADING CONSIDERATIONS:
168 *
169 * It is legal for several different threads to lock a buffer for read access;
170 * none of the threads are blocked.
171 *
172 * Locking a buffer simultaneously for write or read/write is undefined, but
173 * will neither terminate the process nor block the caller; AHardwareBuffer_lock
174 * may return an error or leave the buffer's content into an indeterminate
175 * state.
176 *
177 * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL or if the usage0
178 * flags are not a combination of AHARDWAREBUFFER_USAGE0_CPU_*, or an error
179 * number of the lock fails for any reason.
180 */
181int AHardwareBuffer_lock(AHardwareBuffer* buffer, uint64_t usage0,
182 int32_t fence, const ARect* rect, void** outVirtualAddress);
183
184/*
185 * Unlock the AHardwareBuffer; must be called after all changes to the buffer
186 * are completed by the caller. If fence is not NULL then it will be set to a
187 * file descriptor that is signaled when all pending work on the buffer is
188 * completed. The caller is responsible for closing the fence when it is no
189 * longer needed.
190 *
191 * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error
192 * number of the lock fails for any reason.
193 */
194int AHardwareBuffer_unlock(AHardwareBuffer* buffer, int32_t* fence);
195
196/*
197 * Send the AHardwareBuffer to an AF_UNIX socket.
198 *
199 * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error
200 * number of the lock fails for any reason.
201 */
202int AHardwareBuffer_sendHandleToUnixSocket(const AHardwareBuffer* buffer,
203 int socketFd);
204
205/*
206 * Receive the AHardwareBuffer from an AF_UNIX socket.
207 *
208 * Returns NO_ERROR on success, BAD_VALUE if the buffer is NULL, or an error
209 * number of the lock fails for any reason.
210 */
211int AHardwareBuffer_recvHandleFromUnixSocket(int socketFd,
212 AHardwareBuffer** outBuffer);
213
214// ----------------------------------------------------------------------------
215// Everything below here is part of the public NDK API, but is intended only
216// for use by device-specific graphics drivers.
217struct native_handle;
218const struct native_handle* AHardwareBuffer_getNativeHandle(
219 const AHardwareBuffer* buffer);
220
221__END_DECLS
222
223#endif // ANDROID_HARDWARE_BUFFER_H