Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 17 | #define LOG_TAG "GraphicBuffer" |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 19 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 20 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 21 | |
| 22 | #include <cutils/atomic.h> |
| 23 | |
Jesse Hall | 7992781 | 2017-03-23 11:03:23 -0700 | [diff] [blame] | 24 | #include <grallocusage/GrallocUsageConversion.h> |
| 25 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 26 | #ifndef LIBUI_IN_VNDK |
| 27 | #include <ui/BufferHubBuffer.h> |
| 28 | #endif // LIBUI_IN_VNDK |
| 29 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 30 | #include <ui/Gralloc2.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 31 | #include <ui/GraphicBufferAllocator.h> |
| 32 | #include <ui/GraphicBufferMapper.h> |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | // =========================================================================== |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 38 | // Buffer and implementation of ANativeWindowBuffer |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 39 | // =========================================================================== |
| 40 | |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 41 | static 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 Agopian | f543e5a | 2017-04-03 17:16:41 -0700 | [diff] [blame] | 48 | sp<GraphicBuffer> GraphicBuffer::from(ANativeWindowBuffer* anwb) { |
| 49 | return static_cast<GraphicBuffer *>(anwb); |
| 50 | } |
| 51 | |
Pawin Vongmasa | e672cd0 | 2019-02-14 16:01:29 -0800 | [diff] [blame] | 52 | GraphicBuffer* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer* buffer) { |
| 53 | return reinterpret_cast<GraphicBuffer*>(buffer); |
| 54 | } |
| 55 | |
| 56 | GraphicBuffer const* GraphicBuffer::fromAHardwareBuffer(AHardwareBuffer const* buffer) { |
| 57 | return reinterpret_cast<GraphicBuffer const*>(buffer); |
| 58 | } |
| 59 | |
| 60 | AHardwareBuffer* GraphicBuffer::toAHardwareBuffer() { |
| 61 | return reinterpret_cast<AHardwareBuffer*>(this); |
| 62 | } |
| 63 | |
| 64 | AHardwareBuffer const* GraphicBuffer::toAHardwareBuffer() const { |
| 65 | return reinterpret_cast<AHardwareBuffer const*>(this); |
| 66 | } |
| 67 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 68 | GraphicBuffer::GraphicBuffer() |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 69 | : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 70 | mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0) |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 71 | { |
Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 72 | width = |
| 73 | height = |
| 74 | stride = |
| 75 | format = |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 76 | usage_deprecated = 0; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 77 | usage = 0; |
Mathias Agopian | 841abed | 2017-02-10 16:15:34 -0800 | [diff] [blame] | 78 | layerCount = 0; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 79 | handle = NULL; |
| 80 | } |
| 81 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 82 | // deprecated |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 83 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, |
Dan Stoza | 024e931 | 2016-08-24 12:17:29 -0700 | [diff] [blame] | 84 | PixelFormat inFormat, uint32_t inUsage, std::string requestorName) |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 85 | : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast<uint64_t>(inUsage), requestorName) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 86 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 89 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 90 | uint32_t inLayerCount, uint64_t inUsage, std::string requestorName) |
| 91 | : GraphicBuffer() { |
| 92 | mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, |
| 93 | std::move(requestorName)); |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 96 | // deprecated |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 97 | GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, |
| 98 | PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage, |
| 99 | uint32_t inStride, native_handle_t* inHandle, bool keepOwnership) |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 100 | : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 101 | inWidth, inHeight, inFormat, inLayerCount, static_cast<uint64_t>(inUsage), |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 102 | inStride) |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 103 | { |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 106 | GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, |
| 107 | uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 108 | uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) |
| 109 | : GraphicBuffer() { |
| 110 | mInitCheck = initWithHandle(inHandle, method, inWidth, inHeight, inFormat, inLayerCount, |
| 111 | inUsage, inStride); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 114 | #ifndef LIBUI_IN_VNDK |
| 115 | GraphicBuffer::GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer) : GraphicBuffer() { |
| 116 | if (buffer == nullptr) { |
| 117 | mInitCheck = BAD_VALUE; |
| 118 | return; |
| 119 | } |
| 120 | |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 121 | mInitCheck = initWithHandle(buffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 122 | buffer->desc().width, buffer->desc().height, |
| 123 | static_cast<PixelFormat>(buffer->desc().format), |
| 124 | buffer->desc().layers, buffer->desc().usage, buffer->desc().stride); |
Jiwen 'Steve' Cai | 29160fb | 2018-12-13 12:03:28 -0800 | [diff] [blame] | 125 | mBufferId = buffer->id(); |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 126 | mBufferHubBuffer = std::move(buffer); |
| 127 | } |
| 128 | #endif // LIBUI_IN_VNDK |
| 129 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 130 | GraphicBuffer::~GraphicBuffer() |
| 131 | { |
Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 132 | ATRACE_CALL(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 133 | if (handle) { |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 134 | free_handle(); |
| 135 | } |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 136 | for (auto& [callback, context] : mDeathCallbacks) { |
| 137 | callback(context, mId); |
| 138 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void GraphicBuffer::free_handle() |
| 142 | { |
| 143 | if (mOwner == ownHandle) { |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 144 | mBufferMapper.freeBuffer(handle); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 145 | } else if (mOwner == ownData) { |
| 146 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 147 | allocator.free(handle); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 148 | } |
Praveen Chavan | 22e4cc3 | 2015-09-16 11:20:00 -0700 | [diff] [blame] | 149 | handle = NULL; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | status_t GraphicBuffer::initCheck() const { |
Dan Stoza | 133caac | 2014-12-01 15:15:31 -0800 | [diff] [blame] | 153 | return static_cast<status_t>(mInitCheck); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 156 | void GraphicBuffer::dumpAllocationsToSystemLog() |
| 157 | { |
| 158 | GraphicBufferAllocator::dumpToSystemLog(); |
| 159 | } |
| 160 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 161 | ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 162 | { |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 163 | return static_cast<ANativeWindowBuffer*>( |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 164 | const_cast<GraphicBuffer*>(this)); |
| 165 | } |
| 166 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 167 | status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 168 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 169 | { |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 170 | if (mOwner != ownData) |
| 171 | return INVALID_OPERATION; |
| 172 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 173 | if (handle && |
| 174 | static_cast<int>(inWidth) == width && |
| 175 | static_cast<int>(inHeight) == height && |
| 176 | inFormat == format && |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 177 | inLayerCount == layerCount && |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 178 | inUsage == usage) |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 179 | return NO_ERROR; |
| 180 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 181 | if (handle) { |
| 182 | GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); |
| 183 | allocator.free(handle); |
| 184 | handle = 0; |
| 185 | } |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 186 | return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]"); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 189 | bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 190 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 191 | { |
| 192 | if (static_cast<int>(inWidth) != width) return true; |
| 193 | if (static_cast<int>(inHeight) != height) return true; |
| 194 | if (inFormat != format) return true; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 195 | if (inLayerCount != layerCount) return true; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 196 | if ((usage & inUsage) != inUsage) return true; |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 200 | status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 201 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, |
| 202 | std::string requestorName) |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 203 | { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 204 | GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 205 | uint32_t outStride = 0; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 206 | status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 207 | inUsage, &handle, &outStride, mId, |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 208 | std::move(requestorName)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 209 | if (err == NO_ERROR) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 210 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); |
| 211 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 212 | width = static_cast<int>(inWidth); |
| 213 | height = static_cast<int>(inHeight); |
| 214 | format = inFormat; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 215 | layerCount = inLayerCount; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 216 | usage = inUsage; |
| 217 | usage_deprecated = int(usage); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 218 | stride = static_cast<int>(outStride); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 219 | } |
| 220 | return err; |
| 221 | } |
| 222 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 223 | status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method, |
| 224 | uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 225 | uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) { |
| 226 | ANativeWindowBuffer::width = static_cast<int>(inWidth); |
| 227 | ANativeWindowBuffer::height = static_cast<int>(inHeight); |
| 228 | ANativeWindowBuffer::stride = static_cast<int>(inStride); |
| 229 | ANativeWindowBuffer::format = inFormat; |
| 230 | ANativeWindowBuffer::usage = inUsage; |
| 231 | ANativeWindowBuffer::usage_deprecated = int(inUsage); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 232 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 233 | ANativeWindowBuffer::layerCount = inLayerCount; |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 234 | |
| 235 | mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle; |
| 236 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 237 | if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) { |
| 238 | buffer_handle_t importedHandle; |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 239 | status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount, |
| 240 | inFormat, inUsage, inStride, &importedHandle); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 241 | if (err != NO_ERROR) { |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 242 | initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 243 | |
| 244 | return err; |
| 245 | } |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 246 | |
| 247 | if (method == TAKE_UNREGISTERED_HANDLE) { |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 248 | native_handle_close(inHandle); |
| 249 | native_handle_delete(const_cast<native_handle_t*>(inHandle)); |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 252 | inHandle = importedHandle; |
| 253 | mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 256 | ANativeWindowBuffer::handle = inHandle; |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 257 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 258 | return NO_ERROR; |
| 259 | } |
| 260 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 261 | status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel, |
| 262 | int32_t* outBytesPerStride) { |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 263 | const Rect lockBounds(width, height); |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 264 | status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 265 | return res; |
| 266 | } |
| 267 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 268 | status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr, |
| 269 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 270 | if (rect.left < 0 || rect.right > width || |
| 271 | rect.top < 0 || rect.bottom > height) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 272 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
Dan Stoza | 01049c8 | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 273 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 274 | width, height); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 275 | return BAD_VALUE; |
| 276 | } |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 277 | |
Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 278 | status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel, |
| 279 | outBytesPerStride); |
| 280 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 281 | return res; |
| 282 | } |
| 283 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 284 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 285 | { |
| 286 | const Rect lockBounds(width, height); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 287 | status_t res = lockYCbCr(inUsage, lockBounds, ycbcr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 288 | return res; |
| 289 | } |
| 290 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 291 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect, |
| 292 | android_ycbcr* ycbcr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 293 | { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 294 | if (rect.left < 0 || rect.right > width || |
| 295 | rect.top < 0 || rect.bottom > height) { |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 296 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 297 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 298 | width, height); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 299 | return BAD_VALUE; |
| 300 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 301 | status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 302 | return res; |
| 303 | } |
| 304 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 305 | status_t GraphicBuffer::unlock() |
| 306 | { |
| 307 | status_t res = getBufferMapper().unlock(handle); |
| 308 | return res; |
| 309 | } |
| 310 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 311 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd, |
| 312 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 313 | const Rect lockBounds(width, height); |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 314 | status_t res = |
| 315 | lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 316 | return res; |
| 317 | } |
| 318 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 319 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd, |
| 320 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
| 321 | return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 324 | status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, |
| 325 | const Rect& rect, void** vaddr, int fenceFd, |
| 326 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 327 | if (rect.left < 0 || rect.right > width || |
| 328 | rect.top < 0 || rect.bottom > height) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 329 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 330 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 331 | width, height); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 332 | return BAD_VALUE; |
| 333 | } |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 334 | |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 335 | status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect, |
Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 336 | vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); |
| 337 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 338 | return res; |
| 339 | } |
| 340 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 341 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr, |
| 342 | int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 343 | { |
| 344 | const Rect lockBounds(width, height); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 345 | status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 346 | return res; |
| 347 | } |
| 348 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 349 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, |
| 350 | android_ycbcr* ycbcr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 351 | { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 352 | if (rect.left < 0 || rect.right > width || |
| 353 | rect.top < 0 || rect.bottom > height) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 354 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", |
| 355 | rect.left, rect.top, rect.right, rect.bottom, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 356 | width, height); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 357 | return BAD_VALUE; |
| 358 | } |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 359 | status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 360 | return res; |
| 361 | } |
| 362 | |
| 363 | status_t GraphicBuffer::unlockAsync(int *fenceFd) |
| 364 | { |
| 365 | status_t res = getBufferMapper().unlockAsync(handle, fenceFd); |
| 366 | return res; |
| 367 | } |
| 368 | |
Valerie Hau | ddbfaeb | 2019-02-01 09:54:20 -0800 | [diff] [blame] | 369 | status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
| 370 | uint32_t inLayerCount, uint64_t inUsage, |
| 371 | bool* outSupported) const { |
| 372 | return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage, |
| 373 | outSupported); |
| 374 | } |
| 375 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 376 | size_t GraphicBuffer::getFlattenedSize() const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 377 | return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 378 | } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 379 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 380 | size_t GraphicBuffer::getFdCount() const { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 381 | return static_cast<size_t>(handle ? mTransportNumFds : 0); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 384 | status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 385 | size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); |
| 386 | if (size < sizeNeeded) return NO_MEMORY; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 387 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 388 | size_t fdCountNeeded = GraphicBuffer::getFdCount(); |
| 389 | if (count < fdCountNeeded) return NO_MEMORY; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 390 | |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 391 | int32_t* buf = static_cast<int32_t*>(buffer); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 392 | buf[0] = 'GB01'; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 393 | buf[1] = width; |
| 394 | buf[2] = height; |
| 395 | buf[3] = stride; |
| 396 | buf[4] = format; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 397 | buf[5] = static_cast<int32_t>(layerCount); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 398 | buf[6] = int(usage); // low 32-bits |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 399 | buf[7] = static_cast<int32_t>(mId >> 32); |
| 400 | buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull); |
| 401 | buf[9] = static_cast<int32_t>(mGenerationNumber); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 402 | buf[10] = 0; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 403 | buf[11] = 0; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 404 | buf[12] = int(usage >> 32); // high 32-bits |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 405 | |
| 406 | if (handle) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 407 | buf[10] = int32_t(mTransportNumFds); |
| 408 | buf[11] = int32_t(mTransportNumInts); |
| 409 | memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int)); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 410 | memcpy(buf + 13, handle->data + handle->numFds, |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 411 | static_cast<size_t>(mTransportNumInts) * sizeof(int)); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 412 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 413 | |
Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 414 | buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 415 | size -= sizeNeeded; |
Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 416 | if (handle) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 417 | fds += mTransportNumFds; |
| 418 | count -= static_cast<size_t>(mTransportNumFds); |
Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 419 | } |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 420 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 421 | return NO_ERROR; |
| 422 | } |
| 423 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 424 | status_t GraphicBuffer::unflatten( |
| 425 | void const*& buffer, size_t& size, int const*& fds, size_t& count) { |
Chia-I Wu | bf8d721 | 2018-10-09 15:22:46 -0700 | [diff] [blame] | 426 | if (size < 12 * sizeof(int)) { |
| 427 | android_errorWriteLog(0x534e4554, "114223584"); |
| 428 | return NO_MEMORY; |
| 429 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 430 | |
| 431 | int const* buf = static_cast<int const*>(buffer); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 432 | |
| 433 | // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!! |
| 434 | // see H2BGraphicBufferProducer.cpp |
| 435 | uint32_t flattenWordCount = 0; |
| 436 | if (buf[0] == 'GB01') { |
| 437 | // new version with 64-bits usage bits |
| 438 | flattenWordCount = 13; |
| 439 | } else if (buf[0] == 'GBFR') { |
| 440 | // old version, when usage bits were 32-bits |
| 441 | flattenWordCount = 12; |
| 442 | } else { |
| 443 | return BAD_TYPE; |
| 444 | } |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 445 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 446 | const size_t numFds = static_cast<size_t>(buf[10]); |
| 447 | const size_t numInts = static_cast<size_t>(buf[11]); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 448 | |
Michael Lentine | c168b8a | 2015-02-18 10:14:18 -0800 | [diff] [blame] | 449 | // Limit the maxNumber to be relatively small. The number of fds or ints |
| 450 | // should not come close to this number, and the number itself was simply |
| 451 | // chosen to be high enough to not cause issues and low enough to prevent |
| 452 | // overflow problems. |
| 453 | const size_t maxNumber = 4096; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 454 | if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) { |
| 455 | width = height = stride = format = usage_deprecated = 0; |
| 456 | layerCount = 0; |
| 457 | usage = 0; |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 458 | handle = NULL; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 459 | ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts); |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 460 | return BAD_VALUE; |
| 461 | } |
| 462 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 463 | const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 464 | if (size < sizeNeeded) return NO_MEMORY; |
| 465 | |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 466 | size_t fdCountNeeded = numFds; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 467 | if (count < fdCountNeeded) return NO_MEMORY; |
| 468 | |
| 469 | if (handle) { |
| 470 | // free previous handle if any |
| 471 | free_handle(); |
| 472 | } |
| 473 | |
| 474 | if (numFds || numInts) { |
| 475 | width = buf[1]; |
| 476 | height = buf[2]; |
| 477 | stride = buf[3]; |
| 478 | format = buf[4]; |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 479 | layerCount = static_cast<uintptr_t>(buf[5]); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 480 | usage_deprecated = buf[6]; |
| 481 | if (flattenWordCount == 13) { |
| 482 | usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]); |
| 483 | } else { |
| 484 | usage = uint64_t(usage_deprecated); |
| 485 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 486 | native_handle* h = native_handle_create( |
| 487 | static_cast<int>(numFds), static_cast<int>(numInts)); |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 488 | if (!h) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 489 | width = height = stride = format = usage_deprecated = 0; |
| 490 | layerCount = 0; |
| 491 | usage = 0; |
Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 492 | handle = NULL; |
| 493 | ALOGE("unflatten: native_handle_create failed"); |
| 494 | return NO_MEMORY; |
| 495 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 496 | memcpy(h->data, fds, numFds * sizeof(int)); |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 497 | memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int)); |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 498 | handle = h; |
| 499 | } else { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 500 | width = height = stride = format = usage_deprecated = 0; |
| 501 | layerCount = 0; |
| 502 | usage = 0; |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 503 | handle = NULL; |
| 504 | } |
| 505 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 506 | mId = static_cast<uint64_t>(buf[7]) << 32; |
| 507 | mId |= static_cast<uint32_t>(buf[8]); |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 508 | |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 509 | mGenerationNumber = static_cast<uint32_t>(buf[9]); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 510 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 511 | mOwner = ownHandle; |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 512 | |
| 513 | if (handle != 0) { |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 514 | buffer_handle_t importedHandle; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 515 | status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height), |
| 516 | uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle); |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 517 | if (err != NO_ERROR) { |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 518 | width = height = stride = format = usage_deprecated = 0; |
| 519 | layerCount = 0; |
| 520 | usage = 0; |
Lingyun Zhu | 2aff702 | 2012-11-20 19:24:35 +0800 | [diff] [blame] | 521 | handle = NULL; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 522 | ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err); |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 523 | return err; |
| 524 | } |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 525 | |
| 526 | native_handle_close(handle); |
| 527 | native_handle_delete(const_cast<native_handle_t*>(handle)); |
| 528 | handle = importedHandle; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 529 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 532 | buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 533 | size -= sizeNeeded; |
| 534 | fds += numFds; |
| 535 | count -= numFds; |
| 536 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 537 | return NO_ERROR; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame^] | 540 | void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) { |
| 541 | mDeathCallbacks.emplace_back(deathCallback, context); |
| 542 | } |
| 543 | |
Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 544 | #ifndef LIBUI_IN_VNDK |
| 545 | bool GraphicBuffer::isBufferHubBuffer() const { |
| 546 | return mBufferHubBuffer != nullptr; |
| 547 | } |
| 548 | #endif // LIBUI_IN_VNDK |
| 549 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 550 | // --------------------------------------------------------------------------- |
| 551 | |
| 552 | }; // namespace android |