Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 15 | #include "GrallocAndroid.hpp" |
Alistair Strachan | c28d28a | 2018-03-24 12:24:00 -0700 | [diff] [blame] | 16 | #include "Debug.hpp" |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 17 | |
Alistair Strachan | c28d28a | 2018-03-24 12:24:00 -0700 | [diff] [blame] | 18 | #ifdef HAVE_GRALLOC1 |
| 19 | #include <sync/sync.h> |
| 20 | #endif |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 21 | |
Nicolas Capens | 83dfdcb | 2015-09-04 13:31:30 -0400 | [diff] [blame] | 22 | GrallocModule *GrallocModule::getInstance() |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 23 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 24 | static GrallocModule instance; |
| 25 | return &instance; |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | GrallocModule::GrallocModule() |
| 29 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 30 | const hw_module_t *module = nullptr; |
| 31 | hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); |
Nicolas Capens | 83dfdcb | 2015-09-04 13:31:30 -0400 | [diff] [blame] | 32 | |
Jaesung Chung | fccddfe | 2017-06-22 20:02:09 +0900 | [diff] [blame] | 33 | m_major_version = (module->module_api_version >> 8) & 0xff; |
| 34 | switch(m_major_version) |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 35 | { |
Jaesung Chung | fccddfe | 2017-06-22 20:02:09 +0900 | [diff] [blame] | 36 | case 0: |
| 37 | m_module = reinterpret_cast<const gralloc_module_t*>(module); |
| 38 | break; |
| 39 | case 1: |
| 40 | #ifdef HAVE_GRALLOC1 |
| 41 | gralloc1_open(module, &m_gralloc1_device); |
| 42 | m_gralloc1_lock = (GRALLOC1_PFN_LOCK) m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_LOCK); |
| 43 | m_gralloc1_unlock = (GRALLOC1_PFN_UNLOCK)m_gralloc1_device->getFunction(m_gralloc1_device, GRALLOC1_FUNCTION_UNLOCK); |
| 44 | break; |
| 45 | #endif |
| 46 | default: |
Alistair Strachan | c28d28a | 2018-03-24 12:24:00 -0700 | [diff] [blame] | 47 | TRACE("unknown gralloc major version (%d)", m_major_version); |
Jaesung Chung | fccddfe | 2017-06-22 20:02:09 +0900 | [diff] [blame] | 48 | break; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 49 | } |
Greg Hartman | f66c110 | 2015-04-17 17:29:14 -0700 | [diff] [blame] | 50 | } |
Alistair Strachan | c28d28a | 2018-03-24 12:24:00 -0700 | [diff] [blame] | 51 | |
| 52 | int GrallocModule::lock(buffer_handle_t handle, int usage, int left, int top, int width, int height, void **vaddr) |
| 53 | { |
| 54 | switch(m_major_version) |
| 55 | { |
| 56 | case 0: |
| 57 | { |
| 58 | return m_module->lock(m_module, handle, usage, left, top, width, height, vaddr); |
| 59 | } |
| 60 | case 1: |
| 61 | #ifdef HAVE_GRALLOC1 |
| 62 | { |
| 63 | gralloc1_rect_t outRect{}; |
| 64 | outRect.left = left; |
| 65 | outRect.top = top; |
| 66 | outRect.width = width; |
| 67 | outRect.height = height; |
| 68 | return m_gralloc1_lock(m_gralloc1_device, handle, usage, usage, &outRect, vaddr, -1); |
| 69 | } |
| 70 | #endif |
| 71 | default: |
| 72 | { |
| 73 | TRACE("no gralloc module to lock"); |
| 74 | return -1; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int GrallocModule::unlock(buffer_handle_t handle) |
| 80 | { |
| 81 | switch(m_major_version) |
| 82 | { |
| 83 | case 0: |
| 84 | { |
| 85 | return m_module->unlock(m_module, handle); |
| 86 | } |
| 87 | case 1: |
| 88 | #ifdef HAVE_GRALLOC1 |
| 89 | { |
| 90 | int32_t fenceFd = -1; |
| 91 | int error = m_gralloc1_unlock(m_gralloc1_device, handle, &fenceFd); |
| 92 | if (!error) |
| 93 | { |
| 94 | sync_wait(fenceFd, -1); |
| 95 | close(fenceFd); |
| 96 | } |
| 97 | return error; |
| 98 | } |
| 99 | #endif |
| 100 | default: |
| 101 | { |
| 102 | TRACE("no gralloc module to unlock"); |
| 103 | return -1; |
| 104 | } |
| 105 | } |
| 106 | } |