blob: 03824795ed62707502cc88714e76a18b8ea26993 [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' Cai44addfe2018-03-13 21:37:11 -070025#include <ui/DetachedBufferHandle.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070026#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <ui/GraphicBufferAllocator.h>
28#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070029
Mathias Agopian3330b202009-10-05 17:07:12 -070030namespace android {
31
32// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070033// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070034// ===========================================================================
35
Dan Stozab1363d32014-03-28 15:10:52 -070036static uint64_t getUniqueId() {
37 static volatile int32_t nextId = 0;
38 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
39 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
40 return id;
41}
42
Mathias Agopianf543e5a2017-04-03 17:16:41 -070043sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
44 return static_cast<GraphicBuffer *>(anwb);
45}
46
Mathias Agopian3330b202009-10-05 17:07:12 -070047GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070048 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070049 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070050{
Dan Stoza01049c82014-11-11 10:32:31 -080051 width =
52 height =
53 stride =
54 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070055 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070056 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080057 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070058 handle = NULL;
59}
60
Chia-I Wub42f1712017-03-21 13:15:39 -070061// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080062GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070063 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070064 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070065{
Mathias Agopian3330b202009-10-05 17:07:12 -070066}
67
Dan Stozad3182402014-11-17 12:03:59 -080068GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Mathias Agopiancb496ac2017-05-22 14:21:00 -070069 PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070070 : GraphicBuffer()
Craig Donner6ebc46a2016-10-21 15:23:44 -070071{
Chia-I Wub42f1712017-03-21 13:15:39 -070072 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070073 usage, std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070074}
75
Chia-I Wub42f1712017-03-21 13:15:39 -070076// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070077GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
78 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
79 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070080 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070081 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070082 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070083{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070084}
85
Chia-I Wub42f1712017-03-21 13:15:39 -070086GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
87 HandleWrapMethod method, uint32_t width, uint32_t height,
88 PixelFormat format, uint32_t layerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070089 uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -070090 uint32_t stride)
91 : GraphicBuffer()
92{
93 mInitCheck = initWithHandle(handle, method, width, height, format,
Chris Forbes82c04982017-04-19 14:29:54 -070094 layerCount, usage, stride);
Chia-I Wub42f1712017-03-21 13:15:39 -070095}
96
Mathias Agopian3330b202009-10-05 17:07:12 -070097GraphicBuffer::~GraphicBuffer()
98{
99 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800100 free_handle();
101 }
102}
103
104void GraphicBuffer::free_handle()
105{
106 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700107 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800108 } else if (mOwner == ownData) {
109 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
110 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700111 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700112 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700113}
114
115status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800116 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700117}
118
Mathias Agopian678bdd62010-12-03 17:33:09 -0800119void GraphicBuffer::dumpAllocationsToSystemLog()
120{
121 GraphicBufferAllocator::dumpToSystemLog();
122}
123
Iliyan Malchev697526b2011-05-01 11:33:26 -0700124ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700125{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700126 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700127 const_cast<GraphicBuffer*>(this));
128}
129
Dan Stozad3182402014-11-17 12:03:59 -0800130status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700131 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700132{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700133 if (mOwner != ownData)
134 return INVALID_OPERATION;
135
Dan Stozad3182402014-11-17 12:03:59 -0800136 if (handle &&
137 static_cast<int>(inWidth) == width &&
138 static_cast<int>(inHeight) == height &&
139 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700140 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700141 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700142 return NO_ERROR;
143
Mathias Agopian3330b202009-10-05 17:07:12 -0700144 if (handle) {
145 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
146 allocator.free(handle);
147 handle = 0;
148 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700149 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700150}
151
Dan Stoza9de72932015-04-16 17:28:43 -0700152bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700153 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700154{
155 if (static_cast<int>(inWidth) != width) return true;
156 if (static_cast<int>(inHeight) != height) return true;
157 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700158 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700159 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700160 return false;
161}
162
Chia-I Wub42f1712017-03-21 13:15:39 -0700163status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700164 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
165 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700166{
Mathias Agopian3330b202009-10-05 17:07:12 -0700167 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800168 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700169 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700170 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800171 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700172 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700173 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
174
Dan Stozad3182402014-11-17 12:03:59 -0800175 width = static_cast<int>(inWidth);
176 height = static_cast<int>(inHeight);
177 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700178 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700179 usage = inUsage;
180 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800181 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 }
183 return err;
184}
185
Chia-I Wub42f1712017-03-21 13:15:39 -0700186status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
187 HandleWrapMethod method, uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700188 PixelFormat format, uint32_t layerCount, uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -0700189 uint32_t stride)
190{
Chia-I Wub42f1712017-03-21 13:15:39 -0700191 ANativeWindowBuffer::width = static_cast<int>(width);
192 ANativeWindowBuffer::height = static_cast<int>(height);
193 ANativeWindowBuffer::stride = static_cast<int>(stride);
194 ANativeWindowBuffer::format = format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700195 ANativeWindowBuffer::usage = usage;
196 ANativeWindowBuffer::usage_deprecated = int(usage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700197
198 ANativeWindowBuffer::layerCount = layerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700199
200 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
201
Chia-I Wucb8405e2017-04-17 15:20:19 -0700202 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
203 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700204 status_t err = mBufferMapper.importBuffer(handle, width, height,
205 layerCount, format, usage, stride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700206 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700207 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700208
209 return err;
210 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700211
212 if (method == TAKE_UNREGISTERED_HANDLE) {
213 native_handle_close(handle);
214 native_handle_delete(const_cast<native_handle_t*>(handle));
215 }
216
217 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700218 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700219 }
220
Chia-I Wucb8405e2017-04-17 15:20:19 -0700221 ANativeWindowBuffer::handle = handle;
222
Chia-I Wub42f1712017-03-21 13:15:39 -0700223 return NO_ERROR;
224}
225
Dan Stozad3182402014-11-17 12:03:59 -0800226status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700227{
228 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800229 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700230 return res;
231}
232
Dan Stozad3182402014-11-17 12:03:59 -0800233status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700234{
Dan Stozad3182402014-11-17 12:03:59 -0800235 if (rect.left < 0 || rect.right > width ||
236 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000237 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800238 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800239 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700240 return BAD_VALUE;
241 }
Dan Stozad3182402014-11-17 12:03:59 -0800242 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700243 return res;
244}
245
Dan Stozad3182402014-11-17 12:03:59 -0800246status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700247{
248 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800249 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700250 return res;
251}
252
Dan Stozad3182402014-11-17 12:03:59 -0800253status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
254 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700255{
Dan Stozad3182402014-11-17 12:03:59 -0800256 if (rect.left < 0 || rect.right > width ||
257 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700258 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
259 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800260 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700261 return BAD_VALUE;
262 }
Dan Stozad3182402014-11-17 12:03:59 -0800263 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700264 return res;
265}
266
Mathias Agopian3330b202009-10-05 17:07:12 -0700267status_t GraphicBuffer::unlock()
268{
269 status_t res = getBufferMapper().unlock(handle);
270 return res;
271}
272
Dan Stozad3182402014-11-17 12:03:59 -0800273status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300274{
275 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800276 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300277 return res;
278}
279
Dan Stozad3182402014-11-17 12:03:59 -0800280status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
281 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300282{
Craig Donnere96a3252017-02-02 12:13:34 -0800283 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
284}
285
286status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
287 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
288{
Dan Stozad3182402014-11-17 12:03:59 -0800289 if (rect.left < 0 || rect.right > width ||
290 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300291 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
292 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800293 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300294 return BAD_VALUE;
295 }
Craig Donnere96a3252017-02-02 12:13:34 -0800296 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
297 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300298 return res;
299}
300
Dan Stozad3182402014-11-17 12:03:59 -0800301status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
302 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300303{
304 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800305 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300306 return res;
307}
308
Dan Stozad3182402014-11-17 12:03:59 -0800309status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
310 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300311{
Dan Stozad3182402014-11-17 12:03:59 -0800312 if (rect.left < 0 || rect.right > width ||
313 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300314 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
315 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800316 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300317 return BAD_VALUE;
318 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700319 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300320 return res;
321}
322
323status_t GraphicBuffer::unlockAsync(int *fenceFd)
324{
325 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
326 return res;
327}
328
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800329size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700330 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331}
Mathias Agopian3330b202009-10-05 17:07:12 -0700332
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800333size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700334 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800335}
336
Mathias Agopiane1424282013-07-29 21:24:40 -0700337status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800338 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
339 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700340
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800341 size_t fdCountNeeded = GraphicBuffer::getFdCount();
342 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700343
Dan Stozab1363d32014-03-28 15:10:52 -0700344 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700345 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800346 buf[1] = width;
347 buf[2] = height;
348 buf[3] = stride;
349 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700350 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700351 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700352 buf[7] = static_cast<int32_t>(mId >> 32);
353 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
354 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700355 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700356 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700357 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800358
359 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700360 buf[10] = int32_t(mTransportNumFds);
361 buf[11] = int32_t(mTransportNumInts);
362 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700363 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700364 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700365 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800366
Dan Stozaeea6d682015-04-20 12:07:13 -0700367 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700368 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700369 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700370 fds += mTransportNumFds;
371 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700372 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700373
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800374 return NO_ERROR;
375}
376
Mathias Agopiane1424282013-07-29 21:24:40 -0700377status_t GraphicBuffer::unflatten(
378 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800379
380 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700381
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 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394
Craig Donner6ebc46a2016-10-21 15:23:44 -0700395 const size_t numFds = static_cast<size_t>(buf[10]);
396 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800397
Michael Lentinec168b8a2015-02-18 10:14:18 -0800398 // 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;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700403 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
404 width = height = stride = format = usage_deprecated = 0;
405 layerCount = 0;
406 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700407 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700409 return BAD_VALUE;
410 }
411
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700412 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800413 if (size < sizeNeeded) return NO_MEMORY;
414
Michael Lentine38803262014-10-31 15:25:03 -0700415 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800416 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];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700428 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700429 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 }
Dan Stozad3182402014-11-17 12:03:59 -0800435 native_handle* h = native_handle_create(
436 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700437 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700438 width = height = stride = format = usage_deprecated = 0;
439 layerCount = 0;
440 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700441 handle = NULL;
442 ALOGE("unflatten: native_handle_create failed");
443 return NO_MEMORY;
444 }
Dan Stozad3182402014-11-17 12:03:59 -0800445 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700446 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800447 handle = h;
448 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700449 width = height = stride = format = usage_deprecated = 0;
450 layerCount = 0;
451 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800452 handle = NULL;
453 }
454
Craig Donner6ebc46a2016-10-21 15:23:44 -0700455 mId = static_cast<uint64_t>(buf[7]) << 32;
456 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700457
Craig Donner6ebc46a2016-10-21 15:23:44 -0700458 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700459
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800460 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700461
462 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700463 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700464 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
465 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700466 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700467 width = height = stride = format = usage_deprecated = 0;
468 layerCount = 0;
469 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800470 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700471 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700472 return err;
473 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700474
475 native_handle_close(handle);
476 native_handle_delete(const_cast<native_handle_t*>(handle));
477 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700478 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700479 }
480
Dan Stozaeea6d682015-04-20 12:07:13 -0700481 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700482 size -= sizeNeeded;
483 fds += numFds;
484 count -= numFds;
485
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800486 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700487}
488
Jiwen 'Steve' Cai44addfe2018-03-13 21:37:11 -0700489bool GraphicBuffer::isDetachedBuffer() const {
490 return mDetachedBufferHandle && mDetachedBufferHandle->isValid();
491}
492
493status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> channel) {
494 if (isDetachedBuffer()) {
495 ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this "
496 "GraphicBuffer. Replacing the old one.");
497 }
498
499 mDetachedBufferHandle = std::move(channel);
500 return NO_ERROR;
501}
502
503std::unique_ptr<DetachedBufferHandle> GraphicBuffer::takeDetachedBufferHandle() {
504 return std::move(mDetachedBufferHandle);
505}
506
Mathias Agopian3330b202009-10-05 17:07:12 -0700507// ---------------------------------------------------------------------------
508
509}; // namespace android