1 /*
2 * Copyright (C) 2007 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 #define LOG_TAG "GraphicBuffer"
18
19 #include <ui/GraphicBuffer.h>
20
21 #include <cutils/atomic.h>
22
23 #include <grallocusage/GrallocUsageConversion.h>
24
25 #include <ui/Gralloc2.h>
26 #include <ui/GraphicBufferAllocator.h>
27 #include <ui/GraphicBufferMapper.h>
28
29 namespace android {
30
31 // ===========================================================================
32 // Buffer and implementation of ANativeWindowBuffer
33 // ===========================================================================
34
getUniqueId()35 static uint64_t getUniqueId() {
36 static volatile int32_t nextId = 0;
37 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
38 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
39 return id;
40 }
41
from(ANativeWindowBuffer * anwb)42 sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
43 return static_cast<GraphicBuffer *>(anwb);
44 }
45
GraphicBuffer()46 GraphicBuffer::GraphicBuffer()
47 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
48 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
49 {
50 width =
51 height =
52 stride =
53 format =
54 usage_deprecated = 0;
55 usage = 0;
56 layerCount = 0;
57 handle = NULL;
58 }
59
60 // deprecated
GraphicBuffer(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inUsage,std::string requestorName)61 GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
62 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
63 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
64 {
65 }
66
GraphicBuffer(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint64_t usage,std::string requestorName)67 GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
68 PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName)
69 : GraphicBuffer()
70 {
71 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
72 usage, std::move(requestorName));
73 }
74
75 // deprecated
GraphicBuffer(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint32_t inUsage,uint32_t inStride,native_handle_t * inHandle,bool keepOwnership)76 GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
77 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
78 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
79 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
80 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
81 inStride)
82 {
83 }
84
GraphicBuffer(const native_handle_t * handle,HandleWrapMethod method,uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint32_t stride)85 GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
86 HandleWrapMethod method, uint32_t width, uint32_t height,
87 PixelFormat format, uint32_t layerCount,
88 uint64_t usage,
89 uint32_t stride)
90 : GraphicBuffer()
91 {
92 mInitCheck = initWithHandle(handle, method, width, height, format,
93 layerCount, usage, stride);
94 }
95
~GraphicBuffer()96 GraphicBuffer::~GraphicBuffer()
97 {
98 if (handle) {
99 free_handle();
100 }
101 }
102
free_handle()103 void GraphicBuffer::free_handle()
104 {
105 if (mOwner == ownHandle) {
106 mBufferMapper.freeBuffer(handle);
107 } else if (mOwner == ownData) {
108 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
109 allocator.free(handle);
110 }
111 handle = NULL;
112 }
113
initCheck() const114 status_t GraphicBuffer::initCheck() const {
115 return static_cast<status_t>(mInitCheck);
116 }
117
dumpAllocationsToSystemLog()118 void GraphicBuffer::dumpAllocationsToSystemLog()
119 {
120 GraphicBufferAllocator::dumpToSystemLog();
121 }
122
getNativeBuffer() const123 ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
124 {
125 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
126 return static_cast<ANativeWindowBuffer*>(
127 const_cast<GraphicBuffer*>(this));
128 }
129
reallocate(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint64_t inUsage)130 status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
131 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
132 {
133 if (mOwner != ownData)
134 return INVALID_OPERATION;
135
136 if (handle &&
137 static_cast<int>(inWidth) == width &&
138 static_cast<int>(inHeight) == height &&
139 inFormat == format &&
140 inLayerCount == layerCount &&
141 inUsage == usage)
142 return NO_ERROR;
143
144 if (handle) {
145 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
146 allocator.free(handle);
147 handle = 0;
148 }
149 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
150 }
151
needsReallocation(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint64_t inUsage)152 bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
153 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
154 {
155 if (static_cast<int>(inWidth) != width) return true;
156 if (static_cast<int>(inHeight) != height) return true;
157 if (inFormat != format) return true;
158 if (inLayerCount != layerCount) return true;
159 if ((usage & inUsage) != inUsage) return true;
160 return false;
161 }
162
initWithSize(uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint64_t inUsage,std::string requestorName)163 status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
164 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
165 std::string requestorName)
166 {
167 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
168 uint32_t outStride = 0;
169 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
170 inUsage, &handle, &outStride, mId,
171 std::move(requestorName));
172 if (err == NO_ERROR) {
173 width = static_cast<int>(inWidth);
174 height = static_cast<int>(inHeight);
175 format = inFormat;
176 layerCount = inLayerCount;
177 usage = inUsage;
178 usage_deprecated = int(usage);
179 stride = static_cast<int>(outStride);
180 }
181 return err;
182 }
183
initWithHandle(const native_handle_t * handle,HandleWrapMethod method,uint32_t width,uint32_t height,PixelFormat format,uint32_t layerCount,uint64_t usage,uint32_t stride)184 status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
185 HandleWrapMethod method, uint32_t width, uint32_t height,
186 PixelFormat format, uint32_t layerCount, uint64_t usage,
187 uint32_t stride)
188 {
189 ANativeWindowBuffer::width = static_cast<int>(width);
190 ANativeWindowBuffer::height = static_cast<int>(height);
191 ANativeWindowBuffer::stride = static_cast<int>(stride);
192 ANativeWindowBuffer::format = format;
193 ANativeWindowBuffer::usage = usage;
194 ANativeWindowBuffer::usage_deprecated = int(usage);
195
196 ANativeWindowBuffer::layerCount = layerCount;
197
198 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
199
200 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
201 buffer_handle_t importedHandle;
202 status_t err = mBufferMapper.importBuffer(handle, &importedHandle);
203 if (err != NO_ERROR) {
204 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
205
206 return err;
207 }
208
209 if (method == TAKE_UNREGISTERED_HANDLE) {
210 native_handle_close(handle);
211 native_handle_delete(const_cast<native_handle_t*>(handle));
212 }
213
214 handle = importedHandle;
215 }
216
217 ANativeWindowBuffer::handle = handle;
218
219 return NO_ERROR;
220 }
221
lock(uint32_t inUsage,void ** vaddr)222 status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
223 {
224 const Rect lockBounds(width, height);
225 status_t res = lock(inUsage, lockBounds, vaddr);
226 return res;
227 }
228
lock(uint32_t inUsage,const Rect & rect,void ** vaddr)229 status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
230 {
231 if (rect.left < 0 || rect.right > width ||
232 rect.top < 0 || rect.bottom > height) {
233 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
234 rect.left, rect.top, rect.right, rect.bottom,
235 width, height);
236 return BAD_VALUE;
237 }
238 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
239 return res;
240 }
241
lockYCbCr(uint32_t inUsage,android_ycbcr * ycbcr)242 status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
243 {
244 const Rect lockBounds(width, height);
245 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
246 return res;
247 }
248
lockYCbCr(uint32_t inUsage,const Rect & rect,android_ycbcr * ycbcr)249 status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
250 android_ycbcr* ycbcr)
251 {
252 if (rect.left < 0 || rect.right > width ||
253 rect.top < 0 || rect.bottom > height) {
254 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
255 rect.left, rect.top, rect.right, rect.bottom,
256 width, height);
257 return BAD_VALUE;
258 }
259 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
260 return res;
261 }
262
unlock()263 status_t GraphicBuffer::unlock()
264 {
265 status_t res = getBufferMapper().unlock(handle);
266 return res;
267 }
268
lockAsync(uint32_t inUsage,void ** vaddr,int fenceFd)269 status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
270 {
271 const Rect lockBounds(width, height);
272 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
273 return res;
274 }
275
lockAsync(uint32_t inUsage,const Rect & rect,void ** vaddr,int fenceFd)276 status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
277 void** vaddr, int fenceFd)
278 {
279 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
280 }
281
lockAsync(uint64_t inProducerUsage,uint64_t inConsumerUsage,const Rect & rect,void ** vaddr,int fenceFd)282 status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
283 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
284 {
285 if (rect.left < 0 || rect.right > width ||
286 rect.top < 0 || rect.bottom > height) {
287 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
288 rect.left, rect.top, rect.right, rect.bottom,
289 width, height);
290 return BAD_VALUE;
291 }
292 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
293 inConsumerUsage, rect, vaddr, fenceFd);
294 return res;
295 }
296
lockAsyncYCbCr(uint32_t inUsage,android_ycbcr * ycbcr,int fenceFd)297 status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
298 int fenceFd)
299 {
300 const Rect lockBounds(width, height);
301 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
302 return res;
303 }
304
lockAsyncYCbCr(uint32_t inUsage,const Rect & rect,android_ycbcr * ycbcr,int fenceFd)305 status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
306 android_ycbcr* ycbcr, int fenceFd)
307 {
308 if (rect.left < 0 || rect.right > width ||
309 rect.top < 0 || rect.bottom > height) {
310 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
311 rect.left, rect.top, rect.right, rect.bottom,
312 width, height);
313 return BAD_VALUE;
314 }
315 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
316 return res;
317 }
318
unlockAsync(int * fenceFd)319 status_t GraphicBuffer::unlockAsync(int *fenceFd)
320 {
321 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
322 return res;
323 }
324
getFlattenedSize() const325 size_t GraphicBuffer::getFlattenedSize() const {
326 return static_cast<size_t>(13 + (handle ? handle->numInts : 0)) * sizeof(int);
327 }
328
getFdCount() const329 size_t GraphicBuffer::getFdCount() const {
330 return static_cast<size_t>(handle ? handle->numFds : 0);
331 }
332
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const333 status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
334 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
335 if (size < sizeNeeded) return NO_MEMORY;
336
337 size_t fdCountNeeded = GraphicBuffer::getFdCount();
338 if (count < fdCountNeeded) return NO_MEMORY;
339
340 int32_t* buf = static_cast<int32_t*>(buffer);
341 buf[0] = 'GB01';
342 buf[1] = width;
343 buf[2] = height;
344 buf[3] = stride;
345 buf[4] = format;
346 buf[5] = static_cast<int32_t>(layerCount);
347 buf[6] = int(usage); // low 32-bits
348 buf[7] = static_cast<int32_t>(mId >> 32);
349 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
350 buf[9] = static_cast<int32_t>(mGenerationNumber);
351 buf[10] = 0;
352 buf[11] = 0;
353 buf[12] = int(usage >> 32); // high 32-bits
354
355 if (handle) {
356 buf[10] = handle->numFds;
357 buf[11] = handle->numInts;
358 memcpy(fds, handle->data, static_cast<size_t>(handle->numFds) * sizeof(int));
359 memcpy(buf + 13, handle->data + handle->numFds,
360 static_cast<size_t>(handle->numInts) * sizeof(int));
361 }
362
363 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
364 size -= sizeNeeded;
365 if (handle) {
366 fds += handle->numFds;
367 count -= static_cast<size_t>(handle->numFds);
368 }
369
370 return NO_ERROR;
371 }
372
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)373 status_t GraphicBuffer::unflatten(
374 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
375 if (size < 12 * sizeof(int)) {
376 android_errorWriteLog(0x534e4554, "114223584");
377 return NO_MEMORY;
378 }
379
380 int const* buf = static_cast<int const*>(buffer);
381
382 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
383 // see H2BGraphicBufferProducer.cpp
384 uint32_t flattenWordCount = 0;
385 if (buf[0] == 'GB01') {
386 // new version with 64-bits usage bits
387 flattenWordCount = 13;
388 } else if (buf[0] == 'GBFR') {
389 // old version, when usage bits were 32-bits
390 flattenWordCount = 12;
391 } else {
392 return BAD_TYPE;
393 }
394
395 const size_t numFds = static_cast<size_t>(buf[10]);
396 const size_t numInts = static_cast<size_t>(buf[11]);
397
398 // Limit the maxNumber to be relatively small. The number of fds or ints
399 // should not come close to this number, and the number itself was simply
400 // chosen to be high enough to not cause issues and low enough to prevent
401 // overflow problems.
402 const size_t maxNumber = 4096;
403 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
404 width = height = stride = format = usage_deprecated = 0;
405 layerCount = 0;
406 usage = 0;
407 handle = NULL;
408 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
409 return BAD_VALUE;
410 }
411
412 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
413 if (size < sizeNeeded) return NO_MEMORY;
414
415 size_t fdCountNeeded = numFds;
416 if (count < fdCountNeeded) return NO_MEMORY;
417
418 if (handle) {
419 // free previous handle if any
420 free_handle();
421 }
422
423 if (numFds || numInts) {
424 width = buf[1];
425 height = buf[2];
426 stride = buf[3];
427 format = buf[4];
428 layerCount = static_cast<uintptr_t>(buf[5]);
429 usage_deprecated = buf[6];
430 if (flattenWordCount == 13) {
431 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
432 } else {
433 usage = uint64_t(usage_deprecated);
434 }
435 native_handle* h = native_handle_create(
436 static_cast<int>(numFds), static_cast<int>(numInts));
437 if (!h) {
438 width = height = stride = format = usage_deprecated = 0;
439 layerCount = 0;
440 usage = 0;
441 handle = NULL;
442 ALOGE("unflatten: native_handle_create failed");
443 return NO_MEMORY;
444 }
445 memcpy(h->data, fds, numFds * sizeof(int));
446 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
447 handle = h;
448 } else {
449 width = height = stride = format = usage_deprecated = 0;
450 layerCount = 0;
451 usage = 0;
452 handle = NULL;
453 }
454
455 mId = static_cast<uint64_t>(buf[7]) << 32;
456 mId |= static_cast<uint32_t>(buf[8]);
457
458 mGenerationNumber = static_cast<uint32_t>(buf[9]);
459
460 mOwner = ownHandle;
461
462 if (handle != 0) {
463 buffer_handle_t importedHandle;
464 status_t err = mBufferMapper.importBuffer(handle, &importedHandle);
465 if (err != NO_ERROR) {
466 width = height = stride = format = usage_deprecated = 0;
467 layerCount = 0;
468 usage = 0;
469 handle = NULL;
470 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
471 return err;
472 }
473
474 native_handle_close(handle);
475 native_handle_delete(const_cast<native_handle_t*>(handle));
476 handle = importedHandle;
477 }
478
479 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
480 size -= sizeNeeded;
481 fds += numFds;
482 count -= numFds;
483
484 return NO_ERROR;
485 }
486
487 // ---------------------------------------------------------------------------
488
489 }; // namespace android
490