blob: 7670ac6fbdbf282c3251ecea068774498748e60a [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{
Colin Cross18fae752014-07-22 15:55:08 -0700126 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700127 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700128 const_cast<GraphicBuffer*>(this));
129}
130
Dan Stozad3182402014-11-17 12:03:59 -0800131status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700132 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700133{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700134 if (mOwner != ownData)
135 return INVALID_OPERATION;
136
Dan Stozad3182402014-11-17 12:03:59 -0800137 if (handle &&
138 static_cast<int>(inWidth) == width &&
139 static_cast<int>(inHeight) == height &&
140 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700141 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700142 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700143 return NO_ERROR;
144
Mathias Agopian3330b202009-10-05 17:07:12 -0700145 if (handle) {
146 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
147 allocator.free(handle);
148 handle = 0;
149 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700150 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700151}
152
Dan Stoza9de72932015-04-16 17:28:43 -0700153bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700154 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700155{
156 if (static_cast<int>(inWidth) != width) return true;
157 if (static_cast<int>(inHeight) != height) return true;
158 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700159 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700160 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700161 return false;
162}
163
Chia-I Wub42f1712017-03-21 13:15:39 -0700164status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700165 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
166 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700167{
Mathias Agopian3330b202009-10-05 17:07:12 -0700168 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800169 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700170 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700171 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800172 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700173 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700174 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
175
Dan Stozad3182402014-11-17 12:03:59 -0800176 width = static_cast<int>(inWidth);
177 height = static_cast<int>(inHeight);
178 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700179 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700180 usage = inUsage;
181 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800182 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700183 }
184 return err;
185}
186
Chia-I Wub42f1712017-03-21 13:15:39 -0700187status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
188 HandleWrapMethod method, uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700189 PixelFormat format, uint32_t layerCount, uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -0700190 uint32_t stride)
191{
Chia-I Wub42f1712017-03-21 13:15:39 -0700192 ANativeWindowBuffer::width = static_cast<int>(width);
193 ANativeWindowBuffer::height = static_cast<int>(height);
194 ANativeWindowBuffer::stride = static_cast<int>(stride);
195 ANativeWindowBuffer::format = format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700196 ANativeWindowBuffer::usage = usage;
197 ANativeWindowBuffer::usage_deprecated = int(usage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700198
199 ANativeWindowBuffer::layerCount = layerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700200
201 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
202
Chia-I Wucb8405e2017-04-17 15:20:19 -0700203 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
204 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700205 status_t err = mBufferMapper.importBuffer(handle, width, height,
206 layerCount, format, usage, stride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700207 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700208 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700209
210 return err;
211 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700212
213 if (method == TAKE_UNREGISTERED_HANDLE) {
214 native_handle_close(handle);
215 native_handle_delete(const_cast<native_handle_t*>(handle));
216 }
217
218 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700219 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700220 }
221
Chia-I Wucb8405e2017-04-17 15:20:19 -0700222 ANativeWindowBuffer::handle = handle;
223
Chia-I Wub42f1712017-03-21 13:15:39 -0700224 return NO_ERROR;
225}
226
Dan Stozad3182402014-11-17 12:03:59 -0800227status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700228{
229 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800230 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700231 return res;
232}
233
Dan Stozad3182402014-11-17 12:03:59 -0800234status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700235{
Dan Stozad3182402014-11-17 12:03:59 -0800236 if (rect.left < 0 || rect.right > width ||
237 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000238 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800239 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800240 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700241 return BAD_VALUE;
242 }
Dan Stozad3182402014-11-17 12:03:59 -0800243 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700244 return res;
245}
246
Dan Stozad3182402014-11-17 12:03:59 -0800247status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700248{
249 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800250 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700251 return res;
252}
253
Dan Stozad3182402014-11-17 12:03:59 -0800254status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
255 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700256{
Dan Stozad3182402014-11-17 12:03:59 -0800257 if (rect.left < 0 || rect.right > width ||
258 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700259 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
260 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800261 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700262 return BAD_VALUE;
263 }
Dan Stozad3182402014-11-17 12:03:59 -0800264 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700265 return res;
266}
267
Mathias Agopian3330b202009-10-05 17:07:12 -0700268status_t GraphicBuffer::unlock()
269{
270 status_t res = getBufferMapper().unlock(handle);
271 return res;
272}
273
Dan Stozad3182402014-11-17 12:03:59 -0800274status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300275{
276 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800277 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300278 return res;
279}
280
Dan Stozad3182402014-11-17 12:03:59 -0800281status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
282 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300283{
Craig Donnere96a3252017-02-02 12:13:34 -0800284 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
285}
286
287status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
288 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
289{
Dan Stozad3182402014-11-17 12:03:59 -0800290 if (rect.left < 0 || rect.right > width ||
291 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300292 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
293 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800294 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300295 return BAD_VALUE;
296 }
Craig Donnere96a3252017-02-02 12:13:34 -0800297 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
298 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300299 return res;
300}
301
Dan Stozad3182402014-11-17 12:03:59 -0800302status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
303 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300304{
305 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800306 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300307 return res;
308}
309
Dan Stozad3182402014-11-17 12:03:59 -0800310status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
311 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300312{
Dan Stozad3182402014-11-17 12:03:59 -0800313 if (rect.left < 0 || rect.right > width ||
314 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300315 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
316 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800317 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300318 return BAD_VALUE;
319 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700320 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300321 return res;
322}
323
324status_t GraphicBuffer::unlockAsync(int *fenceFd)
325{
326 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
327 return res;
328}
329
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800330size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700331 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800332}
Mathias Agopian3330b202009-10-05 17:07:12 -0700333
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800334size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700335 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800336}
337
Mathias Agopiane1424282013-07-29 21:24:40 -0700338status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800339 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
340 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700341
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800342 size_t fdCountNeeded = GraphicBuffer::getFdCount();
343 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700344
Dan Stozab1363d32014-03-28 15:10:52 -0700345 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700346 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800347 buf[1] = width;
348 buf[2] = height;
349 buf[3] = stride;
350 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700351 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700352 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700353 buf[7] = static_cast<int32_t>(mId >> 32);
354 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
355 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700356 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700357 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700358 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800359
360 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700361 buf[10] = int32_t(mTransportNumFds);
362 buf[11] = int32_t(mTransportNumInts);
363 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700364 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700365 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700366 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800367
Dan Stozaeea6d682015-04-20 12:07:13 -0700368 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700369 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700370 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700371 fds += mTransportNumFds;
372 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700373 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700374
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800375 return NO_ERROR;
376}
377
Mathias Agopiane1424282013-07-29 21:24:40 -0700378status_t GraphicBuffer::unflatten(
379 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Chia-I Wubf8d7212018-10-09 15:22:46 -0700380 if (size < 12 * sizeof(int)) {
381 android_errorWriteLog(0x534e4554, "114223584");
382 return NO_MEMORY;
383 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800384
385 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700386
387 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
388 // see H2BGraphicBufferProducer.cpp
389 uint32_t flattenWordCount = 0;
390 if (buf[0] == 'GB01') {
391 // new version with 64-bits usage bits
392 flattenWordCount = 13;
393 } else if (buf[0] == 'GBFR') {
394 // old version, when usage bits were 32-bits
395 flattenWordCount = 12;
396 } else {
397 return BAD_TYPE;
398 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800399
Craig Donner6ebc46a2016-10-21 15:23:44 -0700400 const size_t numFds = static_cast<size_t>(buf[10]);
401 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800402
Michael Lentinec168b8a2015-02-18 10:14:18 -0800403 // Limit the maxNumber to be relatively small. The number of fds or ints
404 // should not come close to this number, and the number itself was simply
405 // chosen to be high enough to not cause issues and low enough to prevent
406 // overflow problems.
407 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
409 width = height = stride = format = usage_deprecated = 0;
410 layerCount = 0;
411 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700412 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700413 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700414 return BAD_VALUE;
415 }
416
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700417 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800418 if (size < sizeNeeded) return NO_MEMORY;
419
Michael Lentine38803262014-10-31 15:25:03 -0700420 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800421 if (count < fdCountNeeded) return NO_MEMORY;
422
423 if (handle) {
424 // free previous handle if any
425 free_handle();
426 }
427
428 if (numFds || numInts) {
429 width = buf[1];
430 height = buf[2];
431 stride = buf[3];
432 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700433 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700434 usage_deprecated = buf[6];
435 if (flattenWordCount == 13) {
436 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
437 } else {
438 usage = uint64_t(usage_deprecated);
439 }
Dan Stozad3182402014-11-17 12:03:59 -0800440 native_handle* h = native_handle_create(
441 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700442 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700443 width = height = stride = format = usage_deprecated = 0;
444 layerCount = 0;
445 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700446 handle = NULL;
447 ALOGE("unflatten: native_handle_create failed");
448 return NO_MEMORY;
449 }
Dan Stozad3182402014-11-17 12:03:59 -0800450 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700451 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800452 handle = h;
453 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700454 width = height = stride = format = usage_deprecated = 0;
455 layerCount = 0;
456 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800457 handle = NULL;
458 }
459
Craig Donner6ebc46a2016-10-21 15:23:44 -0700460 mId = static_cast<uint64_t>(buf[7]) << 32;
461 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700462
Craig Donner6ebc46a2016-10-21 15:23:44 -0700463 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700464
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800465 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700466
467 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700468 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700469 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
470 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700471 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700472 width = height = stride = format = usage_deprecated = 0;
473 layerCount = 0;
474 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800475 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700476 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700477 return err;
478 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700479
480 native_handle_close(handle);
481 native_handle_delete(const_cast<native_handle_t*>(handle));
482 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700483 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700484 }
485
Dan Stozaeea6d682015-04-20 12:07:13 -0700486 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700487 size -= sizeNeeded;
488 fds += numFds;
489 count -= numFds;
490
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800491 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700492}
493
Jiwen 'Steve' Cai44addfe2018-03-13 21:37:11 -0700494bool GraphicBuffer::isDetachedBuffer() const {
495 return mDetachedBufferHandle && mDetachedBufferHandle->isValid();
496}
497
498status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> channel) {
499 if (isDetachedBuffer()) {
500 ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this "
501 "GraphicBuffer. Replacing the old one.");
502 }
503
504 mDetachedBufferHandle = std::move(channel);
505 return NO_ERROR;
506}
507
508std::unique_ptr<DetachedBufferHandle> GraphicBuffer::takeDetachedBufferHandle() {
509 return std::move(mDetachedBufferHandle);
510}
511
Mathias Agopian3330b202009-10-05 17:07:12 -0700512// ---------------------------------------------------------------------------
513
514}; // namespace android