blob: 9b265af2b4ed156e5c2e4a7f68f2e1efb9d5aae5 [file] [log] [blame]
Dan Stozad3182402014-11-17 12:03:59 -08001/*
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002**
3** Copyright 2009, The Android Open Source Project
4**
Dan Stozad3182402014-11-17 12:03:59 -08005** 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -07008**
Dan Stozad3182402014-11-17 12:03:59 -08009** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070010**
Dan Stozad3182402014-11-17 12:03:59 -080011** 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070015** limitations under the License.
16*/
17
Mathias Agopian5629eb12010-04-15 14:57:39 -070018#define LOG_TAG "GraphicBufferAllocator"
Mathias Agopiancf563192012-02-29 20:43:29 -080019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopian5629eb12010-04-15 14:57:39 -070020
Mathias Agopian076b1cc2009-04-10 14:24:30 -070021#include <cutils/log.h>
Mathias Agopian4243e662009-04-15 18:34:24 -070022
23#include <utils/Singleton.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/String8.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080025#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <ui/GraphicBufferAllocator.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029namespace 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);
Steve Blocke6f43dd2012-01-06 19:20:56 +000043 ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044 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 Agopiana947de82011-07-29 16:35:41 -070066 if (rec.size) {
67 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
68 list.keyAt(i), rec.size/1024.0f,
Dan Stozad3182402014-11-17 12:03:59 -080069 rec.width, rec.stride, rec.height, rec.format, rec.usage);
Mathias Agopiana947de82011-07-29 16:35:41 -070070 } else {
71 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n",
72 list.keyAt(i),
Dan Stozad3182402014-11-17 12:03:59 -080073 rec.width, rec.stride, rec.height, rec.format, rec.usage);
Mathias Agopiana947de82011-07-29 16:35:41 -070074 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075 result.append(buffer);
76 total += rec.size;
77 }
Mathias Agopiana947de82011-07-29 16:35:41 -070078 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070079 result.append(buffer);
Erik Gilling1d21a9c2010-12-01 16:38:01 -080080 if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
81 mAllocDev->dump(mAllocDev, buffer, SIZE);
82 result.append(buffer);
83 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084}
85
Mathias Agopian678bdd62010-12-03 17:33:09 -080086void GraphicBufferAllocator::dumpToSystemLog()
87{
88 String8 s;
89 GraphicBufferAllocator::getInstance().dump(s);
Steve Block9d453682011-12-20 16:23:08 +000090 ALOGD("%s", s.string());
Mathias Agopian678bdd62010-12-03 17:33:09 -080091}
92
Dan Stozad3182402014-11-17 12:03:59 -080093status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
94 PixelFormat format, uint32_t usage, buffer_handle_t* handle,
95 uint32_t* stride)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096{
Mathias Agopiancf563192012-02-29 20:43:29 -080097 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -080098
Mathias Agopian5629eb12010-04-15 14:57:39 -070099 // make sure to not allocate a N x 0 or 0 x N buffer, since this is
100 // allowed from an API stand-point allocate a 1x1 buffer instead.
Dan Stozad3182402014-11-17 12:03:59 -0800101 if (!width || !height)
102 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700103
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104 // we have a h/w allocator and h/w buffer is requested
Dan Stozad3182402014-11-17 12:03:59 -0800105 status_t err;
106
Dan Stoza24fa67f2015-05-29 12:48:04 -0700107 // Filter out any usage bits that should not be passed to the gralloc module
108 usage &= GRALLOC_USAGE_ALLOC_MASK;
109
Dan Stozad3182402014-11-17 12:03:59 -0800110 int outStride = 0;
111 err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
112 static_cast<int>(height), format, static_cast<int>(usage), handle,
113 &outStride);
114 *stride = static_cast<uint32_t>(outStride);
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700115
Steve Block32397c12012-01-05 23:22:43 +0000116 ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
Dan Stozad3182402014-11-17 12:03:59 -0800117 width, height, format, usage, err, strerror(-err));
118
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119 if (err == NO_ERROR) {
120 Mutex::Autolock _l(sLock);
121 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800122 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700123 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800124 rec.width = width;
125 rec.height = height;
126 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700127 rec.format = format;
128 rec.usage = usage;
Dan Stozad3182402014-11-17 12:03:59 -0800129 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 list.add(*handle, rec);
131 }
132
133 return err;
134}
135
Mathias Agopian3330b202009-10-05 17:07:12 -0700136status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137{
Mathias Agopiancf563192012-02-29 20:43:29 -0800138 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -0700139 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -0800140
141 err = mAllocDev->free(mAllocDev, handle);
Mathias Agopianb26af232009-10-05 18:19:57 -0700142
Steve Block32397c12012-01-05 23:22:43 +0000143 ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144 if (err == NO_ERROR) {
145 Mutex::Autolock _l(sLock);
146 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
147 list.removeItem(handle);
148 }
149
150 return err;
151}
152
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153// ---------------------------------------------------------------------------
154}; // namespace android