blob: c1f1d2583f6155ed0aed54f7d7b57e41fe34ba0e [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
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080068GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
69 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
70 : GraphicBuffer() {
71 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
72 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070073}
74
Chia-I Wub42f1712017-03-21 13:15:39 -070075// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070076GraphicBuffer::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)
Chia-I Wub42f1712017-03-21 13:15:39 -070079 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070080 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070081 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070082{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070083}
84
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080085GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
86 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
87 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
88 : GraphicBuffer() {
89 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
90 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -070091}
92
Mathias Agopian3330b202009-10-05 17:07:12 -070093GraphicBuffer::~GraphicBuffer()
94{
95 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080096 free_handle();
97 }
98}
99
100void GraphicBuffer::free_handle()
101{
102 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700103 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800104 } else if (mOwner == ownData) {
105 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
106 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700107 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700108 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700109}
110
111status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800112 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700113}
114
Mathias Agopian678bdd62010-12-03 17:33:09 -0800115void GraphicBuffer::dumpAllocationsToSystemLog()
116{
117 GraphicBufferAllocator::dumpToSystemLog();
118}
119
Iliyan Malchev697526b2011-05-01 11:33:26 -0700120ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700121{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700122 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700123 const_cast<GraphicBuffer*>(this));
124}
125
Dan Stozad3182402014-11-17 12:03:59 -0800126status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700127 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700128{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700129 if (mOwner != ownData)
130 return INVALID_OPERATION;
131
Dan Stozad3182402014-11-17 12:03:59 -0800132 if (handle &&
133 static_cast<int>(inWidth) == width &&
134 static_cast<int>(inHeight) == height &&
135 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700136 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700137 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700138 return NO_ERROR;
139
Mathias Agopian3330b202009-10-05 17:07:12 -0700140 if (handle) {
141 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
142 allocator.free(handle);
143 handle = 0;
144 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700145 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700146}
147
Dan Stoza9de72932015-04-16 17:28:43 -0700148bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700149 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700150{
151 if (static_cast<int>(inWidth) != width) return true;
152 if (static_cast<int>(inHeight) != height) return true;
153 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700154 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700155 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700156 return false;
157}
158
Chia-I Wub42f1712017-03-21 13:15:39 -0700159status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700160 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
161 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700162{
Mathias Agopian3330b202009-10-05 17:07:12 -0700163 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800164 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700165 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700166 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800167 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700169 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
170
Dan Stozad3182402014-11-17 12:03:59 -0800171 width = static_cast<int>(inWidth);
172 height = static_cast<int>(inHeight);
173 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700174 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700175 usage = inUsage;
176 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800177 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700178 }
179 return err;
180}
181
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800182status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
183 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
184 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
185 ANativeWindowBuffer::width = static_cast<int>(inWidth);
186 ANativeWindowBuffer::height = static_cast<int>(inHeight);
187 ANativeWindowBuffer::stride = static_cast<int>(inStride);
188 ANativeWindowBuffer::format = inFormat;
189 ANativeWindowBuffer::usage = inUsage;
190 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700191
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800192 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700193
194 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
195
Chia-I Wucb8405e2017-04-17 15:20:19 -0700196 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
197 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800198 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
199 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700200 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700201 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700202
203 return err;
204 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700205
206 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800207 native_handle_close(inHandle);
208 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700209 }
210
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800211 inHandle = importedHandle;
212 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700213 }
214
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800215 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700216
Chia-I Wub42f1712017-03-21 13:15:39 -0700217 return NO_ERROR;
218}
219
Dan Stozad3182402014-11-17 12:03:59 -0800220status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700221{
222 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800223 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700224 return res;
225}
226
Dan Stozad3182402014-11-17 12:03:59 -0800227status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700228{
Dan Stozad3182402014-11-17 12:03:59 -0800229 if (rect.left < 0 || rect.right > width ||
230 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800232 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800233 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700234 return BAD_VALUE;
235 }
Dan Stozad3182402014-11-17 12:03:59 -0800236 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700237 return res;
238}
239
Dan Stozad3182402014-11-17 12:03:59 -0800240status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700241{
242 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800243 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700244 return res;
245}
246
Dan Stozad3182402014-11-17 12:03:59 -0800247status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
248 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700249{
Dan Stozad3182402014-11-17 12:03:59 -0800250 if (rect.left < 0 || rect.right > width ||
251 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700252 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
253 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800254 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700255 return BAD_VALUE;
256 }
Dan Stozad3182402014-11-17 12:03:59 -0800257 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700258 return res;
259}
260
Mathias Agopian3330b202009-10-05 17:07:12 -0700261status_t GraphicBuffer::unlock()
262{
263 status_t res = getBufferMapper().unlock(handle);
264 return res;
265}
266
Dan Stozad3182402014-11-17 12:03:59 -0800267status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300268{
269 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800270 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300271 return res;
272}
273
Dan Stozad3182402014-11-17 12:03:59 -0800274status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
275 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300276{
Craig Donnere96a3252017-02-02 12:13:34 -0800277 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
278}
279
280status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
281 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
282{
Dan Stozad3182402014-11-17 12:03:59 -0800283 if (rect.left < 0 || rect.right > width ||
284 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300285 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
286 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800287 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300288 return BAD_VALUE;
289 }
Craig Donnere96a3252017-02-02 12:13:34 -0800290 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
291 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300292 return res;
293}
294
Dan Stozad3182402014-11-17 12:03:59 -0800295status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
296 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300297{
298 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800299 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300300 return res;
301}
302
Dan Stozad3182402014-11-17 12:03:59 -0800303status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
304 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300305{
Dan Stozad3182402014-11-17 12:03:59 -0800306 if (rect.left < 0 || rect.right > width ||
307 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300308 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
309 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800310 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300311 return BAD_VALUE;
312 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700313 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300314 return res;
315}
316
317status_t GraphicBuffer::unlockAsync(int *fenceFd)
318{
319 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
320 return res;
321}
322
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800323size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700324 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800325}
Mathias Agopian3330b202009-10-05 17:07:12 -0700326
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800327size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700328 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800329}
330
Mathias Agopiane1424282013-07-29 21:24:40 -0700331status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800332 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
333 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700334
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800335 size_t fdCountNeeded = GraphicBuffer::getFdCount();
336 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700337
Dan Stozab1363d32014-03-28 15:10:52 -0700338 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700339 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800340 buf[1] = width;
341 buf[2] = height;
342 buf[3] = stride;
343 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700344 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700345 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700346 buf[7] = static_cast<int32_t>(mId >> 32);
347 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
348 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700349 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700350 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700351 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800352
353 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700354 buf[10] = int32_t(mTransportNumFds);
355 buf[11] = int32_t(mTransportNumInts);
356 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700357 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700358 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700359 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800360
Dan Stozaeea6d682015-04-20 12:07:13 -0700361 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700362 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700363 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700364 fds += mTransportNumFds;
365 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700366 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700367
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800368 return NO_ERROR;
369}
370
Mathias Agopiane1424282013-07-29 21:24:40 -0700371status_t GraphicBuffer::unflatten(
372 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800373
374 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700375
376 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
377 // see H2BGraphicBufferProducer.cpp
378 uint32_t flattenWordCount = 0;
379 if (buf[0] == 'GB01') {
380 // new version with 64-bits usage bits
381 flattenWordCount = 13;
382 } else if (buf[0] == 'GBFR') {
383 // old version, when usage bits were 32-bits
384 flattenWordCount = 12;
385 } else {
386 return BAD_TYPE;
387 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800388
Craig Donner6ebc46a2016-10-21 15:23:44 -0700389 const size_t numFds = static_cast<size_t>(buf[10]);
390 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800391
Michael Lentinec168b8a2015-02-18 10:14:18 -0800392 // Limit the maxNumber to be relatively small. The number of fds or ints
393 // should not come close to this number, and the number itself was simply
394 // chosen to be high enough to not cause issues and low enough to prevent
395 // overflow problems.
396 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700397 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
398 width = height = stride = format = usage_deprecated = 0;
399 layerCount = 0;
400 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700401 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700402 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700403 return BAD_VALUE;
404 }
405
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700406 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800407 if (size < sizeNeeded) return NO_MEMORY;
408
Michael Lentine38803262014-10-31 15:25:03 -0700409 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800410 if (count < fdCountNeeded) return NO_MEMORY;
411
412 if (handle) {
413 // free previous handle if any
414 free_handle();
415 }
416
417 if (numFds || numInts) {
418 width = buf[1];
419 height = buf[2];
420 stride = buf[3];
421 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700422 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700423 usage_deprecated = buf[6];
424 if (flattenWordCount == 13) {
425 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
426 } else {
427 usage = uint64_t(usage_deprecated);
428 }
Dan Stozad3182402014-11-17 12:03:59 -0800429 native_handle* h = native_handle_create(
430 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700431 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700432 width = height = stride = format = usage_deprecated = 0;
433 layerCount = 0;
434 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700435 handle = NULL;
436 ALOGE("unflatten: native_handle_create failed");
437 return NO_MEMORY;
438 }
Dan Stozad3182402014-11-17 12:03:59 -0800439 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700440 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800441 handle = h;
442 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700443 width = height = stride = format = usage_deprecated = 0;
444 layerCount = 0;
445 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800446 handle = NULL;
447 }
448
Craig Donner6ebc46a2016-10-21 15:23:44 -0700449 mId = static_cast<uint64_t>(buf[7]) << 32;
450 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700451
Craig Donner6ebc46a2016-10-21 15:23:44 -0700452 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700453
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800454 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700455
456 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700457 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700458 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
459 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700460 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700461 width = height = stride = format = usage_deprecated = 0;
462 layerCount = 0;
463 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800464 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700465 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700466 return err;
467 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700468
469 native_handle_close(handle);
470 native_handle_delete(const_cast<native_handle_t*>(handle));
471 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700472 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700473 }
474
Dan Stozaeea6d682015-04-20 12:07:13 -0700475 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700476 size -= sizeNeeded;
477 fds += numFds;
478 count -= numFds;
479
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800480 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700481}
482
Jiwen 'Steve' Cai44addfe2018-03-13 21:37:11 -0700483bool GraphicBuffer::isDetachedBuffer() const {
484 return mDetachedBufferHandle && mDetachedBufferHandle->isValid();
485}
486
487status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> channel) {
488 if (isDetachedBuffer()) {
489 ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this "
490 "GraphicBuffer. Replacing the old one.");
491 }
492
493 mDetachedBufferHandle = std::move(channel);
494 return NO_ERROR;
495}
496
497std::unique_ptr<DetachedBufferHandle> GraphicBuffer::takeDetachedBufferHandle() {
498 return std::move(mDetachedBufferHandle);
499}
500
Mathias Agopian3330b202009-10-05 17:07:12 -0700501// ---------------------------------------------------------------------------
502
503}; // namespace android