Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -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 | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 17 | #define LOG_TAG "GraphicBufferMapper" |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 20 | |
Mathias Agopian | fe2f54f | 2017-02-15 19:48:58 -0800 | [diff] [blame] | 21 | #include <ui/GraphicBufferMapper.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 23 | #include <grallocusage/GrallocUsageConversion.h> |
| 24 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 25 | // We would eliminate the non-conforming zero-length array, but we can't since |
| 26 | // this is effectively included from the Linux kernel |
| 27 | #pragma clang diagnostic push |
| 28 | #pragma clang diagnostic ignored "-Wzero-length-array" |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 29 | #include <sync/sync.h> |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 30 | #pragma clang diagnostic pop |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 31 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <utils/Log.h> |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 34 | |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 35 | #include <ui/Gralloc.h> |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 36 | #include <ui/Gralloc2.h> |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 37 | #include <ui/Gralloc3.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 38 | #include <ui/GraphicBuffer.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 40 | #include <system/graphics.h> |
Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | // --------------------------------------------------------------------------- |
| 44 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 45 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper ) |
Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 46 | |
Jesse Hall | eeb4ac7 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 47 | void GraphicBufferMapper::preloadHal() { |
Marissa Wall | d380e2c | 2018-12-29 14:17:29 -0800 | [diff] [blame] | 48 | Gralloc2Mapper::preload(); |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 49 | Gralloc3Mapper::preload(); |
Jesse Hall | eeb4ac7 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Marissa Wall | 925bf7f | 2018-12-29 14:27:11 -0800 | [diff] [blame] | 52 | GraphicBufferMapper::GraphicBufferMapper() { |
| 53 | mMapper = std::make_unique<const Gralloc3Mapper>(); |
| 54 | if (!mMapper->isSupported()) { |
| 55 | mMapper = std::make_unique<const Gralloc2Mapper>(); |
| 56 | } |
| 57 | |
| 58 | if (!mMapper->isSupported()) { |
| 59 | LOG_ALWAYS_FATAL("gralloc-mapper is missing"); |
| 60 | } |
| 61 | } |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 62 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 63 | status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle, |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 64 | uint32_t width, uint32_t height, uint32_t layerCount, |
| 65 | PixelFormat format, uint64_t usage, uint32_t stride, |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 66 | buffer_handle_t* outHandle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 67 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 68 | ATRACE_CALL(); |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 69 | |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 70 | buffer_handle_t bufferHandle; |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 71 | status_t error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), &bufferHandle); |
| 72 | if (error != NO_ERROR) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 73 | ALOGW("importBuffer(%p) failed: %d", rawHandle, error); |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 74 | return error; |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 75 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 76 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 77 | error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage, |
| 78 | stride); |
| 79 | if (error != NO_ERROR) { |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 80 | ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error); |
| 81 | freeBuffer(bufferHandle); |
| 82 | return static_cast<status_t>(error); |
| 83 | } |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 84 | |
Chia-I Wu | dbbe33b | 2017-09-27 15:22:21 -0700 | [diff] [blame] | 85 | *outHandle = bufferHandle; |
| 86 | |
| 87 | return NO_ERROR; |
| 88 | } |
| 89 | |
| 90 | void GraphicBufferMapper::getTransportSize(buffer_handle_t handle, |
| 91 | uint32_t* outTransportNumFds, uint32_t* outTransportNumInts) |
| 92 | { |
| 93 | mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts); |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 96 | status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 97 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 98 | ATRACE_CALL(); |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 99 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 100 | mMapper->freeBuffer(handle); |
Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 101 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 102 | return NO_ERROR; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame^] | 105 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds, |
| 106 | void** vaddr, int32_t* outBytesPerPixel, |
| 107 | int32_t* outBytesPerStride) { |
| 108 | return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride); |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 109 | } |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 110 | |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 111 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, |
| 112 | const Rect& bounds, android_ycbcr *ycbcr) |
| 113 | { |
| 114 | return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 117 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 118 | { |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 119 | int32_t fenceFd = -1; |
| 120 | status_t error = unlockAsync(handle, &fenceFd); |
Daniel Jarai | e9147c2 | 2017-09-20 11:33:51 +0200 | [diff] [blame] | 121 | if (error == NO_ERROR && fenceFd >= 0) { |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 122 | sync_wait(fenceFd, -1); |
| 123 | close(fenceFd); |
| 124 | } |
| 125 | return error; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame^] | 128 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds, |
| 129 | void** vaddr, int fenceFd, int32_t* outBytesPerPixel, |
| 130 | int32_t* outBytesPerStride) { |
| 131 | return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel, |
| 132 | outBytesPerStride); |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame^] | 135 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage, |
| 136 | uint64_t consumerUsage, const Rect& bounds, void** vaddr, |
| 137 | int fenceFd, int32_t* outBytesPerPixel, |
| 138 | int32_t* outBytesPerStride) { |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 139 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 140 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 141 | const uint64_t usage = static_cast<uint64_t>( |
| 142 | android_convertGralloc1To0Usage(producerUsage, consumerUsage)); |
Valerie Hau | 0c9fc36 | 2019-01-22 09:17:19 -0800 | [diff] [blame^] | 143 | return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel, |
| 144 | outBytesPerStride); |
Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 147 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 148 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 149 | { |
| 150 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 151 | |
Marissa Wall | 1e77925 | 2018-12-29 12:01:57 -0800 | [diff] [blame] | 152 | return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) |
| 156 | { |
| 157 | ATRACE_CALL(); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 158 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 159 | *fenceFd = mMapper->unlock(handle); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 160 | |
Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 161 | return NO_ERROR; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 162 | } |
| 163 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 164 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 165 | }; // namespace android |