Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <cutils/log.h> |
| 31 | #include <sync/sync.h> |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 32 | #include <algorithm> |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 33 | #include <sstream> |
| 34 | #include <string> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 35 | |
| 36 | #include "gr_device_impl.h" |
| 37 | #include "gr_buf_descriptor.h" |
| 38 | #include "gralloc_priv.h" |
| 39 | #include "qd_utils.h" |
| 40 | #include "qdMetaData.h" |
| 41 | #include "gr_utils.h" |
| 42 | |
| 43 | int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device); |
| 44 | |
| 45 | int gralloc_device_close(struct hw_device_t *device); |
| 46 | |
| 47 | static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open}; |
| 48 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 49 | struct gralloc_module_t HAL_MODULE_INFO_SYM = { |
| 50 | .common = { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 51 | .tag = HARDWARE_MODULE_TAG, |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 52 | .module_api_version = GRALLOC_MODULE_API_VERSION_1_0, |
| 53 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 54 | .id = GRALLOC_HARDWARE_MODULE_ID, |
| 55 | .name = "Graphics Memory Module", |
| 56 | .author = "Code Aurora Forum", |
| 57 | .methods = &gralloc_module_methods, |
| 58 | .dso = 0, |
| 59 | .reserved = {0}, |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) { |
| 64 | int status = -EINVAL; |
| 65 | if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 66 | gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 67 | *device = reinterpret_cast<hw_device_t *>(dev); |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 68 | if (dev) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 69 | status = 0; |
| 70 | } else { |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 71 | ALOGE("Fatal error opening gralloc1 device"); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 72 | } |
| 73 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 74 | return status; |
| 75 | } |
| 76 | |
| 77 | namespace gralloc1 { |
| 78 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 79 | GrallocImpl::GrallocImpl(const hw_module_t *module) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 80 | common.tag = HARDWARE_DEVICE_TAG; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 81 | common.version = GRALLOC_MODULE_API_VERSION_1_0; |
| 82 | common.module = const_cast<hw_module_t *>(module); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 83 | common.close = CloseDevice; |
| 84 | getFunction = GetFunction; |
| 85 | getCapabilities = GetCapabilities; |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 86 | |
| 87 | initalized_ = Init(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool GrallocImpl::Init() { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 91 | buf_mgr_ = BufferManager::GetInstance(); |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 92 | return buf_mgr_ != nullptr; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | GrallocImpl::~GrallocImpl() { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 96 | } |
| 97 | |
Naseer Ahmed | 7df1e40 | 2017-03-16 15:13:34 -0400 | [diff] [blame] | 98 | int GrallocImpl::CloseDevice(hw_device_t *device __unused) { |
| 99 | // No-op since the gralloc device is a singleton |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, |
Naseer Ahmed | 8f9c7c3 | 2017-02-21 16:45:14 -0500 | [diff] [blame] | 104 | int32_t /*gralloc1_capability_t*/ *out_capabilities) { |
| 105 | if (device != nullptr) { |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 106 | if (out_capabilities != nullptr && *out_count >= 2) { |
Naseer Ahmed | 8f9c7c3 | 2017-02-21 16:45:14 -0500 | [diff] [blame] | 107 | out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE; |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 108 | out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 109 | } |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 110 | *out_count = 2; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 111 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) { |
| 116 | if (!device) { |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | switch (function) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 121 | case GRALLOC1_FUNCTION_DUMP: |
| 122 | return reinterpret_cast<gralloc1_function_pointer_t>(Dump); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 123 | case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR: |
| 124 | return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor); |
| 125 | case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR: |
| 126 | return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor); |
| 127 | case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE: |
| 128 | return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage); |
| 129 | case GRALLOC1_FUNCTION_SET_DIMENSIONS: |
| 130 | return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions); |
| 131 | case GRALLOC1_FUNCTION_SET_FORMAT: |
| 132 | return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 133 | case GRALLOC1_FUNCTION_SET_LAYER_COUNT: |
| 134 | return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 135 | case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE: |
| 136 | return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage); |
| 137 | case GRALLOC1_FUNCTION_GET_BACKING_STORE: |
| 138 | return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore); |
| 139 | case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE: |
| 140 | return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage); |
| 141 | case GRALLOC1_FUNCTION_GET_DIMENSIONS: |
| 142 | return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions); |
| 143 | case GRALLOC1_FUNCTION_GET_FORMAT: |
| 144 | return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat); |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 145 | case GRALLOC1_FUNCTION_GET_LAYER_COUNT: |
| 146 | return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 147 | case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE: |
| 148 | return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage); |
| 149 | case GRALLOC1_FUNCTION_GET_STRIDE: |
| 150 | return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride); |
| 151 | case GRALLOC1_FUNCTION_ALLOCATE: |
| 152 | return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers); |
| 153 | case GRALLOC1_FUNCTION_RETAIN: |
| 154 | return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer); |
| 155 | case GRALLOC1_FUNCTION_RELEASE: |
| 156 | return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 157 | case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES: |
| 158 | return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 159 | case GRALLOC1_FUNCTION_LOCK: |
| 160 | return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 161 | case GRALLOC1_FUNCTION_LOCK_FLEX: |
| 162 | return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 163 | case GRALLOC1_FUNCTION_UNLOCK: |
| 164 | return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer); |
| 165 | case GRALLOC1_FUNCTION_PERFORM: |
| 166 | return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform); |
| 167 | default: |
| 168 | ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 175 | gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size, |
| 176 | char *out_buffer) { |
| 177 | if (!device) { |
| 178 | ALOGE("Gralloc Error : device=%p", (void *)device); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 179 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 180 | } |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 181 | const size_t max_dump_size = 8192; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 182 | if (out_buffer == nullptr) { |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 183 | *out_size = max_dump_size; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 184 | } else { |
| 185 | std::ostringstream os; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 186 | os << "-------------------------------" << std::endl; |
| 187 | os << "QTI gralloc dump:" << std::endl; |
| 188 | os << "-------------------------------" << std::endl; |
Naseer Ahmed | dc91813 | 2017-03-07 15:25:14 -0500 | [diff] [blame] | 189 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 190 | dev->buf_mgr_->Dump(&os); |
| 191 | os << "-------------------------------" << std::endl; |
| 192 | auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0); |
| 193 | *out_size = UINT(copied); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 194 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 195 | |
| 196 | return GRALLOC1_ERROR_NONE; |
| 197 | } |
| 198 | |
| 199 | gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device, |
| 200 | buffer_handle_t buffer) { |
| 201 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 202 | if (!device || (private_handle_t::validate(hnd) != 0)) { |
| 203 | ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer); |
| 204 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 205 | } |
| 206 | |
| 207 | return GRALLOC1_ERROR_NONE; |
| 208 | } |
| 209 | |
| 210 | gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device, |
| 211 | gralloc1_buffer_descriptor_t *out_descriptor) { |
| 212 | if (!device) { |
| 213 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 214 | } |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 215 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 216 | return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device, |
| 220 | gralloc1_buffer_descriptor_t descriptor) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 221 | if (!device) { |
| 222 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 223 | } |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 224 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 225 | return dev->buf_mgr_->DestroyBufferDescriptor(descriptor); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device, |
| 229 | gralloc1_buffer_descriptor_t descriptor, |
| 230 | gralloc1_consumer_usage_t usage) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 231 | if (!device) { |
| 232 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 233 | } else { |
| 234 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 235 | return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, |
| 236 | &BufferDescriptor::SetConsumerUsage, usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 237 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device, |
| 241 | gralloc1_buffer_descriptor_t descriptor, |
| 242 | uint32_t width, uint32_t height) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 243 | if (!device) { |
| 244 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 245 | } else { |
| 246 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 247 | return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, |
| 248 | &BufferDescriptor::SetDimensions, |
| 249 | INT(width), INT(height)); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 250 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device, |
| 254 | gralloc1_buffer_descriptor_t descriptor, |
| 255 | int32_t format) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 256 | if (!device) { |
| 257 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 258 | } else { |
| 259 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 260 | return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, |
| 261 | &BufferDescriptor::SetColorFormat, format); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 262 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 263 | } |
| 264 | |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 265 | gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device, |
| 266 | gralloc1_buffer_descriptor_t descriptor, |
| 267 | uint32_t layer_count) { |
| 268 | if (!device) { |
| 269 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 270 | } else { |
| 271 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 272 | return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, |
| 273 | &BufferDescriptor::SetLayerCount, |
| 274 | layer_count); |
| 275 | } |
| 276 | } |
| 277 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 278 | gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device, |
| 279 | gralloc1_buffer_descriptor_t descriptor, |
| 280 | gralloc1_producer_usage_t usage) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 281 | if (!device) { |
| 282 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 283 | } else { |
| 284 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 285 | return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor, |
| 286 | &BufferDescriptor::SetProducerUsage, usage); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 287 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, |
| 291 | gralloc1_backing_store_t *out_backstore) { |
| 292 | if (!device || !buffer) { |
| 293 | return GRALLOC1_ERROR_BAD_HANDLE; |
| 294 | } |
| 295 | |
| 296 | *out_backstore = |
| 297 | static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore()); |
| 298 | |
| 299 | return GRALLOC1_ERROR_NONE; |
| 300 | } |
| 301 | |
| 302 | gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, |
| 303 | gralloc1_consumer_usage_t *outUsage) { |
| 304 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 305 | if (status == GRALLOC1_ERROR_NONE) { |
| 306 | *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage(); |
| 307 | } |
| 308 | |
| 309 | return status; |
| 310 | } |
| 311 | |
| 312 | gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, |
| 313 | uint32_t *outWidth, uint32_t *outHeight) { |
| 314 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 315 | if (status == GRALLOC1_ERROR_NONE) { |
| 316 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 317 | *outWidth = UINT(hnd->GetUnalignedWidth()); |
| 318 | *outHeight = UINT(hnd->GetUnalignedHeight()); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer, |
| 325 | int32_t *outFormat) { |
| 326 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 327 | if (status == GRALLOC1_ERROR_NONE) { |
| 328 | *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat(); |
| 329 | } |
| 330 | |
| 331 | return status; |
| 332 | } |
| 333 | |
Naseer Ahmed | baa39c5 | 2017-03-27 14:00:07 -0400 | [diff] [blame] | 334 | gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer, |
| 335 | uint32_t *outLayerCount) { |
| 336 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 337 | if (status == GRALLOC1_ERROR_NONE) { |
| 338 | *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount(); |
| 339 | } |
| 340 | |
| 341 | return status; |
| 342 | } |
| 343 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 344 | gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, |
| 345 | gralloc1_producer_usage_t *outUsage) { |
| 346 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 347 | if (status == GRALLOC1_ERROR_NONE) { |
| 348 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 349 | *outUsage = hnd->GetProducerUsage(); |
| 350 | } |
| 351 | |
| 352 | return status; |
| 353 | } |
| 354 | |
| 355 | gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, |
| 356 | uint32_t *outStride) { |
| 357 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 358 | if (status == GRALLOC1_ERROR_NONE) { |
| 359 | *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride()); |
| 360 | } |
| 361 | |
| 362 | return status; |
| 363 | } |
| 364 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 365 | gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors, |
| 366 | const gralloc1_buffer_descriptor_t *descriptors, |
| 367 | buffer_handle_t *out_buffers) { |
| 368 | if (!num_descriptors || !descriptors) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 369 | return GRALLOC1_ERROR_BAD_DESCRIPTOR; |
| 370 | } |
| 371 | |
| 372 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 373 | gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors, |
| 374 | out_buffers); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 375 | |
| 376 | return status; |
| 377 | } |
| 378 | |
| 379 | gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { |
| 380 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 381 | if (status == GRALLOC1_ERROR_NONE) { |
| 382 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 383 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 384 | status = dev->buf_mgr_->RetainBuffer(hnd); |
| 385 | } |
| 386 | |
| 387 | return status; |
| 388 | } |
| 389 | |
| 390 | gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { |
| 391 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 392 | if (status == GRALLOC1_ERROR_NONE) { |
| 393 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 394 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 395 | status = dev->buf_mgr_->ReleaseBuffer(hnd); |
| 396 | } |
| 397 | |
| 398 | return status; |
| 399 | } |
| 400 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 401 | gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer, |
| 402 | uint32_t *out_num_planes) { |
| 403 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 404 | if (status == GRALLOC1_ERROR_NONE) { |
| 405 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 406 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 407 | status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes); |
| 408 | } |
| 409 | return status; |
| 410 | } |
| 411 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 412 | gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, |
| 413 | gralloc1_producer_usage_t prod_usage, |
| 414 | gralloc1_consumer_usage_t cons_usage, |
| 415 | const gralloc1_rect_t *region, void **out_data, |
| 416 | int32_t acquire_fence) { |
| 417 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 418 | if (status == GRALLOC1_ERROR_NONE && (acquire_fence > 0)) { |
| 419 | int error = sync_wait(acquire_fence, 1000); |
| 420 | if (error < 0) { |
| 421 | ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno)); |
| 422 | return GRALLOC1_ERROR_UNDEFINED; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 427 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 428 | |
| 429 | // Either producer usage or consumer usage must be *_USAGE_NONE |
| 430 | if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) && |
| 431 | (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) { |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 432 | // Current gralloc1 clients do not satisfy this restriction. |
| 433 | // See b/33588773 for details |
| 434 | // return GRALLOC1_ERROR_BAD_VALUE; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // currently we ignore the region/rect client wants to lock |
| 438 | if (region == NULL) { |
| 439 | return GRALLOC1_ERROR_BAD_VALUE; |
| 440 | } |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 441 | // TODO(user): Need to check if buffer was allocated with the same flags |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 442 | status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage); |
| 443 | |
| 444 | *out_data = reinterpret_cast<void *>(hnd->base); |
| 445 | |
| 446 | return status; |
| 447 | } |
| 448 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 449 | gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer, |
| 450 | gralloc1_producer_usage_t prod_usage, |
| 451 | gralloc1_consumer_usage_t cons_usage, |
| 452 | const gralloc1_rect_t *region, |
| 453 | struct android_flex_layout *out_flex_layout, |
| 454 | int32_t acquire_fence) { |
| 455 | void *out_data; |
| 456 | gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region, |
| 457 | &out_data, acquire_fence); |
| 458 | if (status != GRALLOC1_ERROR_NONE) { |
| 459 | return status; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 460 | } |
| 461 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 462 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 463 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 464 | dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 465 | return status; |
| 466 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 467 | |
| 468 | gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, |
| 469 | int32_t *release_fence) { |
| 470 | gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); |
| 471 | |
| 472 | if (status != GRALLOC1_ERROR_NONE) { |
| 473 | return status; |
| 474 | } |
| 475 | |
| 476 | const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); |
| 477 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 478 | |
| 479 | *release_fence = -1; |
| 480 | |
| 481 | return dev->buf_mgr_->UnlockBuffer(hnd); |
| 482 | } |
| 483 | |
| 484 | gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) { |
| 485 | va_list args; |
| 486 | va_start(args, operation); |
| 487 | GrallocImpl const *dev = GRALLOC_IMPL(device); |
| 488 | gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args); |
| 489 | va_end(args); |
| 490 | |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | } // namespace gralloc1 |