1303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza/*
2076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
3076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian** Copyright 2009, The Android Open Source Project
4076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
5303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** Licensed under the Apache License, Version 2.0 (the "License");
6303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** you may not use this file except in compliance with the License.
7303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** You may obtain a copy of the License at
8076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
9303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza**     http://www.apache.org/licenses/LICENSE-2.0
10076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian**
11303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** Unless required by applicable law or agreed to in writing, software
12303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** distributed under the License is distributed on an "AS IS" BASIS,
13303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza** 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"
19cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian#define ATRACE_TAG ATRACE_TAG_GRAPHICS
205629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian
21076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/log.h>
224243e666213029a293935987c979831093fb0779Mathias Agopian
234243e666213029a293935987c979831093fb0779Mathias Agopian#include <utils/Singleton.h>
24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <utils/String8.h>
25cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian#include <utils/Trace.h>
26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
273330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBufferAllocator.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);
43e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block    ALOGE_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));
66a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian        if (rec.size) {
67a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian            snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
68a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian                    list.keyAt(i), rec.size/1024.0f,
69303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza                    rec.width, rec.stride, rec.height, rec.format, rec.usage);
70a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian        } else {
71a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian            snprintf(buffer, SIZE, "%10p: unknown     | %4u (%4u) x %4u | %8X | 0x%08x\n",
72a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian                    list.keyAt(i),
73303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza                    rec.width, rec.stride, rec.height, rec.format, rec.usage);
74a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian        }
75076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        result.append(buffer);
76076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        total += rec.size;
77076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
78a947de88e7863859e6d7e825b1d45abb596a08e5Mathias Agopian    snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
79076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    result.append(buffer);
801d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
811d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        mAllocDev->dump(mAllocDev, buffer, SIZE);
821d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling        result.append(buffer);
831d21a9cafc534c34a2f28c985c4c7aa176d0e67bErik Gilling    }
84076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
85076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
86678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopianvoid GraphicBufferAllocator::dumpToSystemLog()
87678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian{
88678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    String8 s;
89678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian    GraphicBufferAllocator::getInstance().dump(s);
909d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block    ALOGD("%s", s.string());
91678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian}
92678bdd6349344df254cc0c3377a40fd99e216635Mathias Agopian
93303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stozastatus_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
94303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        PixelFormat format, uint32_t usage, buffer_handle_t* handle,
95303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        uint32_t* stride)
96076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
97cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian    ATRACE_CALL();
98303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
995629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian    // make sure to not allocate a N x 0 or 0 x N buffer, since this is
1005629eb167638a9ebfa5059177d227c7ac67db46fMathias Agopian    // allowed from an API stand-point allocate a 1x1 buffer instead.
101303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza    if (!width || !height)
102303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        width = height = 1;
103cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
104076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    // we have a h/w allocator and h/w buffer is requested
105303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza    status_t err;
106303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
107b8efdc40039e4900ae2c64fbdfa03906c867ec84Dan Stoza    // Filter out any usage bits that should not be passed to the gralloc module
108b8efdc40039e4900ae2c64fbdfa03906c867ec84Dan Stoza    usage &= GRALLOC_USAGE_ALLOC_MASK;
109b8efdc40039e4900ae2c64fbdfa03906c867ec84Dan Stoza
110303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza    int outStride = 0;
111303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza    err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
112303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza            static_cast<int>(height), format, static_cast<int>(usage), handle,
113303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza            &outStride);
114303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza    *stride = static_cast<uint32_t>(outStride);
115cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
11632397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
117303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza            width, height, format, usage, err, strerror(-err));
118303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza
119076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    if (err == NO_ERROR) {
120076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        Mutex::Autolock _l(sLock);
121076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
122303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        uint32_t bpp = bytesPerPixel(format);
123076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        alloc_rec_t rec;
124303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        rec.width = width;
125303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        rec.height = height;
126303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        rec.stride = *stride;
127076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.format = format;
128076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        rec.usage = usage;
129303b9a51239d36d237a7d40c67b5085cdb3c1059Dan Stoza        rec.size = static_cast<size_t>(height * (*stride) * bpp);
130076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        list.add(*handle, rec);
131076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
132076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
133076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    return err;
134076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
135076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
1363330b203039dea366d4981db1408a460134b2d2cMathias Agopianstatus_t GraphicBufferAllocator::free(buffer_handle_t handle)
137076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian{
138cf56319d4deb2215e5274f321f7fee71caa1ada1Mathias Agopian    ATRACE_CALL();
139b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian    status_t err;
1400a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian
1410a757814f3e4ca4db772144e85b687fe89a7fba5Mathias Agopian    err = mAllocDev->free(mAllocDev, handle);
142b26af23744fa73e8bc142b1eb98772fde5970c10Mathias Agopian
14332397c1cd3327905173b36baa6fd1c579bc328ffSteve Block    ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
144076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    if (err == NO_ERROR) {
145076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        Mutex::Autolock _l(sLock);
146076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
147076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian        list.removeItem(handle);
148076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    }
149076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
150076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian    return err;
151076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
152076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
153076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// ---------------------------------------------------------------------------
154076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}; // namespace android
155