blob: 9655dd257d0ffb00441e9d3a257f9bc3268af18b [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
Mark Salyzyna5e161b2016-09-29 08:08:05 -070021#include <android/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>
Dan Stozaf62eaf52016-06-01 18:21:44 -070028#include <ui/Gralloc1On0Adapter.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030namespace android {
31// ---------------------------------------------------------------------------
32
Mathias Agopian3330b202009-10-05 17:07:12 -070033ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
Mathias Agopian4243e662009-04-15 18:34:24 -070034
Mathias Agopian3330b202009-10-05 17:07:12 -070035Mutex GraphicBufferAllocator::sLock;
Mathias Agopianb26af232009-10-05 18:19:57 -070036KeyedVector<buffer_handle_t,
37 GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
Mathias Agopian3330b202009-10-05 17:07:12 -070039GraphicBufferAllocator::GraphicBufferAllocator()
Dan Stozaf62eaf52016-06-01 18:21:44 -070040 : mLoader(std::make_unique<Gralloc1::Loader>()),
41 mDevice(mLoader->getDevice()) {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042
Dan Stozaf62eaf52016-06-01 18:21:44 -070043GraphicBufferAllocator::~GraphicBufferAllocator() {}
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044
Mathias Agopian3330b202009-10-05 17:07:12 -070045void GraphicBufferAllocator::dump(String8& result) const
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046{
47 Mutex::Autolock _l(sLock);
48 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
49 size_t total = 0;
Erik Gilling1d21a9c2010-12-01 16:38:01 -080050 const size_t SIZE = 4096;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051 char buffer[SIZE];
52 snprintf(buffer, SIZE, "Allocated buffers:\n");
53 result.append(buffer);
54 const size_t c = list.size();
55 for (size_t i=0 ; i<c ; i++) {
56 const alloc_rec_t& rec(list.valueAt(i));
Mathias Agopiana947de82011-07-29 16:35:41 -070057 if (rec.size) {
Dan Stozaf62eaf52016-06-01 18:21:44 -070058 snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070059 list.keyAt(i), rec.size/1024.0f,
Dan Stozaf62eaf52016-06-01 18:21:44 -070060 rec.width, rec.stride, rec.height, rec.format, rec.usage,
61 rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070062 } else {
Dan Stozaf62eaf52016-06-01 18:21:44 -070063 snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x | %s\n",
Mathias Agopiana947de82011-07-29 16:35:41 -070064 list.keyAt(i),
Dan Stozaf62eaf52016-06-01 18:21:44 -070065 rec.width, rec.stride, rec.height, rec.format, rec.usage,
66 rec.requestorName.c_str());
Mathias Agopiana947de82011-07-29 16:35:41 -070067 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 result.append(buffer);
69 total += rec.size;
70 }
Mathias Agopiana947de82011-07-29 16:35:41 -070071 snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072 result.append(buffer);
Dan Stozaf62eaf52016-06-01 18:21:44 -070073 std::string deviceDump = mDevice->dump();
74 result.append(deviceDump.c_str(), deviceDump.size());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075}
76
Mathias Agopian678bdd62010-12-03 17:33:09 -080077void GraphicBufferAllocator::dumpToSystemLog()
78{
79 String8 s;
80 GraphicBufferAllocator::getInstance().dump(s);
Steve Block9d453682011-12-20 16:23:08 +000081 ALOGD("%s", s.string());
Mathias Agopian678bdd62010-12-03 17:33:09 -080082}
83
Dan Stozaf62eaf52016-06-01 18:21:44 -070084status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height,
Dan Stozad3182402014-11-17 12:03:59 -080085 PixelFormat format, uint32_t usage, buffer_handle_t* handle,
Dan Stozaf62eaf52016-06-01 18:21:44 -070086 uint32_t* stride, uint64_t graphicBufferId, std::string requestorName)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087{
Mathias Agopiancf563192012-02-29 20:43:29 -080088 ATRACE_CALL();
Dan Stozad3182402014-11-17 12:03:59 -080089
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.
Dan Stozad3182402014-11-17 12:03:59 -080092 if (!width || !height)
93 width = height = 1;
Mathias Agopiancbb288b2009-09-07 16:32:45 -070094
Dan Stoza24fa67f2015-05-29 12:48:04 -070095 // Filter out any usage bits that should not be passed to the gralloc module
96 usage &= GRALLOC_USAGE_ALLOC_MASK;
97
Dan Stozaf62eaf52016-06-01 18:21:44 -070098 auto descriptor = mDevice->createDescriptor();
99 auto error = descriptor->setDimensions(width, height);
100 if (error != GRALLOC1_ERROR_NONE) {
101 ALOGE("Failed to set dimensions to (%u, %u): %d", width, height, error);
102 return BAD_VALUE;
103 }
104 error = descriptor->setFormat(static_cast<android_pixel_format_t>(format));
105 if (error != GRALLOC1_ERROR_NONE) {
106 ALOGE("Failed to set format to %d: %d", format, error);
107 return BAD_VALUE;
108 }
109 error = descriptor->setProducerUsage(
110 static_cast<gralloc1_producer_usage_t>(usage));
111 if (error != GRALLOC1_ERROR_NONE) {
112 ALOGE("Failed to set producer usage to %u: %d", usage, error);
113 return BAD_VALUE;
114 }
115 error = descriptor->setConsumerUsage(
116 static_cast<gralloc1_consumer_usage_t>(usage));
117 if (error != GRALLOC1_ERROR_NONE) {
118 ALOGE("Failed to set consumer usage to %u: %d", usage, error);
119 return BAD_VALUE;
120 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700121
Dan Stozaf62eaf52016-06-01 18:21:44 -0700122 error = mDevice->allocate(descriptor, graphicBufferId, handle);
123 if (error != GRALLOC1_ERROR_NONE) {
124 ALOGE("Failed to allocate (%u x %u) format %d usage %u: %d",
125 width, height, format, usage, error);
126 return NO_MEMORY;
127 }
Dan Stozad3182402014-11-17 12:03:59 -0800128
Dan Stozaf62eaf52016-06-01 18:21:44 -0700129 error = mDevice->getStride(*handle, stride);
130 if (error != GRALLOC1_ERROR_NONE) {
131 ALOGW("Failed to get stride from buffer: %d", error);
132 }
133
134 if (error == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135 Mutex::Autolock _l(sLock);
136 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
Dan Stozad3182402014-11-17 12:03:59 -0800137 uint32_t bpp = bytesPerPixel(format);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 alloc_rec_t rec;
Dan Stozad3182402014-11-17 12:03:59 -0800139 rec.width = width;
140 rec.height = height;
141 rec.stride = *stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142 rec.format = format;
143 rec.usage = usage;
Dan Stozad3182402014-11-17 12:03:59 -0800144 rec.size = static_cast<size_t>(height * (*stride) * bpp);
Dan Stozaf62eaf52016-06-01 18:21:44 -0700145 rec.requestorName = std::move(requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700146 list.add(*handle, rec);
147 }
148
Dan Stozaf62eaf52016-06-01 18:21:44 -0700149 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700150}
151
Mathias Agopian3330b202009-10-05 17:07:12 -0700152status_t GraphicBufferAllocator::free(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153{
Mathias Agopiancf563192012-02-29 20:43:29 -0800154 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800155
Dan Stozaf62eaf52016-06-01 18:21:44 -0700156 auto error = mDevice->release(handle);
157 if (error != GRALLOC1_ERROR_NONE) {
158 ALOGE("Failed to free buffer: %d", error);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 }
160
Dan Stozaf62eaf52016-06-01 18:21:44 -0700161 Mutex::Autolock _l(sLock);
162 KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
163 list.removeItem(handle);
164
165 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166}
167
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700168// ---------------------------------------------------------------------------
169}; // namespace android