blob: 41ae2531cd9cb8a6617a3162166725a436968e0b [file] [log] [blame]
Mathias Agopian3330b202009-10-05 17:07:12 -07001/*
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
Mathias Agopian98e71dd2010-02-11 17:30:52 -080017#define LOG_TAG "GraphicBuffer"
18
Mathias Agopian3330b202009-10-05 17:07:12 -070019#include <ui/GraphicBuffer.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080020
21#include <cutils/atomic.h>
22
Jesse Hall79927812017-03-23 11:03:23 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070025#ifndef LIBUI_IN_VNDK
26#include <ui/BufferHubBuffer.h>
27#endif // LIBUI_IN_VNDK
28
Chia-I Wu5bac7f32017-04-06 12:34:32 -070029#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070030#include <ui/GraphicBufferAllocator.h>
31#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070032
Mathias Agopian3330b202009-10-05 17:07:12 -070033namespace android {
34
35// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070036// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070037// ===========================================================================
38
Dan Stozab1363d32014-03-28 15:10:52 -070039static uint64_t getUniqueId() {
40 static volatile int32_t nextId = 0;
41 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
42 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
43 return id;
44}
45
Mathias Agopianf543e5a2017-04-03 17:16:41 -070046sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
47 return static_cast<GraphicBuffer *>(anwb);
48}
49
Mathias Agopian3330b202009-10-05 17:07:12 -070050GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070051 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070052 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070053{
Dan Stoza01049c82014-11-11 10:32:31 -080054 width =
55 height =
56 stride =
57 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070058 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070059 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080060 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070061 handle = NULL;
62}
63
Chia-I Wub42f1712017-03-21 13:15:39 -070064// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080065GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070066 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070067 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070068{
Mathias Agopian3330b202009-10-05 17:07:12 -070069}
70
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080071GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
72 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
73 : GraphicBuffer() {
74 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
75 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070076}
77
Chia-I Wub42f1712017-03-21 13:15:39 -070078// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070079GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
80 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
81 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070082 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070083 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070084 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070085{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070086}
87
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080088GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
89 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
90 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
91 : GraphicBuffer() {
92 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
93 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -070094}
95
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070096#ifndef LIBUI_IN_VNDK
97GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() {
98 if (buffer == nullptr) {
99 mInitCheck = BAD_VALUE;
100 return;
101 }
102
103 mInitCheck = initWithHandle(buffer->DuplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
104 buffer->desc().width, buffer->desc().height,
105 static_cast<PixelFormat>(buffer->desc().format),
106 buffer->desc().layers, buffer->desc().usage, buffer->desc().stride);
Jiwen 'Steve' Cai29160fb2018-12-13 12:03:28 -0800107 mBufferId = buffer->id();
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700108 mBufferHubBuffer = std::move(buffer);
109}
110#endif // LIBUI_IN_VNDK
111
Mathias Agopian3330b202009-10-05 17:07:12 -0700112GraphicBuffer::~GraphicBuffer()
113{
114 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800115 free_handle();
116 }
117}
118
119void GraphicBuffer::free_handle()
120{
121 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700122 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800123 } else if (mOwner == ownData) {
124 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
125 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700126 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700127 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700128}
129
130status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800131 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700132}
133
Mathias Agopian678bdd62010-12-03 17:33:09 -0800134void GraphicBuffer::dumpAllocationsToSystemLog()
135{
136 GraphicBufferAllocator::dumpToSystemLog();
137}
138
Iliyan Malchev697526b2011-05-01 11:33:26 -0700139ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700140{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700141 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700142 const_cast<GraphicBuffer*>(this));
143}
144
Dan Stozad3182402014-11-17 12:03:59 -0800145status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700146 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700147{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700148 if (mOwner != ownData)
149 return INVALID_OPERATION;
150
Dan Stozad3182402014-11-17 12:03:59 -0800151 if (handle &&
152 static_cast<int>(inWidth) == width &&
153 static_cast<int>(inHeight) == height &&
154 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700155 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700156 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700157 return NO_ERROR;
158
Mathias Agopian3330b202009-10-05 17:07:12 -0700159 if (handle) {
160 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
161 allocator.free(handle);
162 handle = 0;
163 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700164 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700165}
166
Dan Stoza9de72932015-04-16 17:28:43 -0700167bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700168 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700169{
170 if (static_cast<int>(inWidth) != width) return true;
171 if (static_cast<int>(inHeight) != height) return true;
172 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700173 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700174 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700175 return false;
176}
177
Chia-I Wub42f1712017-03-21 13:15:39 -0700178status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700179 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
180 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700181{
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800183 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700184 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700185 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800186 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700187 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700188 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
189
Dan Stozad3182402014-11-17 12:03:59 -0800190 width = static_cast<int>(inWidth);
191 height = static_cast<int>(inHeight);
192 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700193 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700194 usage = inUsage;
195 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800196 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700197 }
198 return err;
199}
200
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800201status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
202 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
203 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
204 ANativeWindowBuffer::width = static_cast<int>(inWidth);
205 ANativeWindowBuffer::height = static_cast<int>(inHeight);
206 ANativeWindowBuffer::stride = static_cast<int>(inStride);
207 ANativeWindowBuffer::format = inFormat;
208 ANativeWindowBuffer::usage = inUsage;
209 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700210
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800211 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700212
213 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
214
Chia-I Wucb8405e2017-04-17 15:20:19 -0700215 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
216 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800217 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
218 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700219 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700220 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700221
222 return err;
223 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700224
225 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800226 native_handle_close(inHandle);
227 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700228 }
229
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800230 inHandle = importedHandle;
231 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700232 }
233
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800234 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700235
Chia-I Wub42f1712017-03-21 13:15:39 -0700236 return NO_ERROR;
237}
238
Valerie Hau250c6542019-01-31 14:23:43 -0800239status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
240 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700241 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800242 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700243 return res;
244}
245
Valerie Hau250c6542019-01-31 14:23:43 -0800246status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
247 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800248 if (rect.left < 0 || rect.right > width ||
249 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800251 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800252 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 return BAD_VALUE;
254 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800255
Valerie Hau250c6542019-01-31 14:23:43 -0800256 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
257 outBytesPerStride);
258
Mathias Agopian3330b202009-10-05 17:07:12 -0700259 return res;
260}
261
Dan Stozad3182402014-11-17 12:03:59 -0800262status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700263{
264 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800265 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700266 return res;
267}
268
Dan Stozad3182402014-11-17 12:03:59 -0800269status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
270 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700271{
Dan Stozad3182402014-11-17 12:03:59 -0800272 if (rect.left < 0 || rect.right > width ||
273 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700274 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
275 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800276 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700277 return BAD_VALUE;
278 }
Dan Stozad3182402014-11-17 12:03:59 -0800279 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700280 return res;
281}
282
Mathias Agopian3330b202009-10-05 17:07:12 -0700283status_t GraphicBuffer::unlock()
284{
285 status_t res = getBufferMapper().unlock(handle);
286 return res;
287}
288
Valerie Haub94adfd2019-02-07 14:25:12 -0800289status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
290 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300291 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800292 status_t res =
293 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300294 return res;
295}
296
Valerie Haub94adfd2019-02-07 14:25:12 -0800297status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
298 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
299 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800300}
301
Valerie Haub94adfd2019-02-07 14:25:12 -0800302status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
303 const Rect& rect, void** vaddr, int fenceFd,
304 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800305 if (rect.left < 0 || rect.right > width ||
306 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300307 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
308 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800309 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300310 return BAD_VALUE;
311 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800312
Valerie Hau0c9fc362019-01-22 09:17:19 -0800313 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800314 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
315
Francis Hart8f396012014-04-01 15:30:53 +0300316 return res;
317}
318
Dan Stozad3182402014-11-17 12:03:59 -0800319status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
320 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300321{
322 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800323 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300324 return res;
325}
326
Dan Stozad3182402014-11-17 12:03:59 -0800327status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
328 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300329{
Dan Stozad3182402014-11-17 12:03:59 -0800330 if (rect.left < 0 || rect.right > width ||
331 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300332 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
333 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800334 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300335 return BAD_VALUE;
336 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700337 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300338 return res;
339}
340
341status_t GraphicBuffer::unlockAsync(int *fenceFd)
342{
343 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
344 return res;
345}
346
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800347status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
348 uint32_t inLayerCount, uint64_t inUsage,
349 bool* outSupported) const {
350 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
351 outSupported);
352}
353
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800354size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700355 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800356}
Mathias Agopian3330b202009-10-05 17:07:12 -0700357
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800358size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700359 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800360}
361
Mathias Agopiane1424282013-07-29 21:24:40 -0700362status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800363 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
364 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700365
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800366 size_t fdCountNeeded = GraphicBuffer::getFdCount();
367 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700368
Dan Stozab1363d32014-03-28 15:10:52 -0700369 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700370 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800371 buf[1] = width;
372 buf[2] = height;
373 buf[3] = stride;
374 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700375 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700376 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700377 buf[7] = static_cast<int32_t>(mId >> 32);
378 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
379 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700380 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700381 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700382 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800383
384 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700385 buf[10] = int32_t(mTransportNumFds);
386 buf[11] = int32_t(mTransportNumInts);
387 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700388 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700389 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700390 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800391
Dan Stozaeea6d682015-04-20 12:07:13 -0700392 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700393 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700394 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700395 fds += mTransportNumFds;
396 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700397 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700398
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800399 return NO_ERROR;
400}
401
Mathias Agopiane1424282013-07-29 21:24:40 -0700402status_t GraphicBuffer::unflatten(
403 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Chia-I Wubf8d7212018-10-09 15:22:46 -0700404 if (size < 12 * sizeof(int)) {
405 android_errorWriteLog(0x534e4554, "114223584");
406 return NO_MEMORY;
407 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800408
409 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700410
411 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
412 // see H2BGraphicBufferProducer.cpp
413 uint32_t flattenWordCount = 0;
414 if (buf[0] == 'GB01') {
415 // new version with 64-bits usage bits
416 flattenWordCount = 13;
417 } else if (buf[0] == 'GBFR') {
418 // old version, when usage bits were 32-bits
419 flattenWordCount = 12;
420 } else {
421 return BAD_TYPE;
422 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800423
Craig Donner6ebc46a2016-10-21 15:23:44 -0700424 const size_t numFds = static_cast<size_t>(buf[10]);
425 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800426
Michael Lentinec168b8a2015-02-18 10:14:18 -0800427 // Limit the maxNumber to be relatively small. The number of fds or ints
428 // should not come close to this number, and the number itself was simply
429 // chosen to be high enough to not cause issues and low enough to prevent
430 // overflow problems.
431 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700432 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
433 width = height = stride = format = usage_deprecated = 0;
434 layerCount = 0;
435 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700436 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700437 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700438 return BAD_VALUE;
439 }
440
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700441 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800442 if (size < sizeNeeded) return NO_MEMORY;
443
Michael Lentine38803262014-10-31 15:25:03 -0700444 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800445 if (count < fdCountNeeded) return NO_MEMORY;
446
447 if (handle) {
448 // free previous handle if any
449 free_handle();
450 }
451
452 if (numFds || numInts) {
453 width = buf[1];
454 height = buf[2];
455 stride = buf[3];
456 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700457 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700458 usage_deprecated = buf[6];
459 if (flattenWordCount == 13) {
460 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
461 } else {
462 usage = uint64_t(usage_deprecated);
463 }
Dan Stozad3182402014-11-17 12:03:59 -0800464 native_handle* h = native_handle_create(
465 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700466 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700467 width = height = stride = format = usage_deprecated = 0;
468 layerCount = 0;
469 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700470 handle = NULL;
471 ALOGE("unflatten: native_handle_create failed");
472 return NO_MEMORY;
473 }
Dan Stozad3182402014-11-17 12:03:59 -0800474 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700475 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800476 handle = h;
477 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 width = height = stride = format = usage_deprecated = 0;
479 layerCount = 0;
480 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800481 handle = NULL;
482 }
483
Craig Donner6ebc46a2016-10-21 15:23:44 -0700484 mId = static_cast<uint64_t>(buf[7]) << 32;
485 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700486
Craig Donner6ebc46a2016-10-21 15:23:44 -0700487 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700488
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800489 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700490
491 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700492 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700493 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
494 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700495 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700496 width = height = stride = format = usage_deprecated = 0;
497 layerCount = 0;
498 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800499 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700500 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700501 return err;
502 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700503
504 native_handle_close(handle);
505 native_handle_delete(const_cast<native_handle_t*>(handle));
506 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700507 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700508 }
509
Dan Stozaeea6d682015-04-20 12:07:13 -0700510 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700511 size -= sizeNeeded;
512 fds += numFds;
513 count -= numFds;
514
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800515 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700516}
517
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700518#ifndef LIBUI_IN_VNDK
519bool GraphicBuffer::isBufferHubBuffer() const {
520 return mBufferHubBuffer != nullptr;
521}
522#endif // LIBUI_IN_VNDK
523
Mathias Agopian3330b202009-10-05 17:07:12 -0700524// ---------------------------------------------------------------------------
525
526}; // namespace android