blob: c8805000a48c7ea82a015ba08653ef9d4fab9c7e [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{
Colin Cross18fae752014-07-22 15:55:08 -0700125 LOG_ALWAYS_FATAL_IF(this == NULL, "getNativeBuffer() called on NULL GraphicBuffer");
Iliyan Malchev697526b2011-05-01 11:33:26 -0700126 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700127 const_cast<GraphicBuffer*>(this));
128}
129
Dan Stozad3182402014-11-17 12:03:59 -0800130status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700131 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700132{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700133 if (mOwner != ownData)
134 return INVALID_OPERATION;
135
Dan Stozad3182402014-11-17 12:03:59 -0800136 if (handle &&
137 static_cast<int>(inWidth) == width &&
138 static_cast<int>(inHeight) == height &&
139 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700140 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700141 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700142 return NO_ERROR;
143
Mathias Agopian3330b202009-10-05 17:07:12 -0700144 if (handle) {
145 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
146 allocator.free(handle);
147 handle = 0;
148 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700149 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700150}
151
Dan Stoza9de72932015-04-16 17:28:43 -0700152bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700153 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700154{
155 if (static_cast<int>(inWidth) != width) return true;
156 if (static_cast<int>(inHeight) != height) return true;
157 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700158 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700159 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700160 return false;
161}
162
Chia-I Wub42f1712017-03-21 13:15:39 -0700163status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700164 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
165 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700166{
Mathias Agopian3330b202009-10-05 17:07:12 -0700167 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800168 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700169 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700170 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800171 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700172 if (err == NO_ERROR) {
Dan Stozad3182402014-11-17 12:03:59 -0800173 width = static_cast<int>(inWidth);
174 height = static_cast<int>(inHeight);
175 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700176 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700177 usage = inUsage;
178 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800179 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700180 }
181 return err;
182}
183
Chia-I Wub42f1712017-03-21 13:15:39 -0700184status_t GraphicBuffer::initWithHandle(const native_handle_t* handle,
185 HandleWrapMethod method, uint32_t width, uint32_t height,
Chris Forbes82c04982017-04-19 14:29:54 -0700186 PixelFormat format, uint32_t layerCount, uint64_t usage,
Chia-I Wub42f1712017-03-21 13:15:39 -0700187 uint32_t stride)
188{
Chia-I Wub42f1712017-03-21 13:15:39 -0700189 ANativeWindowBuffer::width = static_cast<int>(width);
190 ANativeWindowBuffer::height = static_cast<int>(height);
191 ANativeWindowBuffer::stride = static_cast<int>(stride);
192 ANativeWindowBuffer::format = format;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700193 ANativeWindowBuffer::usage = usage;
194 ANativeWindowBuffer::usage_deprecated = int(usage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700195
196 ANativeWindowBuffer::layerCount = layerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700197
198 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
199
Chia-I Wucb8405e2017-04-17 15:20:19 -0700200 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
201 buffer_handle_t importedHandle;
202 status_t err = mBufferMapper.importBuffer(handle, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700203 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700204 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700205
206 return err;
207 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700208
209 if (method == TAKE_UNREGISTERED_HANDLE) {
210 native_handle_close(handle);
211 native_handle_delete(const_cast<native_handle_t*>(handle));
212 }
213
214 handle = importedHandle;
Chia-I Wub42f1712017-03-21 13:15:39 -0700215 }
216
Chia-I Wucb8405e2017-04-17 15:20:19 -0700217 ANativeWindowBuffer::handle = handle;
218
Chia-I Wub42f1712017-03-21 13:15:39 -0700219 return NO_ERROR;
220}
221
Dan Stozad3182402014-11-17 12:03:59 -0800222status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700223{
224 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800225 status_t res = lock(inUsage, lockBounds, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700226 return res;
227}
228
Dan Stozad3182402014-11-17 12:03:59 -0800229status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
Mathias Agopian3330b202009-10-05 17:07:12 -0700230{
Dan Stozad3182402014-11-17 12:03:59 -0800231 if (rect.left < 0 || rect.right > width ||
232 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000233 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800234 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800235 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700236 return BAD_VALUE;
237 }
Dan Stozad3182402014-11-17 12:03:59 -0800238 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700239 return res;
240}
241
Dan Stozad3182402014-11-17 12:03:59 -0800242status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700243{
244 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800245 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700246 return res;
247}
248
Dan Stozad3182402014-11-17 12:03:59 -0800249status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
250 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700251{
Dan Stozad3182402014-11-17 12:03:59 -0800252 if (rect.left < 0 || rect.right > width ||
253 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700254 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
255 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800256 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700257 return BAD_VALUE;
258 }
Dan Stozad3182402014-11-17 12:03:59 -0800259 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700260 return res;
261}
262
Mathias Agopian3330b202009-10-05 17:07:12 -0700263status_t GraphicBuffer::unlock()
264{
265 status_t res = getBufferMapper().unlock(handle);
266 return res;
267}
268
Dan Stozad3182402014-11-17 12:03:59 -0800269status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300270{
271 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800272 status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300273 return res;
274}
275
Dan Stozad3182402014-11-17 12:03:59 -0800276status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
277 void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300278{
Craig Donnere96a3252017-02-02 12:13:34 -0800279 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd);
280}
281
282status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage,
283 uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd)
284{
Dan Stozad3182402014-11-17 12:03:59 -0800285 if (rect.left < 0 || rect.right > width ||
286 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300287 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
288 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800289 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300290 return BAD_VALUE;
291 }
Craig Donnere96a3252017-02-02 12:13:34 -0800292 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage,
293 inConsumerUsage, rect, vaddr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300294 return res;
295}
296
Dan Stozad3182402014-11-17 12:03:59 -0800297status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
298 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300299{
300 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800301 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300302 return res;
303}
304
Dan Stozad3182402014-11-17 12:03:59 -0800305status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
306 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300307{
Dan Stozad3182402014-11-17 12:03:59 -0800308 if (rect.left < 0 || rect.right > width ||
309 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300310 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
311 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800312 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300313 return BAD_VALUE;
314 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700315 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300316 return res;
317}
318
319status_t GraphicBuffer::unlockAsync(int *fenceFd)
320{
321 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
322 return res;
323}
324
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800325size_t GraphicBuffer::getFlattenedSize() const {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700326 return static_cast<size_t>(13 + (handle ? handle->numInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800327}
Mathias Agopian3330b202009-10-05 17:07:12 -0700328
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800329size_t GraphicBuffer::getFdCount() const {
Dan Stozad3182402014-11-17 12:03:59 -0800330 return static_cast<size_t>(handle ? handle->numFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800331}
332
Mathias Agopiane1424282013-07-29 21:24:40 -0700333status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800334 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
335 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700336
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800337 size_t fdCountNeeded = GraphicBuffer::getFdCount();
338 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700339
Dan Stozab1363d32014-03-28 15:10:52 -0700340 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700341 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800342 buf[1] = width;
343 buf[2] = height;
344 buf[3] = stride;
345 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700346 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700347 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700348 buf[7] = static_cast<int32_t>(mId >> 32);
349 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
350 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700351 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700352 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700353 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800354
355 if (handle) {
Craig Donner6ebc46a2016-10-21 15:23:44 -0700356 buf[10] = handle->numFds;
357 buf[11] = handle->numInts;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700358 memcpy(fds, handle->data, static_cast<size_t>(handle->numFds) * sizeof(int));
359 memcpy(buf + 13, handle->data + handle->numFds,
Dan Stozad3182402014-11-17 12:03:59 -0800360 static_cast<size_t>(handle->numInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700361 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800362
Dan Stozaeea6d682015-04-20 12:07:13 -0700363 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700364 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700365 if (handle) {
366 fds += handle->numFds;
Dan Stozad3182402014-11-17 12:03:59 -0800367 count -= static_cast<size_t>(handle->numFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700368 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700369
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800370 return NO_ERROR;
371}
372
Mathias Agopiane1424282013-07-29 21:24:40 -0700373status_t GraphicBuffer::unflatten(
374 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800375
376 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700377
378 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
379 // see H2BGraphicBufferProducer.cpp
380 uint32_t flattenWordCount = 0;
381 if (buf[0] == 'GB01') {
382 // new version with 64-bits usage bits
383 flattenWordCount = 13;
384 } else if (buf[0] == 'GBFR') {
385 // old version, when usage bits were 32-bits
386 flattenWordCount = 12;
387 } else {
388 return BAD_TYPE;
389 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800390
Craig Donner6ebc46a2016-10-21 15:23:44 -0700391 const size_t numFds = static_cast<size_t>(buf[10]);
392 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800393
Michael Lentinec168b8a2015-02-18 10:14:18 -0800394 // Limit the maxNumber to be relatively small. The number of fds or ints
395 // should not come close to this number, and the number itself was simply
396 // chosen to be high enough to not cause issues and low enough to prevent
397 // overflow problems.
398 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700399 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
400 width = height = stride = format = usage_deprecated = 0;
401 layerCount = 0;
402 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700403 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700404 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700405 return BAD_VALUE;
406 }
407
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700408 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800409 if (size < sizeNeeded) return NO_MEMORY;
410
Michael Lentine38803262014-10-31 15:25:03 -0700411 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800412 if (count < fdCountNeeded) return NO_MEMORY;
413
414 if (handle) {
415 // free previous handle if any
416 free_handle();
417 }
418
419 if (numFds || numInts) {
420 width = buf[1];
421 height = buf[2];
422 stride = buf[3];
423 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700424 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700425 usage_deprecated = buf[6];
426 if (flattenWordCount == 13) {
427 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
428 } else {
429 usage = uint64_t(usage_deprecated);
430 }
Dan Stozad3182402014-11-17 12:03:59 -0800431 native_handle* h = native_handle_create(
432 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700433 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700434 width = height = stride = format = usage_deprecated = 0;
435 layerCount = 0;
436 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700437 handle = NULL;
438 ALOGE("unflatten: native_handle_create failed");
439 return NO_MEMORY;
440 }
Dan Stozad3182402014-11-17 12:03:59 -0800441 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700442 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800443 handle = h;
444 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700445 width = height = stride = format = usage_deprecated = 0;
446 layerCount = 0;
447 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800448 handle = NULL;
449 }
450
Craig Donner6ebc46a2016-10-21 15:23:44 -0700451 mId = static_cast<uint64_t>(buf[7]) << 32;
452 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700453
Craig Donner6ebc46a2016-10-21 15:23:44 -0700454 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700455
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800456 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700457
458 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700459 buffer_handle_t importedHandle;
460 status_t err = mBufferMapper.importBuffer(handle, &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700461 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700462 width = height = stride = format = usage_deprecated = 0;
463 layerCount = 0;
464 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800465 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700466 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700467 return err;
468 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700469
470 native_handle_close(handle);
471 native_handle_delete(const_cast<native_handle_t*>(handle));
472 handle = importedHandle;
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
Mathias Agopian3330b202009-10-05 17:07:12 -0700483// ---------------------------------------------------------------------------
484
485}; // namespace android