• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "GrallocAndroid.hpp"
16 #include "Debug.hpp"
17 
18 #ifdef HAVE_GRALLOC1
19 #include <sync/sync.h>
20 #endif
21 
getInstance()22 GrallocModule *GrallocModule::getInstance()
23 {
24 	static GrallocModule instance;
25 	return &instance;
26 }
27 
GrallocModule()28 GrallocModule::GrallocModule()
29 {
30 	const hw_module_t *module = nullptr;
31 	hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
32 
33 	m_major_version = (module->module_api_version >> 8) & 0xff;
34 	switch(m_major_version)
35 	{
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:
47 		TRACE("unknown gralloc major version (%d)", m_major_version);
48 		break;
49 	}
50 }
51 
lock(buffer_handle_t handle,int usage,int left,int top,int width,int height,void ** vaddr)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 
unlock(buffer_handle_t handle)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 }
107