blob: 4aa9f628ce60fea600803651ea62d3944b1524d9 [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) {
Chia-I Wu955ef122018-10-09 15:22:46 -0700373 if (size < 12 * sizeof(int)) {
374 android_errorWriteLog(0x534e4554, "114223584");
375 return NO_MEMORY;
376 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800377
378 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700379
380 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
381 // see H2BGraphicBufferProducer.cpp
382 uint32_t flattenWordCount = 0;
383 if (buf[0] == 'GB01') {
384 // new version with 64-bits usage bits
385 flattenWordCount = 13;
386 } else if (buf[0] == 'GBFR') {
387 // old version, when usage bits were 32-bits
388 flattenWordCount = 12;
389 } else {
390 return BAD_TYPE;
391 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800392
Craig Donner6ebc46a2016-10-21 15:23:44 -0700393 const size_t numFds = static_cast<size_t>(buf[10]);
394 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800395
Michael Lentinec168b8a2015-02-18 10:14:18 -0800396 // Limit the maxNumber to be relatively small. The number of fds or ints
397 // should not come close to this number, and the number itself was simply
398 // chosen to be high enough to not cause issues and low enough to prevent
399 // overflow problems.
400 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700401 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
402 width = height = stride = format = usage_deprecated = 0;
403 layerCount = 0;
404 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700405 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700406 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700407 return BAD_VALUE;
408 }
409
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700410 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800411 if (size < sizeNeeded) return NO_MEMORY;
412
Michael Lentine38803262014-10-31 15:25:03 -0700413 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800414 if (count < fdCountNeeded) return NO_MEMORY;
415
416 if (handle) {
417 // free previous handle if any
418 free_handle();
419 }
420
421 if (numFds || numInts) {
422 width = buf[1];
423 height = buf[2];
424 stride = buf[3];
425 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700426 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700427 usage_deprecated = buf[6];
428 if (flattenWordCount == 13) {
429 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
430 } else {
431 usage = uint64_t(usage_deprecated);
432 }
Dan Stozad3182402014-11-17 12:03:59 -0800433 native_handle* h = native_handle_create(
434 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700435 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700436 width = height = stride = format = usage_deprecated = 0;
437 layerCount = 0;
438 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700439 handle = NULL;
440 ALOGE("unflatten: native_handle_create failed");
441 return NO_MEMORY;
442 }
Dan Stozad3182402014-11-17 12:03:59 -0800443 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700444 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800445 handle = h;
446 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700447 width = height = stride = format = usage_deprecated = 0;
448 layerCount = 0;
449 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800450 handle = NULL;
451 }
452
Craig Donner6ebc46a2016-10-21 15:23:44 -0700453 mId = static_cast<uint64_t>(buf[7]) << 32;
454 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700455
Craig Donner6ebc46a2016-10-21 15:23:44 -0700456 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700457
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800458 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700459
460 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700461 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700462 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
463 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700464 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700465 width = height = stride = format = usage_deprecated = 0;
466 layerCount = 0;
467 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800468 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700469 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700470 return err;
471 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700472
473 native_handle_close(handle);
474 native_handle_delete(const_cast<native_handle_t*>(handle));
475 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700476 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700477 }
478
Dan Stozaeea6d682015-04-20 12:07:13 -0700479 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700480 size -= sizeNeeded;
481 fds += numFds;
482 count -= numFds;
483
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800484 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700485}
486
Jiwen 'Steve' Cai44addfe2018-03-13 21:37:11 -0700487bool GraphicBuffer::isDetachedBuffer() const {
488 return mDetachedBufferHandle && mDetachedBufferHandle->isValid();
489}
490
491status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> channel) {
492 if (isDetachedBuffer()) {
493 ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this "
494 "GraphicBuffer. Replacing the old one.");
495 }
496
497 mDetachedBufferHandle = std::move(channel);
498 return NO_ERROR;
499}
500
501std::unique_ptr<DetachedBufferHandle> GraphicBuffer::takeDetachedBufferHandle() {
502 return std::move(mDetachedBufferHandle);
503}
504
Mathias Agopian3330b202009-10-05 17:07:12 -0700505// ---------------------------------------------------------------------------
506
507}; // namespace android