blob: 79958ece9e54b942b82b20dacb5f1d332a715a25 [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"
Alec Mouri6338c9d2019-02-07 16:57:51 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopian98e71dd2010-02-11 17:30:52 -080019
Mathias Agopian3330b202009-10-05 17:07:12 -070020#include <ui/GraphicBuffer.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080021
22#include <cutils/atomic.h>
23
Jesse Hall79927812017-03-23 11:03:23 -070024#include <grallocusage/GrallocUsageConversion.h>
25
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070026#ifndef LIBUI_IN_VNDK
27#include <ui/BufferHubBuffer.h>
28#endif // LIBUI_IN_VNDK
29
Chia-I Wu5bac7f32017-04-06 12:34:32 -070030#include <ui/Gralloc2.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070031#include <ui/GraphicBufferAllocator.h>
32#include <ui/GraphicBufferMapper.h>
Alec Mouri6338c9d2019-02-07 16:57:51 -080033#include <utils/Trace.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070034
Mathias Agopian3330b202009-10-05 17:07:12 -070035namespace android {
36
37// ===========================================================================
Iliyan Malchev697526b2011-05-01 11:33:26 -070038// Buffer and implementation of ANativeWindowBuffer
Mathias Agopian3330b202009-10-05 17:07:12 -070039// ===========================================================================
40
Dan Stozab1363d32014-03-28 15:10:52 -070041static uint64_t getUniqueId() {
42 static volatile int32_t nextId = 0;
43 uint64_t id = static_cast<uint64_t>(getpid()) << 32;
44 id |= static_cast<uint32_t>(android_atomic_inc(&nextId));
45 return id;
46}
47
Mathias Agopianf543e5a2017-04-03 17:16:41 -070048sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) {
49 return static_cast<GraphicBuffer *>(anwb);
50}
51
Mathias Agopian3330b202009-10-05 17:07:12 -070052GraphicBuffer::GraphicBuffer()
Mathias Agopian54ba51d2009-10-26 20:12:37 -070053 : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
Pablo Ceballos53390e12015-08-04 11:25:59 -070054 mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0)
Dan Stozab1363d32014-03-28 15:10:52 -070055{
Dan Stoza01049c82014-11-11 10:32:31 -080056 width =
57 height =
58 stride =
59 format =
Mathias Agopiancb496ac2017-05-22 14:21:00 -070060 usage_deprecated = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070061 usage = 0;
Mathias Agopian841abed2017-02-10 16:15:34 -080062 layerCount = 0;
Mathias Agopian3330b202009-10-05 17:07:12 -070063 handle = NULL;
64}
65
Chia-I Wub42f1712017-03-21 13:15:39 -070066// deprecated
Dan Stozad3182402014-11-17 12:03:59 -080067GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
Dan Stoza024e9312016-08-24 12:17:29 -070068 PixelFormat inFormat, uint32_t inUsage, std::string requestorName)
Mathias Agopiancb496ac2017-05-22 14:21:00 -070069 : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -070070{
Mathias Agopian3330b202009-10-05 17:07:12 -070071}
72
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080073GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
74 uint32_t inLayerCount, uint64_t inUsage, std::string requestorName)
75 : GraphicBuffer() {
76 mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage,
77 std::move(requestorName));
Craig Donner6ebc46a2016-10-21 15:23:44 -070078}
79
Chia-I Wub42f1712017-03-21 13:15:39 -070080// deprecated
Craig Donner6ebc46a2016-10-21 15:23:44 -070081GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
82 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
83 uint32_t inStride, native_handle_t* inHandle, bool keepOwnership)
Chia-I Wub42f1712017-03-21 13:15:39 -070084 : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE,
Chris Forbes82c04982017-04-19 14:29:54 -070085 inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage),
Chia-I Wub42f1712017-03-21 13:15:39 -070086 inStride)
Mathias Agopian54ba51d2009-10-26 20:12:37 -070087{
Mathias Agopian54ba51d2009-10-26 20:12:37 -070088}
89
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -080090GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method,
91 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
92 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride)
93 : GraphicBuffer() {
94 mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
95 inUsage, inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -070096}
97
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070098#ifndef LIBUI_IN_VNDK
99GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() {
100 if (buffer == nullptr) {
101 mInitCheck = BAD_VALUE;
102 return;
103 }
104
Tianyu Jiang727ede42019-02-01 11:44:51 -0800105 mInitCheck = initWithHandle(buffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE,
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700106 buffer->desc().width, buffer->desc().height,
107 static_cast<PixelFormat>(buffer->desc().format),
108 buffer->desc().layers, buffer->desc().usage, buffer->desc().stride);
Jiwen 'Steve' Cai29160fb2018-12-13 12:03:28 -0800109 mBufferId = buffer->id();
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700110 mBufferHubBuffer = std::move(buffer);
111}
112#endif // LIBUI_IN_VNDK
113
Mathias Agopian3330b202009-10-05 17:07:12 -0700114GraphicBuffer::~GraphicBuffer()
115{
Alec Mouri6338c9d2019-02-07 16:57:51 -0800116 ATRACE_CALL();
Mathias Agopian3330b202009-10-05 17:07:12 -0700117 if (handle) {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800118 free_handle();
119 }
120}
121
122void GraphicBuffer::free_handle()
123{
124 if (mOwner == ownHandle) {
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700125 mBufferMapper.freeBuffer(handle);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800126 } else if (mOwner == ownData) {
127 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
128 allocator.free(handle);
Mathias Agopian3330b202009-10-05 17:07:12 -0700129 }
Praveen Chavan22e4cc32015-09-16 11:20:00 -0700130 handle = NULL;
Mathias Agopian3330b202009-10-05 17:07:12 -0700131}
132
133status_t GraphicBuffer::initCheck() const {
Dan Stoza133caac2014-12-01 15:15:31 -0800134 return static_cast<status_t>(mInitCheck);
Mathias Agopian3330b202009-10-05 17:07:12 -0700135}
136
Mathias Agopian678bdd62010-12-03 17:33:09 -0800137void GraphicBuffer::dumpAllocationsToSystemLog()
138{
139 GraphicBufferAllocator::dumpToSystemLog();
140}
141
Iliyan Malchev697526b2011-05-01 11:33:26 -0700142ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const
Mathias Agopian3330b202009-10-05 17:07:12 -0700143{
Iliyan Malchev697526b2011-05-01 11:33:26 -0700144 return static_cast<ANativeWindowBuffer*>(
Mathias Agopian3330b202009-10-05 17:07:12 -0700145 const_cast<GraphicBuffer*>(this));
146}
147
Dan Stozad3182402014-11-17 12:03:59 -0800148status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700149 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Mathias Agopian3330b202009-10-05 17:07:12 -0700150{
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700151 if (mOwner != ownData)
152 return INVALID_OPERATION;
153
Dan Stozad3182402014-11-17 12:03:59 -0800154 if (handle &&
155 static_cast<int>(inWidth) == width &&
156 static_cast<int>(inHeight) == height &&
157 inFormat == format &&
Craig Donner6ebc46a2016-10-21 15:23:44 -0700158 inLayerCount == layerCount &&
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700159 inUsage == usage)
Mathias Agopian579b3f82010-06-08 19:54:15 -0700160 return NO_ERROR;
161
Mathias Agopian3330b202009-10-05 17:07:12 -0700162 if (handle) {
163 GraphicBufferAllocator& allocator(GraphicBufferAllocator::get());
164 allocator.free(handle);
165 handle = 0;
166 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700167 return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]");
Mathias Agopian3330b202009-10-05 17:07:12 -0700168}
169
Dan Stoza9de72932015-04-16 17:28:43 -0700170bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700171 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage)
Dan Stoza9de72932015-04-16 17:28:43 -0700172{
173 if (static_cast<int>(inWidth) != width) return true;
174 if (static_cast<int>(inHeight) != height) return true;
175 if (inFormat != format) return true;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700176 if (inLayerCount != layerCount) return true;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700177 if ((usage & inUsage) != inUsage) return true;
Dan Stoza9de72932015-04-16 17:28:43 -0700178 return false;
179}
180
Chia-I Wub42f1712017-03-21 13:15:39 -0700181status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700182 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
183 std::string requestorName)
Mathias Agopian3330b202009-10-05 17:07:12 -0700184{
Mathias Agopian3330b202009-10-05 17:07:12 -0700185 GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
Dan Stozad3182402014-11-17 12:03:59 -0800186 uint32_t outStride = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700187 status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700188 inUsage, &handle, &outStride, mId,
Craig Donnere96a3252017-02-02 12:13:34 -0800189 std::move(requestorName));
Mathias Agopian3330b202009-10-05 17:07:12 -0700190 if (err == NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700191 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
192
Dan Stozad3182402014-11-17 12:03:59 -0800193 width = static_cast<int>(inWidth);
194 height = static_cast<int>(inHeight);
195 format = inFormat;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700196 layerCount = inLayerCount;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700197 usage = inUsage;
198 usage_deprecated = int(usage);
Dan Stozad3182402014-11-17 12:03:59 -0800199 stride = static_cast<int>(outStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700200 }
201 return err;
202}
203
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800204status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
205 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
206 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) {
207 ANativeWindowBuffer::width = static_cast<int>(inWidth);
208 ANativeWindowBuffer::height = static_cast<int>(inHeight);
209 ANativeWindowBuffer::stride = static_cast<int>(inStride);
210 ANativeWindowBuffer::format = inFormat;
211 ANativeWindowBuffer::usage = inUsage;
212 ANativeWindowBuffer::usage_deprecated = int(inUsage);
Chia-I Wub42f1712017-03-21 13:15:39 -0700213
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800214 ANativeWindowBuffer::layerCount = inLayerCount;
Chia-I Wub42f1712017-03-21 13:15:39 -0700215
216 mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle;
217
Chia-I Wucb8405e2017-04-17 15:20:19 -0700218 if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) {
219 buffer_handle_t importedHandle;
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800220 status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount,
221 inFormat, inUsage, inStride, &importedHandle);
Chia-I Wub42f1712017-03-21 13:15:39 -0700222 if (err != NO_ERROR) {
Chris Forbes82c04982017-04-19 14:29:54 -0700223 initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0);
Chia-I Wub42f1712017-03-21 13:15:39 -0700224
225 return err;
226 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700227
228 if (method == TAKE_UNREGISTERED_HANDLE) {
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800229 native_handle_close(inHandle);
230 native_handle_delete(const_cast<native_handle_t*>(inHandle));
Chia-I Wucb8405e2017-04-17 15:20:19 -0700231 }
232
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800233 inHandle = importedHandle;
234 mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts);
Chia-I Wub42f1712017-03-21 13:15:39 -0700235 }
236
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800237 ANativeWindowBuffer::handle = inHandle;
Chia-I Wucb8405e2017-04-17 15:20:19 -0700238
Chia-I Wub42f1712017-03-21 13:15:39 -0700239 return NO_ERROR;
240}
241
Valerie Hau250c6542019-01-31 14:23:43 -0800242status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel,
243 int32_t* outBytesPerStride) {
Mathias Agopian3330b202009-10-05 17:07:12 -0700244 const Rect lockBounds(width, height);
Valerie Hau250c6542019-01-31 14:23:43 -0800245 status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700246 return res;
247}
248
Valerie Hau250c6542019-01-31 14:23:43 -0800249status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr,
250 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Dan Stozad3182402014-11-17 12:03:59 -0800251 if (rect.left < 0 || rect.right > width ||
252 rect.top < 0 || rect.bottom > height) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000253 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
Dan Stoza01049c82014-11-11 10:32:31 -0800254 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800255 width, height);
Mathias Agopian3330b202009-10-05 17:07:12 -0700256 return BAD_VALUE;
257 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800258
Valerie Hau250c6542019-01-31 14:23:43 -0800259 status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel,
260 outBytesPerStride);
261
Mathias Agopian3330b202009-10-05 17:07:12 -0700262 return res;
263}
264
Dan Stozad3182402014-11-17 12:03:59 -0800265status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700266{
267 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800268 status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700269 return res;
270}
271
Dan Stozad3182402014-11-17 12:03:59 -0800272status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
273 android_ycbcr* ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700274{
Dan Stozad3182402014-11-17 12:03:59 -0800275 if (rect.left < 0 || rect.right > width ||
276 rect.top < 0 || rect.bottom > height) {
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700277 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
278 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800279 width, height);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700280 return BAD_VALUE;
281 }
Dan Stozad3182402014-11-17 12:03:59 -0800282 status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700283 return res;
284}
285
Mathias Agopian3330b202009-10-05 17:07:12 -0700286status_t GraphicBuffer::unlock()
287{
288 status_t res = getBufferMapper().unlock(handle);
289 return res;
290}
291
Valerie Haub94adfd2019-02-07 14:25:12 -0800292status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
293 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300294 const Rect lockBounds(width, height);
Valerie Haub94adfd2019-02-07 14:25:12 -0800295 status_t res =
296 lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Francis Hart8f396012014-04-01 15:30:53 +0300297 return res;
298}
299
Valerie Haub94adfd2019-02-07 14:25:12 -0800300status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
301 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
302 return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800303}
304
Valerie Haub94adfd2019-02-07 14:25:12 -0800305status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
306 const Rect& rect, void** vaddr, int fenceFd,
307 int32_t* outBytesPerPixel, int32_t* outBytesPerStride) {
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 }
Valerie Hau0c9fc362019-01-22 09:17:19 -0800315
Valerie Hau0c9fc362019-01-22 09:17:19 -0800316 status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect,
Valerie Haub94adfd2019-02-07 14:25:12 -0800317 vaddr, fenceFd, outBytesPerPixel, outBytesPerStride);
318
Francis Hart8f396012014-04-01 15:30:53 +0300319 return res;
320}
321
Dan Stozad3182402014-11-17 12:03:59 -0800322status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
323 int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300324{
325 const Rect lockBounds(width, height);
Dan Stozad3182402014-11-17 12:03:59 -0800326 status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300327 return res;
328}
329
Dan Stozad3182402014-11-17 12:03:59 -0800330status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
331 android_ycbcr* ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300332{
Dan Stozad3182402014-11-17 12:03:59 -0800333 if (rect.left < 0 || rect.right > width ||
334 rect.top < 0 || rect.bottom > height) {
Francis Hart8f396012014-04-01 15:30:53 +0300335 ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
336 rect.left, rect.top, rect.right, rect.bottom,
Dan Stozad3182402014-11-17 12:03:59 -0800337 width, height);
Francis Hart8f396012014-04-01 15:30:53 +0300338 return BAD_VALUE;
339 }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700340 status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300341 return res;
342}
343
344status_t GraphicBuffer::unlockAsync(int *fenceFd)
345{
346 status_t res = getBufferMapper().unlockAsync(handle, fenceFd);
347 return res;
348}
349
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800350status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
351 uint32_t inLayerCount, uint64_t inUsage,
352 bool* outSupported) const {
353 return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage,
354 outSupported);
355}
356
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800357size_t GraphicBuffer::getFlattenedSize() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700358 return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800359}
Mathias Agopian3330b202009-10-05 17:07:12 -0700360
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800361size_t GraphicBuffer::getFdCount() const {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700362 return static_cast<size_t>(handle ? mTransportNumFds : 0);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800363}
364
Mathias Agopiane1424282013-07-29 21:24:40 -0700365status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800366 size_t sizeNeeded = GraphicBuffer::getFlattenedSize();
367 if (size < sizeNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700368
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800369 size_t fdCountNeeded = GraphicBuffer::getFdCount();
370 if (count < fdCountNeeded) return NO_MEMORY;
Mathias Agopian3330b202009-10-05 17:07:12 -0700371
Dan Stozab1363d32014-03-28 15:10:52 -0700372 int32_t* buf = static_cast<int32_t*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700373 buf[0] = 'GB01';
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800374 buf[1] = width;
375 buf[2] = height;
376 buf[3] = stride;
377 buf[4] = format;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700378 buf[5] = static_cast<int32_t>(layerCount);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700379 buf[6] = int(usage); // low 32-bits
Craig Donner6ebc46a2016-10-21 15:23:44 -0700380 buf[7] = static_cast<int32_t>(mId >> 32);
381 buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull);
382 buf[9] = static_cast<int32_t>(mGenerationNumber);
Dan Stoza812ed062015-06-02 15:45:22 -0700383 buf[10] = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -0700384 buf[11] = 0;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700385 buf[12] = int(usage >> 32); // high 32-bits
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800386
387 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700388 buf[10] = int32_t(mTransportNumFds);
389 buf[11] = int32_t(mTransportNumInts);
390 memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700391 memcpy(buf + 13, handle->data + handle->numFds,
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700392 static_cast<size_t>(mTransportNumInts) * sizeof(int));
Mathias Agopian3330b202009-10-05 17:07:12 -0700393 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800394
Dan Stozaeea6d682015-04-20 12:07:13 -0700395 buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700396 size -= sizeNeeded;
Andy McFaddenbc96e472014-03-17 16:48:23 -0700397 if (handle) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700398 fds += mTransportNumFds;
399 count -= static_cast<size_t>(mTransportNumFds);
Andy McFaddenbc96e472014-03-17 16:48:23 -0700400 }
Mathias Agopiane1424282013-07-29 21:24:40 -0700401
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800402 return NO_ERROR;
403}
404
Mathias Agopiane1424282013-07-29 21:24:40 -0700405status_t GraphicBuffer::unflatten(
406 void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Chia-I Wubf8d7212018-10-09 15:22:46 -0700407 if (size < 12 * sizeof(int)) {
408 android_errorWriteLog(0x534e4554, "114223584");
409 return NO_MEMORY;
410 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800411
412 int const* buf = static_cast<int const*>(buffer);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700413
414 // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!!
415 // see H2BGraphicBufferProducer.cpp
416 uint32_t flattenWordCount = 0;
417 if (buf[0] == 'GB01') {
418 // new version with 64-bits usage bits
419 flattenWordCount = 13;
420 } else if (buf[0] == 'GBFR') {
421 // old version, when usage bits were 32-bits
422 flattenWordCount = 12;
423 } else {
424 return BAD_TYPE;
425 }
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800426
Craig Donner6ebc46a2016-10-21 15:23:44 -0700427 const size_t numFds = static_cast<size_t>(buf[10]);
428 const size_t numInts = static_cast<size_t>(buf[11]);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800429
Michael Lentinec168b8a2015-02-18 10:14:18 -0800430 // Limit the maxNumber to be relatively small. The number of fds or ints
431 // should not come close to this number, and the number itself was simply
432 // chosen to be high enough to not cause issues and low enough to prevent
433 // overflow problems.
434 const size_t maxNumber = 4096;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700435 if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) {
436 width = height = stride = format = usage_deprecated = 0;
437 layerCount = 0;
438 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700439 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700440 ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts);
Michael Lentine38803262014-10-31 15:25:03 -0700441 return BAD_VALUE;
442 }
443
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700444 const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int);
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800445 if (size < sizeNeeded) return NO_MEMORY;
446
Michael Lentine38803262014-10-31 15:25:03 -0700447 size_t fdCountNeeded = numFds;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800448 if (count < fdCountNeeded) return NO_MEMORY;
449
450 if (handle) {
451 // free previous handle if any
452 free_handle();
453 }
454
455 if (numFds || numInts) {
456 width = buf[1];
457 height = buf[2];
458 stride = buf[3];
459 format = buf[4];
Craig Donner6ebc46a2016-10-21 15:23:44 -0700460 layerCount = static_cast<uintptr_t>(buf[5]);
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700461 usage_deprecated = buf[6];
462 if (flattenWordCount == 13) {
463 usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]);
464 } else {
465 usage = uint64_t(usage_deprecated);
466 }
Dan Stozad3182402014-11-17 12:03:59 -0800467 native_handle* h = native_handle_create(
468 static_cast<int>(numFds), static_cast<int>(numInts));
Michael Lentine38803262014-10-31 15:25:03 -0700469 if (!h) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700470 width = height = stride = format = usage_deprecated = 0;
471 layerCount = 0;
472 usage = 0;
Michael Lentine38803262014-10-31 15:25:03 -0700473 handle = NULL;
474 ALOGE("unflatten: native_handle_create failed");
475 return NO_MEMORY;
476 }
Dan Stozad3182402014-11-17 12:03:59 -0800477 memcpy(h->data, fds, numFds * sizeof(int));
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700478 memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int));
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800479 handle = h;
480 } else {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700481 width = height = stride = format = usage_deprecated = 0;
482 layerCount = 0;
483 usage = 0;
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800484 handle = NULL;
485 }
486
Craig Donner6ebc46a2016-10-21 15:23:44 -0700487 mId = static_cast<uint64_t>(buf[7]) << 32;
488 mId |= static_cast<uint32_t>(buf[8]);
Dan Stozab1363d32014-03-28 15:10:52 -0700489
Craig Donner6ebc46a2016-10-21 15:23:44 -0700490 mGenerationNumber = static_cast<uint32_t>(buf[9]);
Dan Stoza812ed062015-06-02 15:45:22 -0700491
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800492 mOwner = ownHandle;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700493
494 if (handle != 0) {
Chia-I Wucb8405e2017-04-17 15:20:19 -0700495 buffer_handle_t importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700496 status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height),
497 uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700498 if (err != NO_ERROR) {
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700499 width = height = stride = format = usage_deprecated = 0;
500 layerCount = 0;
501 usage = 0;
Lingyun Zhu2aff7022012-11-20 19:24:35 +0800502 handle = NULL;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700503 ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err);
Jamie Gennisd69097f2012-08-30 13:28:23 -0700504 return err;
505 }
Chia-I Wucb8405e2017-04-17 15:20:19 -0700506
507 native_handle_close(handle);
508 native_handle_delete(const_cast<native_handle_t*>(handle));
509 handle = importedHandle;
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700510 mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700511 }
512
Dan Stozaeea6d682015-04-20 12:07:13 -0700513 buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded);
Mathias Agopiane1424282013-07-29 21:24:40 -0700514 size -= sizeNeeded;
515 fds += numFds;
516 count -= numFds;
517
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800518 return NO_ERROR;
Mathias Agopian3330b202009-10-05 17:07:12 -0700519}
520
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700521#ifndef LIBUI_IN_VNDK
522bool GraphicBuffer::isBufferHubBuffer() const {
523 return mBufferHubBuffer != nullptr;
524}
525#endif // LIBUI_IN_VNDK
526
Mathias Agopian3330b202009-10-05 17:07:12 -0700527// ---------------------------------------------------------------------------
528
529}; // namespace android