blob: ac32985890ac8436b7f2063eab01f228b88d77b4 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <sys/mman.h>
19#include <utils/CallStack.h>
20#include <cutils/ashmem.h>
21#include <cutils/log.h>
22#include <utils/String8.h>
23
24#include <ui/BufferMapper.h>
25
26#include "BufferAllocator.h"
27
28// FIXME: ANDROID_GRALLOC_DEBUG must only be used with *our* gralloc
29#define ANDROID_GRALLOC_DEBUG 1
30
31
32namespace android {
33// ---------------------------------------------------------------------------
34
35Mutex BufferAllocator::sLock;
36KeyedVector<buffer_handle_t, BufferAllocator::alloc_rec_t> BufferAllocator::sAllocList;
37
38BufferAllocator::BufferAllocator()
39 : mAllocDev(0)
40{
41 hw_module_t const* module;
42 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
43 LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
44 if (err == 0) {
45 gralloc_open(module, &mAllocDev);
46 }
47}
48
49BufferAllocator::~BufferAllocator()
50{
51 gralloc_close(mAllocDev);
52}
53
54void BufferAllocator::dump(String8& result) const
55{
56 Mutex::Autolock _l(sLock);
57 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
58 size_t total = 0;
59 const size_t SIZE = 512;
60 char buffer[SIZE];
61 snprintf(buffer, SIZE, "Allocated buffers:\n");
62 result.append(buffer);
63 const size_t c = list.size();
64 for (size_t i=0 ; i<c ; i++) {
65 const alloc_rec_t& rec(list.valueAt(i));
66 snprintf(buffer, SIZE, "%10p: %10p | %7.2f KB | %4u x %4u | %2d | 0x%08x\n",
67 list.keyAt(i), rec.vaddr, rec.size/1024.0f,
68 rec.w, rec.h, rec.format, rec.usage);
69 result.append(buffer);
70 total += rec.size;
71 }
72 snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
73 result.append(buffer);
74}
75
76status_t BufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
77 int usage, buffer_handle_t* handle, int32_t* stride)
78{
79 Mutex::Autolock _l(mLock);
80
81 // we have a h/w allocator and h/w buffer is requested
82 status_t err = mAllocDev->alloc(mAllocDev,
83 w, h, format, usage, handle, stride);
84 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
85 w, h, format, usage, err, strerror(-err));
86
87 if (err == NO_ERROR) {
88 Mutex::Autolock _l(sLock);
89 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
90 alloc_rec_t rec;
91 rec.w = w;
92 rec.h = h;
93 rec.format = format;
94 rec.usage = usage;
95 rec.vaddr = 0;
96 rec.size = h * stride[0] * bytesPerPixel(format);
97 list.add(*handle, rec);
98 }
99
100 return err;
101}
102
103status_t BufferAllocator::free(buffer_handle_t handle)
104{
105 Mutex::Autolock _l(mLock);
106
107#if ANDROID_GRALLOC_DEBUG
108 if (handle->data[2]) {
109 CallStack s;
110 s.update();
111 s.dump("");
112 }
113#endif
114
115 status_t err = mAllocDev->free(mAllocDev, handle);
116 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
117
118 if (err == NO_ERROR) {
119 Mutex::Autolock _l(sLock);
120 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
121 list.removeItem(handle);
122 }
123
124 return err;
125}
126
127status_t BufferAllocator::map(buffer_handle_t handle, void** addr)
128{
129 Mutex::Autolock _l(mLock);
130 status_t err = BufferMapper::get().map(handle, addr);
131 if (err == NO_ERROR) {
132 Mutex::Autolock _l(sLock);
133 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
134 ssize_t idx = list.indexOfKey(handle);
135 if (idx >= 0)
136 list.editValueAt(idx).vaddr = addr;
137 }
138
139 return err;
140}
141
142status_t BufferAllocator::unmap(buffer_handle_t handle)
143{
144 Mutex::Autolock _l(mLock);
145 gralloc_module_t* mod = (gralloc_module_t*)mAllocDev->common.module;
146 status_t err = mod->unmap(mod, handle);
147 LOGW_IF(err, "unmap(...) failed %d (%s)", err, strerror(-err));
148
149 if (err == NO_ERROR) {
150 Mutex::Autolock _l(sLock);
151 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
152 ssize_t idx = list.indexOfKey(handle);
153 if (idx >= 0)
154 list.editValueAt(idx).vaddr = 0;
155 }
156
157 return err;
158}
159
160
161// ---------------------------------------------------------------------------
162}; // namespace android