blob: 33ef1fc8945c0fec9ffcb27b5a620615419d7385 [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 Agopian67bbac82010-04-15 14:57:39 -070018#define LOG_TAG "GraphicBufferAllocator"
19
Mathias Agopian1473f462009-04-10 14:24:30 -070020#include <cutils/log.h>
Mathias Agopiana6b40ba2009-04-15 18:34:24 -070021
22#include <utils/Singleton.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070023#include <utils/String8.h>
24
Mathias Agopian6950e422009-10-05 17:07:12 -070025#include <ui/GraphicBufferAllocator.h>
Mathias Agopian1473f462009-04-10 14:24:30 -070026
Mathias Agopian1473f462009-04-10 14:24:30 -070027namespace 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;
Erik Gilling94720d72010-12-01 16:38:01 -080057 const size_t SIZE = 4096;
Mathias Agopian1473f462009-04-10 14:24:30 -070058 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 Agopian06a61e22011-01-19 16:15:53 -080064 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
Mathias Agopiandff8e582009-05-04 14:17:04 -070065 list.keyAt(i), rec.size/1024.0f,
Mathias Agopian67bbac82010-04-15 14:57:39 -070066 rec.w, rec.s, rec.h, rec.format, rec.usage);
Mathias Agopian1473f462009-04-10 14:24:30 -070067 result.append(buffer);
68 total += rec.size;
69 }
70 snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
71 result.append(buffer);
Erik Gilling94720d72010-12-01 16:38:01 -080072 if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
73 mAllocDev->dump(mAllocDev, buffer, SIZE);
74 result.append(buffer);
75 }
Mathias Agopian1473f462009-04-10 14:24:30 -070076}
77
Mathias Agopiane869aee2010-12-03 17:33:09 -080078void GraphicBufferAllocator::dumpToSystemLog()
79{
80 String8 s;
81 GraphicBufferAllocator::getInstance().dump(s);
82 LOGD("%s", s.string());
83}
84
Mathias Agopian6950e422009-10-05 17:07:12 -070085status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopian1473f462009-04-10 14:24:30 -070086 int usage, buffer_handle_t* handle, int32_t* stride)
87{
Mathias Agopian67bbac82010-04-15 14:57:39 -070088 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
89 // allowed from an API stand-point allocate a 1x1 buffer instead.
90 if (!w || !h)
91 w = h = 1;
Mathias Agopian9779b222009-09-07 16:32:45 -070092
Mathias Agopian1473f462009-04-10 14:24:30 -070093 // we have a h/w allocator and h/w buffer is requested
Mathias Agopian6f5f5a02009-10-05 18:19:57 -070094 status_t err;
95
Mathias Agopian19f9eda2010-12-08 16:40:01 -080096 err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
Mathias Agopian9779b222009-09-07 16:32:45 -070097
Mathias Agopian1473f462009-04-10 14:24:30 -070098 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
99 w, h, format, usage, err, strerror(-err));
100
101 if (err == NO_ERROR) {
102 Mutex::Autolock _l(sLock);
103 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
104 alloc_rec_t rec;
105 rec.w = w;
106 rec.h = h;
Mathias Agopian67bbac82010-04-15 14:57:39 -0700107 rec.s = *stride;
Mathias Agopian1473f462009-04-10 14:24:30 -0700108 rec.format = format;
109 rec.usage = usage;
Mathias Agopian1473f462009-04-10 14:24:30 -0700110 rec.size = h * stride[0] * bytesPerPixel(format);
111 list.add(*handle, rec);
112 }
113
114 return err;
115}
116
Mathias Agopian6950e422009-10-05 17:07:12 -0700117status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian1473f462009-04-10 14:24:30 -0700118{
Mathias Agopian6f5f5a02009-10-05 18:19:57 -0700119 status_t err;
Mathias Agopian19f9eda2010-12-08 16:40:01 -0800120
121 err = mAllocDev->free(mAllocDev, handle);
Mathias Agopian6f5f5a02009-10-05 18:19:57 -0700122
Mathias Agopiana6b40ba2009-04-15 18:34:24 -0700123 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian1473f462009-04-10 14:24:30 -0700124 if (err == NO_ERROR) {
125 Mutex::Autolock _l(sLock);
126 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
127 list.removeItem(handle);
128 }
129
130 return err;
131}
132
Mathias Agopian1473f462009-04-10 14:24:30 -0700133// ---------------------------------------------------------------------------
134}; // namespace android