GraphicBufferAllocator.cpp revision 1d21a9cafc534c34a2f28c985c4c7aa176d0e67b
1076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian/*
2076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
3076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Copyright 2009, The Android Open Source Project
4076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
5076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
6076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** you may not use this file except in compliance with the License.
7076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** You may obtain a copy of the License at
8076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
9076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
10076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
11076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Unless required by applicable law or agreed to in writing, software
12076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
13076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** See the License for the specific language governing permissions and
15076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** limitations under the License.
16076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian*/
17076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
185629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian#define LOG_TAG "GraphicBufferAllocator"
195629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian
20076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/log.h>
214243e666213029a293935987c979831093fb0779Mathias Agopian
224243e666213029a293935987c979831093fb0779Mathias Agopian#include <utils/Singleton.h>
23076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/String8.h>
24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
253330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferAllocator.h>
26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
27b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian#include <private/ui/sw_gralloc_handle.h>
28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
29076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace android {
30076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// ---------------------------------------------------------------------------
31076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
323330b203039dea366d4981db1408a460134b2d2cMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
334243e666213029a293935987c979831093fb0779Mathias Agopian
343330b203039dea366d4981db1408a460134b2d2cMathias AgopianMutex GraphicBufferAllocator::sLock;
35b26af23744fa73e8bc142b1eb98772fde5970c10Mathias AgopianKeyedVector<buffer_handle_t,
36b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
37076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
383330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::GraphicBufferAllocator()
39076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    : mAllocDev(0)
40076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
41076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    hw_module_t const* module;
42076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
43076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
44076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    if (err == 0) {
45076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        gralloc_open(module, &mAllocDev);
46076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
47076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
48076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
493330b203039dea366d4981db1408a460134b2d2cMathias AgopianGraphicBufferAllocator::~GraphicBufferAllocator()
50076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
51076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    gralloc_close(mAllocDev);
52076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
53076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
543330b203039dea366d4981db1408a460134b2d2cMathias Agopianvoid GraphicBufferAllocator::dump(String8& result) const
55076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
56076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    Mutex::Autolock _l(sLock);
57076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
58076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    size_t total = 0;
591d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    const size_t SIZE = 4096;
60076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    char buffer[SIZE];
61076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    snprintf(buffer, SIZE, "Allocated buffers:\n");
62076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    result.append(buffer);
63076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    const size_t c = list.size();
64076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    for (size_t i=0 ; i<c ; i++) {
65076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        const alloc_rec_t& rec(list.valueAt(i));
665629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian        snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %2d | 0x%08x\n",
670926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian            list.keyAt(i), rec.size/1024.0f,
685629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian            rec.w, rec.s, rec.h, rec.format, rec.usage);
69076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        result.append(buffer);
70076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        total += rec.size;
71076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
72076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
73076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    result.append(buffer);
741d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
751d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mAllocDev->dump(mAllocDev, buffer, SIZE);
761d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
771d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
78076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
79076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
80678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopianvoid GraphicBufferAllocator::dumpToSystemLog()
81678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian{
82678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    String8 s;
83678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    GraphicBufferAllocator::getInstance().dump(s);
84678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    LOGD("%s", s.string());
85678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian}
86678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian
873330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
88076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        int usage, buffer_handle_t* handle, int32_t* stride)
89076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
905629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian    // make sure to not allocate a N x 0 or 0 x N buffer, since this is
915629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian    // allowed from an API stand-point allocate a 1x1 buffer instead.
925629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian    if (!w || !h)
935629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian        w = h = 1;
94cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
95076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    // we have a h/w allocator and h/w buffer is requested
96b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    status_t err;
97b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian
98b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    if (usage & GRALLOC_USAGE_HW_MASK) {
99b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian        err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
100b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    } else {
101b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian        err = sw_gralloc_handle_t::alloc(w, h, format, usage, handle, stride);
102b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    }
103cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
104076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
105076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian            w, h, format, usage, err, strerror(-err));
106076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
107076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    if (err == NO_ERROR) {
108076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        Mutex::Autolock _l(sLock);
109076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
110076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        alloc_rec_t rec;
111076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.w = w;
112076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.h = h;
1135629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian        rec.s = *stride;
114076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.format = format;
115076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.usage = usage;
116076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.size = h * stride[0] * bytesPerPixel(format);
117076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        list.add(*handle, rec);
118076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
119076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
120076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    return err;
121076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
122076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
1233330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::free(buffer_handle_t handle)
124076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
125b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    status_t err;
126b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    if (sw_gralloc_handle_t::validate(handle) < 0) {
127b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian        err = mAllocDev->free(mAllocDev, handle);
128b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    } else {
129b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian        err = sw_gralloc_handle_t::free((sw_gralloc_handle_t*)handle);
130b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    }
131b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian
1324243e666213029a293935987c979831093fb0779Mathias Agopian    LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
133076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    if (err == NO_ERROR) {
134076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        Mutex::Autolock _l(sLock);
135076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
136076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        list.removeItem(handle);
137076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
138076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
139076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    return err;
140076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
141076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
142076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// ---------------------------------------------------------------------------
143076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}; // namespace android
144