blob: cee8b643dde611bd9234872af08efaad14a4ff14 [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>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070019#include <cutils/ashmem.h>
20#include <cutils/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070021
22#include <utils/Singleton.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070023#include <utils/String8.h>
24
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025#include "BufferAllocator.h"
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027
28namespace android {
29// ---------------------------------------------------------------------------
30
Mathias Agopian9f88afb2009-04-17 14:15:18 -070031ANDROID_SINGLETON_STATIC_INSTANCE( BufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070032
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033Mutex BufferAllocator::sLock;
34KeyedVector<buffer_handle_t, BufferAllocator::alloc_rec_t> BufferAllocator::sAllocList;
35
36BufferAllocator::BufferAllocator()
37 : mAllocDev(0)
38{
39 hw_module_t const* module;
40 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
41 LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
42 if (err == 0) {
43 gralloc_open(module, &mAllocDev);
44 }
45}
46
47BufferAllocator::~BufferAllocator()
48{
49 gralloc_close(mAllocDev);
50}
51
52void BufferAllocator::dump(String8& result) const
53{
54 Mutex::Autolock _l(sLock);
55 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
56 size_t total = 0;
57 const size_t SIZE = 512;
58 char buffer[SIZE];
59 snprintf(buffer, SIZE, "Allocated buffers:\n");
60 result.append(buffer);
61 const size_t c = list.size();
62 for (size_t i=0 ; i<c ; i++) {
63 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopian0926f502009-05-04 14:17:04 -070064 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u x %4u | %2d | 0x%08x\n",
65 list.keyAt(i), rec.size/1024.0f,
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066 rec.w, rec.h, rec.format, rec.usage);
67 result.append(buffer);
68 total += rec.size;
69 }
70 snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
71 result.append(buffer);
72}
73
74status_t BufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
75 int usage, buffer_handle_t* handle, int32_t* stride)
76{
77 Mutex::Autolock _l(mLock);
78
79 // we have a h/w allocator and h/w buffer is requested
80 status_t err = mAllocDev->alloc(mAllocDev,
81 w, h, format, usage, handle, stride);
82 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
83 w, h, format, usage, err, strerror(-err));
84
85 if (err == NO_ERROR) {
86 Mutex::Autolock _l(sLock);
87 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
88 alloc_rec_t rec;
89 rec.w = w;
90 rec.h = h;
91 rec.format = format;
92 rec.usage = usage;
93 rec.vaddr = 0;
94 rec.size = h * stride[0] * bytesPerPixel(format);
95 list.add(*handle, rec);
96 }
97
98 return err;
99}
100
101status_t BufferAllocator::free(buffer_handle_t handle)
102{
103 Mutex::Autolock _l(mLock);
104
Mathias Agopian4243e662009-04-15 18:34:24 -0700105 status_t err = mAllocDev->free(mAllocDev, handle);
106 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
107
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108 if (err == NO_ERROR) {
109 Mutex::Autolock _l(sLock);
110 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
111 list.removeItem(handle);
112 }
113
114 return err;
115}
116
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117// ---------------------------------------------------------------------------
118}; // namespace android