blob: fe99de1dd93c77e26498389569511c43d61526db [file] [log] [blame]
Dan Stoza303b9a52014-11-17 12:03:59 -08001/*
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002**
3** Copyright 2009, The Android Open Source Project
4**
Dan Stoza303b9a52014-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 Stoza303b9a52014-11-17 12:03:59 -08009** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070010**
Dan Stoza303b9a52014-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
18#ifndef ANDROID_BUFFER_ALLOCATOR_H
19#define ANDROID_BUFFER_ALLOCATOR_H
20
21#include <stdint.h>
22
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080023#include <memory>
24#include <string>
25
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026#include <cutils/native_handle.h>
27
Chia-I Wucb8405e2017-04-17 15:20:19 -070028#include <system/window.h>
29
30#include <ui/PixelFormat.h>
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <utils/Errors.h>
33#include <utils/KeyedVector.h>
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080034#include <utils/Mutex.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035#include <utils/Singleton.h>
36
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037namespace android {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
Chia-I Wu9ba189d2016-09-22 17:13:08 +080039namespace Gralloc2 {
40class Allocator;
41}
42
Chia-I Wu9ba189d2016-09-22 17:13:08 +080043class GraphicBufferMapper;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044class String8;
45
Mathias Agopian3330b202009-10-05 17:07:12 -070046class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047{
48public:
Mathias Agopian3330b202009-10-05 17:07:12 -070049 static inline GraphicBufferAllocator& get() { return getInstance(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070050
Dan Stoza8deb4da2016-06-01 18:21:44 -070051 status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
Chris Forbes82c04982017-04-19 14:29:54 -070052 uint32_t layerCount, uint64_t usage,
Craig Donnere96a3252017-02-02 12:13:34 -080053 buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
Craig Donner6ebc46a2016-10-21 15:23:44 -070054 std::string requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055
56 status_t free(buffer_handle_t handle);
57
Mathias Agopian076b1cc2009-04-10 14:24:30 -070058 void dump(String8& res) const;
Mathias Agopian678bdd62010-12-03 17:33:09 -080059 static void dumpToSystemLog();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060
61private:
62 struct alloc_rec_t {
Dan Stoza303b9a52014-11-17 12:03:59 -080063 uint32_t width;
64 uint32_t height;
65 uint32_t stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066 PixelFormat format;
Craig Donner6ebc46a2016-10-21 15:23:44 -070067 uint32_t layerCount;
Chris Forbes82c04982017-04-19 14:29:54 -070068 uint64_t usage;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 size_t size;
Dan Stoza024e9312016-08-24 12:17:29 -070070 std::string requestorName;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 };
Dan Stoza303b9a52014-11-17 12:03:59 -080072
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073 static Mutex sLock;
74 static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
Dan Stoza303b9a52014-11-17 12:03:59 -080075
Mathias Agopian3330b202009-10-05 17:07:12 -070076 friend class Singleton<GraphicBufferAllocator>;
77 GraphicBufferAllocator();
78 ~GraphicBufferAllocator();
Dan Stoza303b9a52014-11-17 12:03:59 -080079
Chia-I Wu9ba189d2016-09-22 17:13:08 +080080 GraphicBufferMapper& mMapper;
Chia-I Wu5bac7f32017-04-06 12:34:32 -070081 const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082};
83
84// ---------------------------------------------------------------------------
85}; // namespace android
86
87#endif // ANDROID_BUFFER_ALLOCATOR_H