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