blob: 5a1ddee0028219f0995b859d26a2abe0de417093 [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
Chia-I Wu5bac7f32017-04-06 12:34:32 -070025#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070026#include <ui/GraphicBufferAllocator.h>
27#include <ui/GraphicBufferMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070028
Mathias Agopian3330b202009-10-05 17:07:12 -070029namespace android {
30
31// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070032// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070033// ===========================================================================
34
Dan Stozab1363d32014-03-28 15:10:52 -070035static uint64_t getUniqueId() {
36 static volatile int32_t nextId = 0;
37 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
38 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
39 return id;
40}
41
Mathias Agopianf543e5a2017-04-03 17:16:41 -070042sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
43 return static_cast<GraphicBuffer *>(anwb);
44}
45
Mathias Agopian3330b202009-10-05 17:07:12 -070046GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070047 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070048 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070049{
Dan Stoza01049c82014-11-11 10:32:31 -080050 width =
51 height =
52 stride =
53 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070054 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070055 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080056 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070057 handle = NULL;
58}
59
Chia-I Wub42f1712017-03-21 13:15:39 -070060// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080061GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070062 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070063 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070064{
Mathias Agopian3330b202009-10-05 17:07:12 -070065}
66
Dan Stozad3182402014-11-17 12:03:59 -080067GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Mathias Agopiancb496ac2017-05-22 14:21:00 -070068 PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName)
Chia-I Wub42f1712017-03-21 13:15:39 -070069 : GraphicBuffer()
Craig Donner6ebc46a2016-10-21 15:23:44 -070070{
Chia-I Wub42f1712017-03-21 13:15:39 -070071 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070072 usage, 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
Chia-I Wub42f1712017-03-21 13:15:39 -070085GraphicBuffer::GraphicBuffer(const native_handle_t* handle,
86 HandleWrapMethod method, uint32_t width, uint32_t height,
87 PixelFormat format, uint32_t layerCount,
Chris Forbes82c04982017-04-19 14:29:54 -070088 uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -070089 uint32_t stride)
90 : GraphicBuffer()
91{
92 mInitCheck = initWithHandle(handle, method, width, height, format,
Chris Forbes82c04982017-04-19 14:29:54 -070093 layerCount, usage, stride);
Chia-I Wub42f1712017-03-21 13:15:39 -070094}
95
Mathias Agopian3330b202009-10-05 17:07:12 -070096GraphicBuffer::~GraphicBuffer()
97{
98 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -080099 free_handle();
100 }
101}
102
103void GraphicBuffer::free_handle()
104{
105 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700106 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800107 } else if (mOwner == ownData) {
108 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
109 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700110 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700111 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700112}
113
114status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800115 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700116}
117
Mathias Agopian678bdd62010-12-03 17:33:09 -0800118void GraphicBuffer::dumpAllocationsToSystemLog()
119{
120 GraphicBufferAllocator::dumpToSystemLog();
121}
122
Iliyan Malchev697526b2011-05-01 11:33:26 -0700123ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700124{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700125 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700126 const_cast<GraphicBuffer*>(this));
127}
128
Dan Stozad3182402014-11-17 12:03:59 -0800129status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700130 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700131{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700132 if (mOwner != ownData)
133 return INVALID_OPERATION;
134
Dan Stozad3182402014-11-17 12:03:59 -0800135 if (handle &&
136 static_cast<int>(inWidth) == width &&
137 static_cast<int>(inHeight) == height &&
138 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700139 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700140 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700141 return NO_ERROR;
142
Mathias Agopian3330b202009-10-05 17:07:12 -0700143 if (handle) {
144 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
145 allocator.free(handle);
146 handle = 0;
147 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700148 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700149}
150
Dan Stoza9de72932015-04-16 17:28:43 -0700151bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700152 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700153{
154 if (static_cast<int>(inWidth) != width) return true;
155 if (static_cast<int>(inHeight) != height) return true;
156 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700157 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700158 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700159 return false;
160}
161
Chia-I Wub42f1712017-03-21 13:15:39 -0700162status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700163 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
164 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700165{
Mathias Agopian3330b202009-10-05 17:07:12 -0700166 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800167 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700168 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700169 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800170 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700172 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
173
Dan Stozad3182402014-11-17 12:03:59 -0800174 width = static_cast<int>(inWidth);
175 height = static_cast<int>(inHeight);
176 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700177 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700178 usage = inUsage;
179 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800180 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700181 }
182 return err;
183}
184
Chia-I Wub42f1712017-03-21 13:15:39 -0700185status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
186 HandleWrapMethod method, uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700187 PixelFormat format, uint32_t layerCount, uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -0700188 uint32_t stride)
189{
Chia-I Wub42f1712017-03-21 13:15:39 -0700190 ANativeWindowBuffer::width = static_cast<int>(width);
191 ANativeWindowBuffer::height = static_cast<int>(height);
192 ANativeWindowBuffer::stride = static_cast<int>(stride);
193 ANativeWindowBuffer::format = format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700194 ANativeWindowBuffer::usage = usage;
195 ANativeWindowBuffer::usage_deprecated = int(usage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700196
197 ANativeWindowBuffer::layerCount = layerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700198
199 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
200
Chia-I Wucb8405e2017-04-17 15:20:19 -0700201 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
202 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700203 status_t err = mBufferMapper.importBuffer(handle, width, height,
204 layerCount, format, usage, stride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700205 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700206 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700207
208 return err;
209 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700210
211 if (method == TAKE_UNREGISTERED_HANDLE) {
212 native_handle_close(handle);
213 native_handle_delete(const_cast<native_handle_t*>(handle));
214 }
215
216 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700217 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700218 }
219
Chia-I Wucb8405e2017-04-17 15:20:19 -0700220 ANativeWindowBuffer::handle = handle;
221
Chia-I Wub42f1712017-03-21 13:15:39 -0700222 return NO_ERROR;
223}
224
Dan Stozad3182402014-11-17 12:03:59 -0800225status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700226{
227 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800228 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700229 return res;
230}
231
Dan Stozad3182402014-11-17 12:03:59 -0800232status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700233{
Dan Stozad3182402014-11-17 12:03:59 -0800234 if (rect.left < 0 || rect.right > width ||
235 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000236 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800237 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800238 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700239 return BAD_VALUE;
240 }
Dan Stozad3182402014-11-17 12:03:59 -0800241 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700242 return res;
243}
244
Dan Stozad3182402014-11-17 12:03:59 -0800245status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700246{
247 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800248 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700249 return res;
250}
251
Dan Stozad3182402014-11-17 12:03:59 -0800252status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
253 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700254{
Dan Stozad3182402014-11-17 12:03:59 -0800255 if (rect.left < 0 || rect.right > width ||
256 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700257 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
258 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800259 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700260 return BAD_VALUE;
261 }
Dan Stozad3182402014-11-17 12:03:59 -0800262 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700263 return res;
264}
265
Mathias Agopian3330b202009-10-05 17:07:12 -0700266status_t GraphicBuffer::unlock()
267{
268 status_t res = getBufferMapper().unlock(handle);
269 return res;
270}
271
Dan Stozad3182402014-11-17 12:03:59 -0800272status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300273{
274 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800275 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300276 return res;
277}
278
Dan Stozad3182402014-11-17 12:03:59 -0800279status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
280 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300281{
Craig Donnere96a3252017-02-02 12:13:34 -0800282 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
283}
284
285status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
286 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
287{
Dan Stozad3182402014-11-17 12:03:59 -0800288 if (rect.left < 0 || rect.right > width ||
289 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300290 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
291 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800292 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300293 return BAD_VALUE;
294 }
Craig Donnere96a3252017-02-02 12:13:34 -0800295 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
296 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300297 return res;
298}
299
Dan Stozad3182402014-11-17 12:03:59 -0800300status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
301 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300302{
303 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800304 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300305 return res;
306}
307
Dan Stozad3182402014-11-17 12:03:59 -0800308status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
309 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300310{
Dan Stozad3182402014-11-17 12:03:59 -0800311 if (rect.left < 0 || rect.right > width ||
312 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300313 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
314 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800315 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300316 return BAD_VALUE;
317 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700318 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300319 return res;
320}
321
322status_t GraphicBuffer::unlockAsync(int *fenceFd)
323{
324 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
325 return res;
326}
327
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800328size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700329 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800330}
Mathias Agopian3330b202009-10-05 17:07:12 -0700331
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800332size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700333 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800334}
335
Mathias Agopiane1424282013-07-29 21:24:40 -0700336status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800337 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
338 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700339
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800340 size_t fdCountNeeded = GraphicBuffer::getFdCount();
341 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700342
Dan Stozab1363d32014-03-28 15:10:52 -0700343 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700344 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800345 buf[1] = width;
346 buf[2] = height;
347 buf[3] = stride;
348 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700349 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700350 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700351 buf[7] = static_cast<int32_t>(mId >> 32);
352 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
353 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700354 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700355 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700356 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357
358 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700359 buf[10] = int32_t(mTransportNumFds);
360 buf[11] = int32_t(mTransportNumInts);
361 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700362 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700363 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700364 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800365
Dan Stozaeea6d682015-04-20 12:07:13 -0700366 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700367 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700368 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700369 fds += mTransportNumFds;
370 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700371 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700372
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800373 return NO_ERROR;
374}
375
Mathias Agopiane1424282013-07-29 21:24:40 -0700376status_t GraphicBuffer::unflatten(
377 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Chia-I Wubf8d7212018-10-09 15:22:46 -0700378 if (size < 12 * sizeof(int)) {
379 android_errorWriteLog(0x534e4554, "114223584");
380 return NO_MEMORY;
381 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800382
383 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700384
385 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
386 // see H2BGraphicBufferProducer.cpp
387 uint32_t flattenWordCount = 0;
388 if (buf[0] == 'GB01') {
389 // new version with 64-bits usage bits
390 flattenWordCount = 13;
391 } else if (buf[0] == 'GBFR') {
392 // old version, when usage bits were 32-bits
393 flattenWordCount = 12;
394 } else {
395 return BAD_TYPE;
396 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800397
Craig Donner6ebc46a2016-10-21 15:23:44 -0700398 const size_t numFds = static_cast<size_t>(buf[10]);
399 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800400
Michael Lentinec168b8a2015-02-18 10:14:18 -0800401 // Limit the maxNumber to be relatively small. The number of fds or ints
402 // should not come close to this number, and the number itself was simply
403 // chosen to be high enough to not cause issues and low enough to prevent
404 // overflow problems.
405 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700406 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
407 width = height = stride = format = usage_deprecated = 0;
408 layerCount = 0;
409 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700410 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700411 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700412 return BAD_VALUE;
413 }
414
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700415 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800416 if (size < sizeNeeded) return NO_MEMORY;
417
Michael Lentine38803262014-10-31 15:25:03 -0700418 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800419 if (count < fdCountNeeded) return NO_MEMORY;
420
421 if (handle) {
422 // free previous handle if any
423 free_handle();
424 }
425
426 if (numFds || numInts) {
427 width = buf[1];
428 height = buf[2];
429 stride = buf[3];
430 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700431 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700432 usage_deprecated = buf[6];
433 if (flattenWordCount == 13) {
434 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
435 } else {
436 usage = uint64_t(usage_deprecated);
437 }
Dan Stozad3182402014-11-17 12:03:59 -0800438 native_handle* h = native_handle_create(
439 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700440 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700441 width = height = stride = format = usage_deprecated = 0;
442 layerCount = 0;
443 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700444 handle = NULL;
445 ALOGE("unflatten: native_handle_create failed");
446 return NO_MEMORY;
447 }
Dan Stozad3182402014-11-17 12:03:59 -0800448 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700449 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800450 handle = h;
451 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700452 width = height = stride = format = usage_deprecated = 0;
453 layerCount = 0;
454 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800455 handle = NULL;
456 }
457
Craig Donner6ebc46a2016-10-21 15:23:44 -0700458 mId = static_cast<uint64_t>(buf[7]) << 32;
459 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700460
Craig Donner6ebc46a2016-10-21 15:23:44 -0700461 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700462
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800463 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700464
465 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700466 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700467 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
468 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700469 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700470 width = height = stride = format = usage_deprecated = 0;
471 layerCount = 0;
472 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800473 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700474 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700475 return err;
476 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700477
478 native_handle_close(handle);
479 native_handle_delete(const_cast<native_handle_t*>(handle));
480 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700481 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700482 }
483
Dan Stozaeea6d682015-04-20 12:07:13 -0700484 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700485 size -= sizeNeeded;
486 fds += numFds;
487 count -= numFds;
488
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800489 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700490}
491
Mathias Agopian3330b202009-10-05 17:07:12 -0700492// ---------------------------------------------------------------------------
493
494}; // namespace android