blob: c877e9933ca4f8195b6ba0982ac37ab4f82a6b53 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// 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 Hartmanf66c1102015-04-17 17:29:14 -070015#include "GrallocAndroid.hpp"
Alistair Strachanc28d28a2018-03-24 12:24:00 -070016#include "Debug.hpp"
Greg Hartmanf66c1102015-04-17 17:29:14 -070017
Alistair Strachanc28d28a2018-03-24 12:24:00 -070018#ifdef HAVE_GRALLOC1
19#include <sync/sync.h>
20#endif
Greg Hartmanf66c1102015-04-17 17:29:14 -070021
Nicolas Capens83dfdcb2015-09-04 13:31:30 -040022GrallocModule *GrallocModule::getInstance()
Greg Hartmanf66c1102015-04-17 17:29:14 -070023{
Nicolas Capens0bac2852016-05-07 06:09:58 -040024 static GrallocModule instance;
25 return &instance;
Greg Hartmanf66c1102015-04-17 17:29:14 -070026}
27
28GrallocModule::GrallocModule()
29{
Nicolas Capens0bac2852016-05-07 06:09:58 -040030 const hw_module_t *module = nullptr;
31 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
Nicolas Capens83dfdcb2015-09-04 13:31:30 -040032
Jaesung Chungfccddfe2017-06-22 20:02:09 +090033 m_major_version = (module->module_api_version >> 8) & 0xff;
34 switch(m_major_version)
Nicolas Capens0bac2852016-05-07 06:09:58 -040035 {
Jaesung Chungfccddfe2017-06-22 20:02:09 +090036 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 Strachanc28d28a2018-03-24 12:24:00 -070047 TRACE("unknown gralloc major version (%d)", m_major_version);
Jaesung Chungfccddfe2017-06-22 20:02:09 +090048 break;
Nicolas Capens0bac2852016-05-07 06:09:58 -040049 }
Greg Hartmanf66c1102015-04-17 17:29:14 -070050}
Alistair Strachanc28d28a2018-03-24 12:24:00 -070051
52int 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
79int 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}