| 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 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 35 | #include <ui/Gralloc2.h> | 
| Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 36 | #include <ui/GraphicBuffer.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 38 | #include <system/graphics.h> | 
| Mathias Agopian | 8b765b7 | 2009-04-10 20:34:46 -0700 | [diff] [blame] | 39 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 40 | namespace android { | 
|  | 41 | // --------------------------------------------------------------------------- | 
|  | 42 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 43 | ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper ) | 
| Mathias Agopian | 4243e66 | 2009-04-15 18:34:24 -0700 | [diff] [blame] | 44 |  | 
| Jesse Hall | eeb4ac7 | 2017-07-06 14:02:29 -0700 | [diff] [blame] | 45 | void GraphicBufferMapper::preloadHal() { | 
|  | 46 | Gralloc2::Mapper::preload(); | 
|  | 47 | } | 
|  | 48 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 49 | GraphicBufferMapper::GraphicBufferMapper() | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 50 | : mMapper(std::make_unique<const Gralloc2::Mapper>()) | 
|  | 51 | { | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 52 | } | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 53 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 54 | status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle, | 
|  | 55 | buffer_handle_t* outHandle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 56 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 57 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 58 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 59 | Gralloc2::Error error = mMapper->importBuffer( | 
|  | 60 | hardware::hidl_handle(rawHandle), outHandle); | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | ALOGW_IF(error != Gralloc2::Error::NONE, "importBuffer(%p) failed: %d", | 
|  | 63 | rawHandle, error); | 
|  | 64 |  | 
|  | 65 | return static_cast<status_t>(error); | 
|  | 66 | } | 
|  | 67 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 68 | status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 69 | { | 
| Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 70 | ATRACE_CALL(); | 
| Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 71 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 72 | mMapper->freeBuffer(handle); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 73 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 74 | return NO_ERROR; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Chia-I Wu | 5bac7f3 | 2017-04-06 12:34:32 -0700 | [diff] [blame] | 77 | static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) { | 
|  | 78 | Gralloc2::IMapper::Rect outRect{}; | 
|  | 79 | outRect.left = rect.left; | 
|  | 80 | outRect.top = rect.top; | 
|  | 81 | outRect.width = rect.width(); | 
|  | 82 | outRect.height = rect.height(); | 
|  | 83 | return outRect; | 
| Craig Donner | 58a1ef2 | 2017-02-02 12:40:05 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 86 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, | 
|  | 87 | const Rect& bounds, void** vaddr) | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 88 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 89 | return lockAsync(handle, usage, bounds, vaddr, -1); | 
|  | 90 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 91 |  | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 92 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, | 
|  | 93 | const Rect& bounds, android_ycbcr *ycbcr) | 
|  | 94 | { | 
|  | 95 | return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 98 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 99 | { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 100 | int32_t fenceFd = -1; | 
|  | 101 | status_t error = unlockAsync(handle, &fenceFd); | 
| Daniel Jarai | e9147c2 | 2017-09-20 11:33:51 +0200 | [diff] [blame] | 102 | if (error == NO_ERROR && fenceFd >= 0) { | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 103 | sync_wait(fenceFd, -1); | 
|  | 104 | close(fenceFd); | 
|  | 105 | } | 
|  | 106 | return error; | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 109 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 110 | uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 111 | { | 
| Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 112 | return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, | 
|  | 116 | uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds, | 
|  | 117 | void** vaddr, int fenceFd) | 
|  | 118 | { | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 119 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 120 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 121 | const uint64_t usage = static_cast<uint64_t>( | 
|  | 122 | android_convertGralloc1To0Usage(producerUsage, consumerUsage)); | 
|  | 123 | Gralloc2::Error error = mMapper->lock(handle, usage, | 
|  | 124 | asGralloc2Rect(bounds), fenceFd, vaddr); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 125 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 126 | ALOGW_IF(error != Gralloc2::Error::NONE, "lock(%p, ...) failed: %d", | 
|  | 127 | handle, error); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 128 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 129 | return static_cast<status_t>(error); | 
| Dan Stoza | 8deb4da | 2016-06-01 18:21:44 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 132 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, | 
| Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 133 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 134 | { | 
|  | 135 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 136 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 137 | Gralloc2::YCbCrLayout layout; | 
|  | 138 | Gralloc2::Error error = mMapper->lock(handle, usage, | 
|  | 139 | asGralloc2Rect(bounds), fenceFd, &layout); | 
|  | 140 | if (error == Gralloc2::Error::NONE) { | 
|  | 141 | ycbcr->y = layout.y; | 
|  | 142 | ycbcr->cb = layout.cb; | 
|  | 143 | ycbcr->cr = layout.cr; | 
|  | 144 | ycbcr->ystride = static_cast<size_t>(layout.yStride); | 
|  | 145 | ycbcr->cstride = static_cast<size_t>(layout.cStride); | 
|  | 146 | ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep); | 
| Chia-I Wu | 9ba189d | 2016-09-22 17:13:08 +0800 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 149 | return static_cast<status_t>(error); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) | 
|  | 153 | { | 
|  | 154 | ATRACE_CALL(); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 155 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 156 | *fenceFd = mMapper->unlock(handle); | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 157 |  | 
| Chia-I Wu | cb8405e | 2017-04-17 15:20:19 -0700 | [diff] [blame] | 158 | return NO_ERROR; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 161 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 162 | }; // namespace android |