| 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; | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 79 | handle = nullptr; | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 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 | } | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 149 | handle = nullptr; | 
| 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); | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 184 | handle = nullptr; | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 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; | 
| Peiyong Lin | 5d2018a | 2019-03-26 15:07:54 -0700 | [diff] [blame] | 197 | if ((usage & USAGE_PROTECTED) != (inUsage & USAGE_PROTECTED)) return true; | 
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 198 | return false; | 
|  | 199 | } | 
|  | 200 |  | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 201 | status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight, | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 202 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, | 
|  | 203 | std::string requestorName) | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 204 | { | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 205 | GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 206 | uint32_t outStride = 0; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 207 | status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount, | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 208 | inUsage, &handle, &outStride, mId, | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 209 | std::move(requestorName)); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 210 | if (err == NO_ERROR) { | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 211 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); | 
|  | 212 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 213 | width = static_cast<int>(inWidth); | 
|  | 214 | height = static_cast<int>(inHeight); | 
|  | 215 | format = inFormat; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 216 | layerCount = inLayerCount; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 217 | usage = inUsage; | 
|  | 218 | usage_deprecated = int(usage); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 219 | stride = static_cast<int>(outStride); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 220 | } | 
|  | 221 | return err; | 
|  | 222 | } | 
|  | 223 |  | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 224 | status_t GraphicBuffer::initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method, | 
|  | 225 | uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, | 
|  | 226 | uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) { | 
|  | 227 | ANativeWindowBuffer::width = static_cast<int>(inWidth); | 
|  | 228 | ANativeWindowBuffer::height = static_cast<int>(inHeight); | 
|  | 229 | ANativeWindowBuffer::stride = static_cast<int>(inStride); | 
|  | 230 | ANativeWindowBuffer::format = inFormat; | 
|  | 231 | ANativeWindowBuffer::usage = inUsage; | 
|  | 232 | ANativeWindowBuffer::usage_deprecated = int(inUsage); | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 233 |  | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 234 | ANativeWindowBuffer::layerCount = inLayerCount; | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle; | 
|  | 237 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 238 | if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) { | 
|  | 239 | buffer_handle_t importedHandle; | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 240 | status_t err = mBufferMapper.importBuffer(inHandle, inWidth, inHeight, inLayerCount, | 
|  | 241 | inFormat, inUsage, inStride, &importedHandle); | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 242 | if (err != NO_ERROR) { | 
| Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 243 | initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0); | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | return err; | 
|  | 246 | } | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 247 |  | 
|  | 248 | if (method == TAKE_UNREGISTERED_HANDLE) { | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 249 | native_handle_close(inHandle); | 
|  | 250 | native_handle_delete(const_cast<native_handle_t*>(inHandle)); | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 253 | inHandle = importedHandle; | 
|  | 254 | mBufferMapper.getTransportSize(inHandle, &mTransportNumFds, &mTransportNumInts); | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Chih-Hung Hsieh | bcc6c92 | 2018-11-13 15:20:59 -0800 | [diff] [blame] | 257 | ANativeWindowBuffer::handle = inHandle; | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 258 |  | 
| Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 259 | return NO_ERROR; | 
|  | 260 | } | 
|  | 261 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 262 | status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel, | 
|  | 263 | int32_t* outBytesPerStride) { | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 264 | const Rect lockBounds(width, height); | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 265 | status_t res = lock(inUsage, lockBounds, vaddr, outBytesPerPixel, outBytesPerStride); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 266 | return res; | 
|  | 267 | } | 
|  | 268 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 269 | status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr, | 
|  | 270 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 271 | if (rect.left < 0 || rect.right  > width || | 
|  | 272 | rect.top  < 0 || rect.bottom > height) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 273 | 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] | 274 | rect.left, rect.top, rect.right, rect.bottom, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 275 | width, height); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 276 | return BAD_VALUE; | 
|  | 277 | } | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 278 |  | 
| Valerie Hau | 250c654 | 2019-01-31 14:23:43 -0800 | [diff] [blame] | 279 | status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr, outBytesPerPixel, | 
|  | 280 | outBytesPerStride); | 
|  | 281 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 282 | return res; | 
|  | 283 | } | 
|  | 284 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 285 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr) | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 286 | { | 
|  | 287 | const Rect lockBounds(width, height); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 288 | status_t res = lockYCbCr(inUsage, lockBounds, ycbcr); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 289 | return res; | 
|  | 290 | } | 
|  | 291 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 292 | status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect, | 
|  | 293 | android_ycbcr* ycbcr) | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 294 | { | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 295 | if (rect.left < 0 || rect.right  > width || | 
|  | 296 | rect.top  < 0 || rect.bottom > height) { | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 297 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", | 
|  | 298 | rect.left, rect.top, rect.right, rect.bottom, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 299 | width, height); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 300 | return BAD_VALUE; | 
|  | 301 | } | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 302 | status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 303 | return res; | 
|  | 304 | } | 
|  | 305 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 306 | status_t GraphicBuffer::unlock() | 
|  | 307 | { | 
|  | 308 | status_t res = getBufferMapper().unlock(handle); | 
|  | 309 | return res; | 
|  | 310 | } | 
|  | 311 |  | 
| Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 312 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd, | 
|  | 313 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 314 | const Rect lockBounds(width, height); | 
| Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 315 | status_t res = | 
|  | 316 | lockAsync(inUsage, lockBounds, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 317 | return res; | 
|  | 318 | } | 
|  | 319 |  | 
| Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 320 | status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd, | 
|  | 321 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { | 
|  | 322 | return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 325 | status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, | 
|  | 326 | const Rect& rect, void** vaddr, int fenceFd, | 
|  | 327 | int32_t* outBytesPerPixel, int32_t* outBytesPerStride) { | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 328 | if (rect.left < 0 || rect.right  > width || | 
|  | 329 | rect.top  < 0 || rect.bottom > height) { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 330 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", | 
|  | 331 | rect.left, rect.top, rect.right, rect.bottom, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 332 | width, height); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 333 | return BAD_VALUE; | 
|  | 334 | } | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 335 |  | 
| Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame] | 336 | status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, inConsumerUsage, rect, | 
| Valerie Hau | b94adfd | 2019-02-07 14:25:12 -0800 | [diff] [blame] | 337 | vaddr, fenceFd, outBytesPerPixel, outBytesPerStride); | 
|  | 338 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 339 | return res; | 
|  | 340 | } | 
|  | 341 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 342 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr, | 
|  | 343 | int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 344 | { | 
|  | 345 | const Rect lockBounds(width, height); | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 346 | status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 347 | return res; | 
|  | 348 | } | 
|  | 349 |  | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 350 | status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, | 
|  | 351 | android_ycbcr* ycbcr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 352 | { | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 353 | if (rect.left < 0 || rect.right  > width || | 
|  | 354 | rect.top  < 0 || rect.bottom > height) { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 355 | ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", | 
|  | 356 | rect.left, rect.top, rect.right, rect.bottom, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 357 | width, height); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 358 | return BAD_VALUE; | 
|  | 359 | } | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 360 | status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 361 | return res; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | status_t GraphicBuffer::unlockAsync(int *fenceFd) | 
|  | 365 | { | 
|  | 366 | status_t res = getBufferMapper().unlockAsync(handle, fenceFd); | 
|  | 367 | return res; | 
|  | 368 | } | 
|  | 369 |  | 
| Valerie Hau | ddbfaeb | 2019-02-01 09:54:20 -0800 | [diff] [blame] | 370 | status_t GraphicBuffer::isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, | 
|  | 371 | uint32_t inLayerCount, uint64_t inUsage, | 
|  | 372 | bool* outSupported) const { | 
|  | 373 | return mBufferMapper.isSupported(inWidth, inHeight, inFormat, inLayerCount, inUsage, | 
|  | 374 | outSupported); | 
|  | 375 | } | 
|  | 376 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 377 | size_t GraphicBuffer::getFlattenedSize() const { | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 378 | #ifndef LIBUI_IN_VNDK | 
|  | 379 | if (mBufferHubBuffer != nullptr) { | 
|  | 380 | return 48; | 
|  | 381 | } | 
|  | 382 | #endif | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 383 | return static_cast<size_t>(13 + (handle ? mTransportNumInts : 0)) * sizeof(int); | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 384 | } | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 385 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 386 | size_t GraphicBuffer::getFdCount() const { | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 387 | #ifndef LIBUI_IN_VNDK | 
|  | 388 | if (mBufferHubBuffer != nullptr) { | 
|  | 389 | return 0; | 
|  | 390 | } | 
|  | 391 | #endif | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 392 | return static_cast<size_t>(handle ? mTransportNumFds : 0); | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 395 | status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 396 | #ifndef LIBUI_IN_VNDK | 
|  | 397 | if (mBufferHubBuffer != nullptr) { | 
| Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame] | 398 | return flattenBufferHubBuffer(buffer, size); | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 399 | } | 
|  | 400 | #endif | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 401 | size_t sizeNeeded = GraphicBuffer::getFlattenedSize(); | 
|  | 402 | if (size < sizeNeeded) return NO_MEMORY; | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 403 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 404 | size_t fdCountNeeded = GraphicBuffer::getFdCount(); | 
|  | 405 | if (count < fdCountNeeded) return NO_MEMORY; | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 406 |  | 
| Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 407 | int32_t* buf = static_cast<int32_t*>(buffer); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 408 | buf[0] = 'GB01'; | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 409 | buf[1] = width; | 
|  | 410 | buf[2] = height; | 
|  | 411 | buf[3] = stride; | 
|  | 412 | buf[4] = format; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 413 | buf[5] = static_cast<int32_t>(layerCount); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 414 | buf[6] = int(usage); // low 32-bits | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 415 | buf[7] = static_cast<int32_t>(mId >> 32); | 
|  | 416 | buf[8] = static_cast<int32_t>(mId & 0xFFFFFFFFull); | 
|  | 417 | buf[9] = static_cast<int32_t>(mGenerationNumber); | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 418 | buf[10] = 0; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 419 | buf[11] = 0; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 420 | buf[12] = int(usage >> 32); // high 32-bits | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 421 |  | 
|  | 422 | if (handle) { | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 423 | buf[10] = int32_t(mTransportNumFds); | 
|  | 424 | buf[11] = int32_t(mTransportNumInts); | 
|  | 425 | memcpy(fds, handle->data, static_cast<size_t>(mTransportNumFds) * sizeof(int)); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 426 | memcpy(buf + 13, handle->data + handle->numFds, | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 427 | static_cast<size_t>(mTransportNumInts) * sizeof(int)); | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 428 | } | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 429 |  | 
| Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 430 | buffer = static_cast<void*>(static_cast<uint8_t*>(buffer) + sizeNeeded); | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 431 | size -= sizeNeeded; | 
| Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 432 | if (handle) { | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 433 | fds += mTransportNumFds; | 
|  | 434 | count -= static_cast<size_t>(mTransportNumFds); | 
| Andy McFadden | bc96e47 | 2014-03-17 16:48:23 -0700 | [diff] [blame] | 435 | } | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 436 | return NO_ERROR; | 
|  | 437 | } | 
|  | 438 |  | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 439 | status_t GraphicBuffer::unflatten(void const*& buffer, size_t& size, int const*& fds, | 
|  | 440 | size_t& count) { | 
| Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame] | 441 | // Check if size is not smaller than buf[0] is supposed to take. | 
|  | 442 | if (size < sizeof(int)) { | 
|  | 443 | return NO_MEMORY; | 
|  | 444 | } | 
|  | 445 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 446 | int const* buf = static_cast<int const*>(buffer); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 447 |  | 
|  | 448 | // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!! | 
|  | 449 | // see H2BGraphicBufferProducer.cpp | 
|  | 450 | uint32_t flattenWordCount = 0; | 
|  | 451 | if (buf[0] == 'GB01') { | 
|  | 452 | // new version with 64-bits usage bits | 
|  | 453 | flattenWordCount = 13; | 
|  | 454 | } else if (buf[0] == 'GBFR') { | 
|  | 455 | // old version, when usage bits were 32-bits | 
|  | 456 | flattenWordCount = 12; | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 457 | } else if (buf[0] == 'BHBB') { // BufferHub backed buffer. | 
|  | 458 | #ifndef LIBUI_IN_VNDK | 
| Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame] | 459 | return unflattenBufferHubBuffer(buffer, size); | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 460 | #else | 
|  | 461 | return BAD_TYPE; | 
|  | 462 | #endif | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 463 | } else { | 
|  | 464 | return BAD_TYPE; | 
|  | 465 | } | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 466 |  | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 467 | if (size < 12 * sizeof(int)) { | 
|  | 468 | android_errorWriteLog(0x534e4554, "114223584"); | 
|  | 469 | return NO_MEMORY; | 
|  | 470 | } | 
|  | 471 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 472 | const size_t numFds  = static_cast<size_t>(buf[10]); | 
|  | 473 | const size_t numInts = static_cast<size_t>(buf[11]); | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 474 |  | 
| Michael Lentine | c168b8a | 2015-02-18 10:14:18 -0800 | [diff] [blame] | 475 | // Limit the maxNumber to be relatively small. The number of fds or ints | 
|  | 476 | // should not come close to this number, and the number itself was simply | 
|  | 477 | // chosen to be high enough to not cause issues and low enough to prevent | 
|  | 478 | // overflow problems. | 
|  | 479 | const size_t maxNumber = 4096; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 480 | if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) { | 
|  | 481 | width = height = stride = format = usage_deprecated = 0; | 
|  | 482 | layerCount = 0; | 
|  | 483 | usage = 0; | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 484 | handle = nullptr; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 485 | ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts); | 
| Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 486 | return BAD_VALUE; | 
|  | 487 | } | 
|  | 488 |  | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 489 | const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int); | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 490 | if (size < sizeNeeded) return NO_MEMORY; | 
|  | 491 |  | 
| Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 492 | size_t fdCountNeeded = numFds; | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 493 | if (count < fdCountNeeded) return NO_MEMORY; | 
|  | 494 |  | 
|  | 495 | if (handle) { | 
|  | 496 | // free previous handle if any | 
|  | 497 | free_handle(); | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | if (numFds || numInts) { | 
|  | 501 | width  = buf[1]; | 
|  | 502 | height = buf[2]; | 
|  | 503 | stride = buf[3]; | 
|  | 504 | format = buf[4]; | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 505 | layerCount = static_cast<uintptr_t>(buf[5]); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 506 | usage_deprecated = buf[6]; | 
|  | 507 | if (flattenWordCount == 13) { | 
|  | 508 | usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]); | 
|  | 509 | } else { | 
|  | 510 | usage = uint64_t(usage_deprecated); | 
|  | 511 | } | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 512 | native_handle* h = | 
|  | 513 | native_handle_create(static_cast<int>(numFds), static_cast<int>(numInts)); | 
| Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 514 | if (!h) { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 515 | width = height = stride = format = usage_deprecated = 0; | 
|  | 516 | layerCount = 0; | 
|  | 517 | usage = 0; | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 518 | handle = nullptr; | 
| Michael Lentine | 3880326 | 2014-10-31 15:25:03 -0700 | [diff] [blame] | 519 | ALOGE("unflatten: native_handle_create failed"); | 
|  | 520 | return NO_MEMORY; | 
|  | 521 | } | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 522 | memcpy(h->data, fds, numFds * sizeof(int)); | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 523 | memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int)); | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 524 | handle = h; | 
|  | 525 | } else { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 526 | width = height = stride = format = usage_deprecated = 0; | 
|  | 527 | layerCount = 0; | 
|  | 528 | usage = 0; | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 529 | handle = nullptr; | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 532 | mId = static_cast<uint64_t>(buf[7]) << 32; | 
|  | 533 | mId |= static_cast<uint32_t>(buf[8]); | 
| Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 534 |  | 
| Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 535 | mGenerationNumber = static_cast<uint32_t>(buf[9]); | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 536 |  | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 537 | mOwner = ownHandle; | 
| Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 538 |  | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 539 | if (handle != nullptr) { | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 540 | buffer_handle_t importedHandle; | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 541 | status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height), | 
|  | 542 | uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle); | 
| Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 543 | if (err != NO_ERROR) { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 544 | width = height = stride = format = usage_deprecated = 0; | 
|  | 545 | layerCount = 0; | 
|  | 546 | usage = 0; | 
| Yi Kong | 48d7608 | 2019-03-24 02:01:06 -0700 | [diff] [blame] | 547 | handle = nullptr; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 548 | ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err); | 
| Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 549 | return err; | 
|  | 550 | } | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 551 |  | 
|  | 552 | native_handle_close(handle); | 
|  | 553 | native_handle_delete(const_cast<native_handle_t*>(handle)); | 
|  | 554 | handle = importedHandle; | 
| Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 555 | mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); | 
| Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 556 | } | 
|  | 557 |  | 
| Dan Stoza | eea6d68 | 2015-04-20 12:07:13 -0700 | [diff] [blame] | 558 | buffer = static_cast<void const*>(static_cast<uint8_t const*>(buffer) + sizeNeeded); | 
| Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 559 | size -= sizeNeeded; | 
|  | 560 | fds += numFds; | 
|  | 561 | count -= numFds; | 
| Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 562 | return NO_ERROR; | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 563 | } | 
|  | 564 |  | 
| Marissa Wall | 78b7220 | 2019-03-15 14:58:34 -0700 | [diff] [blame] | 565 | void GraphicBuffer::addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context) { | 
|  | 566 | mDeathCallbacks.emplace_back(deathCallback, context); | 
|  | 567 | } | 
|  | 568 |  | 
| Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 569 | #ifndef LIBUI_IN_VNDK | 
| Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame] | 570 | status_t GraphicBuffer::flattenBufferHubBuffer(void*& buffer, size_t& size) const { | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 571 | sp<NativeHandle> tokenHandle = mBufferHubBuffer->duplicate(); | 
|  | 572 | if (tokenHandle == nullptr || tokenHandle->handle() == nullptr || | 
|  | 573 | tokenHandle->handle()->numFds != 0) { | 
|  | 574 | return BAD_VALUE; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | // Size needed for one label, one number of ints inside the token, one generation number and | 
|  | 578 | // the token itself. | 
|  | 579 | int numIntsInToken = tokenHandle->handle()->numInts; | 
|  | 580 | const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int); | 
|  | 581 | if (size < sizeNeeded) { | 
|  | 582 | ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__, | 
|  | 583 | static_cast<int>(sizeNeeded), static_cast<int>(size)); | 
|  | 584 | return NO_MEMORY; | 
|  | 585 | } | 
|  | 586 | size -= sizeNeeded; | 
|  | 587 |  | 
|  | 588 | int* buf = static_cast<int*>(buffer); | 
|  | 589 | buf[0] = 'BHBB'; | 
|  | 590 | buf[1] = numIntsInToken; | 
|  | 591 | memcpy(buf + 2, tokenHandle->handle()->data, static_cast<size_t>(numIntsInToken) * sizeof(int)); | 
|  | 592 | buf[2 + numIntsInToken] = static_cast<int32_t>(mGenerationNumber); | 
|  | 593 |  | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 594 | return NO_ERROR; | 
|  | 595 | } | 
|  | 596 |  | 
| Tianyu Jiang | 69823cf | 2019-03-25 15:38:17 -0700 | [diff] [blame] | 597 | status_t GraphicBuffer::unflattenBufferHubBuffer(void const*& buffer, size_t& size) { | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 598 | const int* buf = static_cast<const int*>(buffer); | 
|  | 599 | int numIntsInToken = buf[1]; | 
|  | 600 | // Size needed for one label, one number of ints inside the token, one generation number and | 
|  | 601 | // the token itself. | 
|  | 602 | const size_t sizeNeeded = static_cast<size_t>(3 + numIntsInToken) * sizeof(int); | 
|  | 603 | if (size < sizeNeeded) { | 
|  | 604 | ALOGE("%s: needed size %d, given size %d. Not enough memory.", __FUNCTION__, | 
|  | 605 | static_cast<int>(sizeNeeded), static_cast<int>(size)); | 
|  | 606 | return NO_MEMORY; | 
|  | 607 | } | 
|  | 608 | size -= sizeNeeded; | 
|  | 609 | native_handle_t* importToken = native_handle_create(/*numFds=*/0, /*numInts=*/numIntsInToken); | 
|  | 610 | memcpy(importToken->data, buf + 2, static_cast<size_t>(buf[1]) * sizeof(int)); | 
|  | 611 | sp<NativeHandle> importTokenHandle = NativeHandle::create(importToken, /*ownHandle=*/true); | 
|  | 612 | std::unique_ptr<BufferHubBuffer> bufferHubBuffer = BufferHubBuffer::import(importTokenHandle); | 
|  | 613 | if (bufferHubBuffer == nullptr || bufferHubBuffer.get() == nullptr) { | 
|  | 614 | return BAD_VALUE; | 
|  | 615 | } | 
|  | 616 | // Reconstruct this GraphicBuffer object using the new BufferHubBuffer object. | 
|  | 617 | if (handle) { | 
|  | 618 | free_handle(); | 
|  | 619 | } | 
|  | 620 | mId = 0; | 
|  | 621 | mGenerationNumber = static_cast<uint32_t>(buf[2 + numIntsInToken]); | 
|  | 622 | mInitCheck = | 
|  | 623 | initWithHandle(bufferHubBuffer->duplicateHandle(), /*method=*/TAKE_UNREGISTERED_HANDLE, | 
|  | 624 | bufferHubBuffer->desc().width, bufferHubBuffer->desc().height, | 
|  | 625 | static_cast<PixelFormat>(bufferHubBuffer->desc().format), | 
|  | 626 | bufferHubBuffer->desc().layers, bufferHubBuffer->desc().usage, | 
|  | 627 | bufferHubBuffer->desc().stride); | 
|  | 628 | mBufferId = bufferHubBuffer->id(); | 
|  | 629 | mBufferHubBuffer.reset(std::move(bufferHubBuffer.get())); | 
|  | 630 |  | 
| Tianyu Jiang | 7791e64 | 2019-02-15 18:26:17 -0800 | [diff] [blame] | 631 | return NO_ERROR; | 
|  | 632 | } | 
|  | 633 |  | 
| Jiwen 'Steve' Cai | 2daf518 | 2018-10-16 00:14:03 -0700 | [diff] [blame] | 634 | bool GraphicBuffer::isBufferHubBuffer() const { | 
|  | 635 | return mBufferHubBuffer != nullptr; | 
|  | 636 | } | 
|  | 637 | #endif // LIBUI_IN_VNDK | 
|  | 638 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 639 | // --------------------------------------------------------------------------- | 
|  | 640 |  | 
|  | 641 | }; // namespace android |