blob: 57d5fc34c63f4275bed969dfc922f3f4b1b63c46 [file] [log] [blame]
Mathias Agopian1473f462009-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
Mathias Agopian1473f462009-04-10 14:24:30 -070018#include <cutils/log.h>
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070019
20#include <utils/Singleton.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070021#include <utils/String8.h>
22
Mathias Agopian6950e422009-10-05 17:07:12 -070023#include <ui/GraphicBufferAllocator.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070024
Mathias Agopian6f5f5a02009-10-05 18:19:57 -070025#include <private/ui/sw_gralloc_handle.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070026
27namespace android {
28// ---------------------------------------------------------------------------
29
Mathias Agopian6950e422009-10-05 17:07:12 -070030ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070031
Mathias Agopian6950e422009-10-05 17:07:12 -070032Mutex GraphicBufferAllocator::sLock;
Mathias Agopian6f5f5a02009-10-05 18:19:57 -070033KeyedVector<buffer_handle_t,
34 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian1473f462009-04-10 14:24:30 -070035
Mathias Agopian6950e422009-10-05 17:07:12 -070036GraphicBufferAllocator::GraphicBufferAllocator()
Mathias Agopian1473f462009-04-10 14:24:30 -070037 : 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
Mathias Agopian6950e422009-10-05 17:07:12 -070047GraphicBufferAllocator::~GraphicBufferAllocator()
Mathias Agopian1473f462009-04-10 14:24:30 -070048{
49 gralloc_close(mAllocDev);
50}
51
Mathias Agopian6950e422009-10-05 17:07:12 -070052void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian1473f462009-04-10 14:24:30 -070053{
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 Agopiandff8e582009-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 Agopian1473f462009-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
Mathias Agopian9779b222009-09-07 16:32:45 -070074static inline uint32_t clamp(uint32_t c) {
75 return c>0 ? c : 1;
76}
77
Mathias Agopian6950e422009-10-05 17:07:12 -070078status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopian1473f462009-04-10 14:24:30 -070079 int usage, buffer_handle_t* handle, int32_t* stride)
80{
81 Mutex::Autolock _l(mLock);
Mathias Agopian9779b222009-09-07 16:32:45 -070082
83 // make sure to not allocate a 0 x 0 buffer
84 w = clamp(w);
85 h = clamp(h);
86
Mathias Agopian1473f462009-04-10 14:24:30 -070087 // we have a h/w allocator and h/w buffer is requested
Mathias Agopian6f5f5a02009-10-05 18:19:57 -070088 status_t err;
89
90 if (usage & GRALLOC_USAGE_HW_MASK) {
91 err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
92 } else {
93 err = sw_gralloc_handle_t::alloc(w, h, format, usage, handle, stride);
94 }
Mathias Agopian9779b222009-09-07 16:32:45 -070095
Mathias Agopian1473f462009-04-10 14:24:30 -070096 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
97 w, h, format, usage, err, strerror(-err));
98
99 if (err == NO_ERROR) {
100 Mutex::Autolock _l(sLock);
101 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
102 alloc_rec_t rec;
103 rec.w = w;
104 rec.h = h;
105 rec.format = format;
106 rec.usage = usage;
107 rec.vaddr = 0;
108 rec.size = h * stride[0] * bytesPerPixel(format);
109 list.add(*handle, rec);
Mathias Agopianc4646e62009-09-27 18:44:09 -0700110 } else {
111 String8 s;
112 dump(s);
113 LOGD("%s", s.string());
Mathias Agopian1473f462009-04-10 14:24:30 -0700114 }
115
116 return err;
117}
118
Mathias Agopian6950e422009-10-05 17:07:12 -0700119status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian1473f462009-04-10 14:24:30 -0700120{
121 Mutex::Autolock _l(mLock);
122
Mathias Agopian6f5f5a02009-10-05 18:19:57 -0700123 status_t err;
124 if (sw_gralloc_handle_t::validate(handle) < 0) {
125 err = mAllocDev->free(mAllocDev, handle);
126 } else {
127 err = sw_gralloc_handle_t::free((sw_gralloc_handle_t*)handle);
128 }
129
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700130 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700131 if (err == NO_ERROR) {
132 Mutex::Autolock _l(sLock);
133 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
134 list.removeItem(handle);
135 }
136
137 return err;
138}
139
Mathias Agopian1473f462009-04-10 14:24:30 -0700140// ---------------------------------------------------------------------------
141}; // namespace android