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