blob: e6ca239831fa33f0fa67ba1b4756368bb2f8b2a1 [file] [log] [blame]
Mathias Agopian1473f462009-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 Agopianaf9a5152009-04-10 20:34:46 -070037// ---------------------------------------------------------------------------
38// enable mapping debugging
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070039#define DEBUG_MAPPINGS 0
Mathias Agopianaf9a5152009-04-10 20:34:46 -070040// never remove mappings from the list
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070041#define DEBUG_MAPPINGS_KEEP_ALL 0
Mathias Agopianaf9a5152009-04-10 20:34:46 -070042// ---------------------------------------------------------------------------
43
Mathias Agopian1473f462009-04-10 14:24:30 -070044namespace android {
45// ---------------------------------------------------------------------------
46
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070047template<class BufferMapper> Mutex Singleton<BufferMapper>::sLock;
48template<> BufferMapper* Singleton<BufferMapper>::sInstance(0);
49
Mathias Agopian1473f462009-04-10 14:24:30 -070050BufferMapper::BufferMapper()
51 : mAllocMod(0)
52{
53 hw_module_t const* module;
54 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
55 LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
56 if (err == 0) {
57 mAllocMod = (gralloc_module_t const *)module;
58 }
59}
60
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070061status_t BufferMapper::map(buffer_handle_t handle, void** addr, const void* id)
Mathias Agopian1473f462009-04-10 14:24:30 -070062{
63 Mutex::Autolock _l(mLock);
64 status_t err = mAllocMod->map(mAllocMod, handle, addr);
65 LOGW_IF(err, "map(...) failed %d (%s)", err, strerror(-err));
Mathias Agopianaf9a5152009-04-10 20:34:46 -070066#if DEBUG_MAPPINGS
67 if (err == NO_ERROR)
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070068 logMapLocked(handle, id);
Mathias Agopianaf9a5152009-04-10 20:34:46 -070069#endif
Mathias Agopian1473f462009-04-10 14:24:30 -070070 return err;
71}
72
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070073status_t BufferMapper::unmap(buffer_handle_t handle, const void* id)
Mathias Agopian1473f462009-04-10 14:24:30 -070074{
75 Mutex::Autolock _l(mLock);
76 status_t err = mAllocMod->unmap(mAllocMod, handle);
77 LOGW_IF(err, "unmap(...) failed %d (%s)", err, strerror(-err));
Mathias Agopianaf9a5152009-04-10 20:34:46 -070078#if DEBUG_MAPPINGS
79 if (err == NO_ERROR)
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070080 logUnmapLocked(handle, id);
Mathias Agopianaf9a5152009-04-10 20:34:46 -070081#endif
Mathias Agopian1473f462009-04-10 14:24:30 -070082 return err;
83}
84
85status_t BufferMapper::lock(buffer_handle_t handle, int usage, const Rect& bounds)
86{
87 status_t err = mAllocMod->lock(mAllocMod, handle, usage,
88 bounds.left, bounds.top, bounds.width(), bounds.height());
89 LOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
90 return err;
91}
92
93status_t BufferMapper::unlock(buffer_handle_t handle)
94{
95 status_t err = mAllocMod->unlock(mAllocMod, handle);
96 LOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
97 return err;
98}
99
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700100void BufferMapper::logMapLocked(buffer_handle_t handle, const void* id)
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700101{
102 CallStack stack;
103 stack.update(2);
104
105 map_info_t info;
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700106 info.id = id;
107 info.stack = stack;
108
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700109 ssize_t index = mMapInfo.indexOfKey(handle);
110 if (index >= 0) {
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700111 Vector<map_info_t>& infos = mMapInfo.editValueAt(index);
112 infos.add(info);
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700113 } else {
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700114 Vector<map_info_t> infos;
115 infos.add(info);
116 mMapInfo.add(handle, infos);
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700117 }
118}
119
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700120void BufferMapper::logUnmapLocked(buffer_handle_t handle, const void* id)
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700121{
122 ssize_t index = mMapInfo.indexOfKey(handle);
123 if (index < 0) {
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700124 LOGE("unmapping %p which doesn't exist in our map!", handle);
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700125 return;
126 }
127
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700128 Vector<map_info_t>& infos = mMapInfo.editValueAt(index);
129 ssize_t count = infos.size();
130 for (int i=0 ; i<count ; ) {
131 if (infos[i].id == id) {
132 infos.removeAt(i);
133 --count;
134 } else {
135 ++i;
136 }
137 }
138 if (count == 0) {
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700139 mMapInfo.removeItemsAt(index, 1);
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700140 }
141}
142
143void BufferMapper::dump(buffer_handle_t handle)
144{
145 Mutex::Autolock _l(mLock);
146 ssize_t index = mMapInfo.indexOfKey(handle);
147 if (index < 0) {
148 LOGD("handle %p is not mapped through BufferMapper", handle);
149 return;
150 }
151
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700152 const Vector<map_info_t>& infos = mMapInfo.valueAt(index);
153 ssize_t count = infos.size();
154 for (int i=0 ; i<count ; i++) {
155 LOGD("#%d", i);
156 infos[i].stack.dump();
Mathias Agopianaf9a5152009-04-10 20:34:46 -0700157 }
158}
159
Mathias Agopian1473f462009-04-10 14:24:30 -0700160// ---------------------------------------------------------------------------
161}; // namespace android