blob: 85a029bcc0d287db2080ab6ea97e9425deae2cd1 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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
17#define LOG_TAG "BufferMapper"
18
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#include <utils/Errors.h>
27#include <utils/threads.h>
28#include <utils/Log.h>
29
30#include <ui/BufferMapper.h>
31#include <ui/Rect.h>
32
33#include <EGL/android_natives.h>
34
35#include <hardware/gralloc.h>
36
Mathias Agopian8b765b72009-04-10 20:34:46 -070037// ---------------------------------------------------------------------------
38// enable mapping debugging
Mathias Agopian4243e662009-04-15 18:34:24 -070039#define DEBUG_MAPPINGS 0
Mathias Agopian8b765b72009-04-10 20:34:46 -070040// never remove mappings from the list
Mathias Agopian4243e662009-04-15 18:34:24 -070041#define DEBUG_MAPPINGS_KEEP_ALL 0
Mathias Agopian8b765b72009-04-10 20:34:46 -070042// ---------------------------------------------------------------------------
43
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044namespace android {
45// ---------------------------------------------------------------------------
46
Mathias Agopian9f88afb2009-04-17 14:15:18 -070047ANDROID_SINGLETON_STATIC_INSTANCE( BufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070048
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049BufferMapper::BufferMapper()
50 : mAllocMod(0)
51{
52 hw_module_t const* module;
53 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
54 LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
55 if (err == 0) {
56 mAllocMod = (gralloc_module_t const *)module;
57 }
58}
59
Mathias Agopian4243e662009-04-15 18:34:24 -070060status_t BufferMapper::map(buffer_handle_t handle, void** addr, const void* id)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061{
62 Mutex::Autolock _l(mLock);
63 status_t err = mAllocMod->map(mAllocMod, handle, addr);
64 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian8b765b72009-04-10 20:34:46 -070065#if DEBUG_MAPPINGS
66 if (err == NO_ERROR)
Mathias Agopian4243e662009-04-15 18:34:24 -070067 logMapLocked(handle, id);
Mathias Agopian8b765b72009-04-10 20:34:46 -070068#endif
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 return err;
70}
71
Mathias Agopian4243e662009-04-15 18:34:24 -070072status_t BufferMapper::unmap(buffer_handle_t handle, const void* id)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073{
74 Mutex::Autolock _l(mLock);
75 status_t err = mAllocMod->unmap(mAllocMod, handle);
76 LOGW_IF(err, "unmap(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian8b765b72009-04-10 20:34:46 -070077#if DEBUG_MAPPINGS
78 if (err == NO_ERROR)
Mathias Agopian4243e662009-04-15 18:34:24 -070079 logUnmapLocked(handle, id);
Mathias Agopian8b765b72009-04-10 20:34:46 -070080#endif
Mathias Agopian076b1cc2009-04-10 14:24:30 -070081 return err;
82}
83
84status_t BufferMapper::lock(buffer_handle_t handle, int usage, const Rect& bounds)
85{
86 status_t err = mAllocMod->lock(mAllocMod, handle, usage,
87 bounds.left, bounds.top, bounds.width(), bounds.height());
88 LOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
89 return err;
90}
91
92status_t BufferMapper::unlock(buffer_handle_t handle)
93{
94 status_t err = mAllocMod->unlock(mAllocMod, handle);
95 LOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
96 return err;
97}
98
Mathias Agopian4243e662009-04-15 18:34:24 -070099void BufferMapper::logMapLocked(buffer_handle_t handle, const void* id)
Mathias Agopian8b765b72009-04-10 20:34:46 -0700100{
101 CallStack stack;
102 stack.update(2);
103
104 map_info_t info;
Mathias Agopian4243e662009-04-15 18:34:24 -0700105 info.id = id;
106 info.stack = stack;
107
Mathias Agopian8b765b72009-04-10 20:34:46 -0700108 ssize_t index = mMapInfo.indexOfKey(handle);
109 if (index >= 0) {
Mathias Agopian4243e662009-04-15 18:34:24 -0700110 Vector<map_info_t>& infos = mMapInfo.editValueAt(index);
111 infos.add(info);
Mathias Agopian8b765b72009-04-10 20:34:46 -0700112 } else {
Mathias Agopian4243e662009-04-15 18:34:24 -0700113 Vector<map_info_t> infos;
114 infos.add(info);
115 mMapInfo.add(handle, infos);
Mathias Agopian8b765b72009-04-10 20:34:46 -0700116 }
117}
118
Mathias Agopian4243e662009-04-15 18:34:24 -0700119void BufferMapper::logUnmapLocked(buffer_handle_t handle, const void* id)
Mathias Agopian8b765b72009-04-10 20:34:46 -0700120{
121 ssize_t index = mMapInfo.indexOfKey(handle);
122 if (index < 0) {
Mathias Agopian4243e662009-04-15 18:34:24 -0700123 LOGE("unmapping %p which doesn't exist in our map!", handle);
Mathias Agopian8b765b72009-04-10 20:34:46 -0700124 return;
125 }
126
Mathias Agopian4243e662009-04-15 18:34:24 -0700127 Vector<map_info_t>& infos = mMapInfo.editValueAt(index);
128 ssize_t count = infos.size();
129 for (int i=0 ; i<count ; ) {
130 if (infos[i].id == id) {
131 infos.removeAt(i);
132 --count;
133 } else {
134 ++i;
135 }
136 }
137 if (count == 0) {
Mathias Agopian8b765b72009-04-10 20:34:46 -0700138 mMapInfo.removeItemsAt(index, 1);
Mathias Agopian8b765b72009-04-10 20:34:46 -0700139 }
140}
141
142void BufferMapper::dump(buffer_handle_t handle)
143{
144 Mutex::Autolock _l(mLock);
145 ssize_t index = mMapInfo.indexOfKey(handle);
146 if (index < 0) {
147 LOGD("handle %p is not mapped through BufferMapper", handle);
148 return;
149 }
150
Mathias Agopian4243e662009-04-15 18:34:24 -0700151 const Vector<map_info_t>& infos = mMapInfo.valueAt(index);
152 ssize_t count = infos.size();
153 for (int i=0 ; i<count ; i++) {
154 LOGD("#%d", i);
155 infos[i].stack.dump();
Mathias Agopian8b765b72009-04-10 20:34:46 -0700156 }
157}
158
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159// ---------------------------------------------------------------------------
160}; // namespace android