blob: 610e2c5609814aa34e5f5a7698b03d567f971567 [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
Mathias Agopian5629eb12010-04-15 14:57:39 -070018#define LOG_TAG "GraphicBufferAllocator"
19
Mathias Agopian076b1cc2009-04-10 14:24:30 -070020#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 Agopian3330b202009-10-05 17:07:12 -070025#include <ui/GraphicBufferAllocator.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026
Mathias Agopianb26af232009-10-05 18:19:57 -070027#include <private/ui/sw_gralloc_handle.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028
29namespace android {
30// ---------------------------------------------------------------------------
31
Mathias Agopian3330b202009-10-05 17:07:12 -070032ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070033
Mathias Agopian3330b202009-10-05 17:07:12 -070034Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070035KeyedVector<buffer_handle_t,
36 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
Mathias Agopian3330b202009-10-05 17:07:12 -070038GraphicBufferAllocator::GraphicBufferAllocator()
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039 : 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
Mathias Agopian3330b202009-10-05 17:07:12 -070049GraphicBufferAllocator::~GraphicBufferAllocator()
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050{
51 gralloc_close(mAllocDev);
52}
53
Mathias Agopian3330b202009-10-05 17:07:12 -070054void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055{
56 Mutex::Autolock _l(sLock);
57 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
58 size_t total = 0;
Erik Gilling1d21a9c2010-12-01 16:38:01 -080059 const size_t SIZE = 4096;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060 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));
Mathias Agopian5629eb12010-04-15 14:57:39 -070066 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %2d | 0x%08x\n",
Mathias Agopian0926f502009-05-04 14:17:04 -070067 list.keyAt(i), rec.size/1024.0f,
Mathias Agopian5629eb12010-04-15 14:57:39 -070068 rec.w, rec.s, rec.h, rec.format, rec.usage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 result.append(buffer);
70 total += rec.size;
71 }
72 snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
73 result.append(buffer);
Erik Gilling1d21a9c2010-12-01 16:38:01 -080074 if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
75 mAllocDev->dump(mAllocDev, buffer, SIZE);
76 result.append(buffer);
77 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078}
79
Mathias Agopian678bdd62010-12-03 17:33:09 -080080void GraphicBufferAllocator::dumpToSystemLog()
81{
82 String8 s;
83 GraphicBufferAllocator::getInstance().dump(s);
84 LOGD("%s", s.string());
85}
86
Mathias Agopian3330b202009-10-05 17:07:12 -070087status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 int usage, buffer_handle_t* handle, int32_t* stride)
89{
Mathias Agopian5629eb12010-04-15 14:57:39 -070090 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
91 // allowed from an API stand-point allocate a 1x1 buffer instead.
92 if (!w || !h)
93 w = h = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -070094
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095 // we have a h/w allocator and h/w buffer is requested
Mathias Agopianb26af232009-10-05 18:19:57 -070096 status_t err;
97
98 if (usage & GRALLOC_USAGE_HW_MASK) {
99 err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
100 } else {
101 err = sw_gralloc_handle_t::alloc(w, h, format, usage, handle, stride);
102 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700103
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104 LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
105 w, h, format, usage, err, strerror(-err));
106
107 if (err == NO_ERROR) {
108 Mutex::Autolock _l(sLock);
109 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
110 alloc_rec_t rec;
111 rec.w = w;
112 rec.h = h;
Mathias Agopian5629eb12010-04-15 14:57:39 -0700113 rec.s = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 rec.format = format;
115 rec.usage = usage;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116 rec.size = h * stride[0] * bytesPerPixel(format);
117 list.add(*handle, rec);
118 }
119
120 return err;
121}
122
Mathias Agopian3330b202009-10-05 17:07:12 -0700123status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700124{
Mathias Agopianb26af232009-10-05 18:19:57 -0700125 status_t err;
126 if (sw_gralloc_handle_t::validate(handle) < 0) {
127 err = mAllocDev->free(mAllocDev, handle);
128 } else {
129 err = sw_gralloc_handle_t::free((sw_gralloc_handle_t*)handle);
130 }
131
Mathias Agopian4243e662009-04-15 18:34:24 -0700132 LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 if (err == NO_ERROR) {
134 Mutex::Autolock _l(sLock);
135 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
136 list.removeItem(handle);
137 }
138
139 return err;
140}
141
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142// ---------------------------------------------------------------------------
143}; // namespace android