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 |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 22 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 23 | // We would eliminate the non-conforming zero-length array, but we can't since |
| 24 | // this is effectively included from the Linux kernel |
| 25 | #pragma clang diagnostic push |
| 26 | #pragma clang diagnostic ignored "-Wzero-length-array" |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 27 | #include <sync/sync.h> |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 28 | #pragma clang diagnostic pop |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 29 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include <utils/Errors.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 | #include <utils/Log.h> |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | #include <ui/GraphicBufferMapper.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | #include <ui/Rect.h> |
| 36 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | #include <hardware/gralloc.h> |
| 38 | |
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 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 45 | GraphicBufferMapper::GraphicBufferMapper() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 46 | : mAllocMod(0) |
| 47 | { |
| 48 | hw_module_t const* module; |
| 49 | int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 50 | ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | if (err == 0) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 52 | mAllocMod = reinterpret_cast<gralloc_module_t const *>(module); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 56 | status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 57 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 58 | ATRACE_CALL(); |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 59 | status_t err; |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 60 | |
| 61 | err = mAllocMod->registerBuffer(mAllocMod, handle); |
| 62 | |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 63 | ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)", |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 64 | handle, err, strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 65 | return err; |
| 66 | } |
| 67 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 68 | status_t GraphicBufferMapper::unregisterBuffer(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 | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 71 | status_t err; |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 72 | |
| 73 | err = mAllocMod->unregisterBuffer(mAllocMod, handle); |
| 74 | |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 75 | ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)", |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 76 | handle, err, strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 77 | return err; |
| 78 | } |
| 79 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 80 | status_t GraphicBufferMapper::lock(buffer_handle_t handle, |
| 81 | uint32_t usage, const Rect& bounds, void** vaddr) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 82 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 83 | ATRACE_CALL(); |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 84 | status_t err; |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 85 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 86 | err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage), |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 87 | bounds.left, bounds.top, bounds.width(), bounds.height(), |
| 88 | vaddr); |
| 89 | |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 90 | ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 94 | status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 95 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr) |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 96 | { |
| 97 | ATRACE_CALL(); |
| 98 | status_t err; |
| 99 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 100 | err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage), |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 101 | bounds.left, bounds.top, bounds.width(), bounds.height(), |
| 102 | ycbcr); |
| 103 | |
| 104 | ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err)); |
| 105 | return err; |
| 106 | } |
| 107 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 108 | status_t GraphicBufferMapper::unlock(buffer_handle_t handle) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 109 | { |
Mathias Agopian | cf56319 | 2012-02-29 20:43:29 -0800 | [diff] [blame] | 110 | ATRACE_CALL(); |
Mathias Agopian | b26af23 | 2009-10-05 18:19:57 -0700 | [diff] [blame] | 111 | status_t err; |
Mathias Agopian | 0a75781 | 2010-12-08 16:40:01 -0800 | [diff] [blame] | 112 | |
| 113 | err = mAllocMod->unlock(mAllocMod, handle); |
| 114 | |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 115 | ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | return err; |
| 117 | } |
| 118 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 119 | status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 120 | uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 121 | { |
| 122 | ATRACE_CALL(); |
| 123 | status_t err; |
| 124 | |
| 125 | if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 126 | err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage), |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 127 | bounds.left, bounds.top, bounds.width(), bounds.height(), |
| 128 | vaddr, fenceFd); |
| 129 | } else { |
| 130 | sync_wait(fenceFd, -1); |
| 131 | close(fenceFd); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 132 | err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage), |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 133 | bounds.left, bounds.top, bounds.width(), bounds.height(), |
| 134 | vaddr); |
| 135 | } |
| 136 | |
| 137 | ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err)); |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle, |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 142 | uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd) |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 143 | { |
| 144 | ATRACE_CALL(); |
| 145 | status_t err; |
| 146 | |
| 147 | if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) { |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 148 | err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle, |
| 149 | static_cast<int>(usage), bounds.left, bounds.top, |
| 150 | bounds.width(), bounds.height(), ycbcr, fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 151 | } else { |
| 152 | sync_wait(fenceFd, -1); |
| 153 | close(fenceFd); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame^] | 154 | err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage), |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 155 | bounds.left, bounds.top, bounds.width(), bounds.height(), |
| 156 | ycbcr); |
| 157 | } |
| 158 | |
| 159 | ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err)); |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd) |
| 164 | { |
| 165 | ATRACE_CALL(); |
| 166 | status_t err; |
| 167 | |
| 168 | if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) { |
| 169 | err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd); |
| 170 | } else { |
| 171 | *fenceFd = -1; |
| 172 | err = mAllocMod->unlock(mAllocMod, handle); |
| 173 | } |
| 174 | |
| 175 | ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err)); |
| 176 | return err; |
| 177 | } |
| 178 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 179 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 180 | }; // namespace android |