blob: 16967d430f88c3e9d55c35d09f5ed53ec9b9ed66 [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
23#include <cutils/native_handle.h>
24
25#include <utils/Errors.h>
26#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Singleton.h>
29
Dan Stoza8deb4da2016-06-01 18:21:44 -070030#include <ui/Gralloc1.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <ui/PixelFormat.h>
32
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033namespace android {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034
Chia-I Wu9ba189d2016-09-22 17:13:08 +080035namespace Gralloc2 {
36class Allocator;
37}
38
Dan Stoza8deb4da2016-06-01 18:21:44 -070039class Gralloc1Loader;
Chia-I Wu9ba189d2016-09-22 17:13:08 +080040class GraphicBufferMapper;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041class String8;
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044{
45public:
46 enum {
Dan Stoza8deb4da2016-06-01 18:21:44 -070047 USAGE_SW_READ_NEVER = GRALLOC1_CONSUMER_USAGE_CPU_READ_NEVER,
48 USAGE_SW_READ_RARELY = GRALLOC1_CONSUMER_USAGE_CPU_READ,
49 USAGE_SW_READ_OFTEN = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
50 USAGE_SW_READ_MASK = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
Dan Stoza303b9a52014-11-17 12:03:59 -080051
Dan Stoza8deb4da2016-06-01 18:21:44 -070052 USAGE_SW_WRITE_NEVER = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_NEVER,
53 USAGE_SW_WRITE_RARELY = GRALLOC1_PRODUCER_USAGE_CPU_WRITE,
54 USAGE_SW_WRITE_OFTEN = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
55 USAGE_SW_WRITE_MASK = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
Dan Stoza303b9a52014-11-17 12:03:59 -080056
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Dan Stoza303b9a52014-11-17 12:03:59 -080058
Dan Stoza8deb4da2016-06-01 18:21:44 -070059 USAGE_HW_TEXTURE = GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE,
60 USAGE_HW_RENDER = GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET,
61 USAGE_HW_2D = 0x00000400, // Deprecated
62 USAGE_HW_MASK = 0x00071F00, // Deprecated
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063 };
64
Mathias Agopian3330b202009-10-05 17:07:12 -070065 static inline GraphicBufferAllocator& get() { return getInstance(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066
Dan Stoza8deb4da2016-06-01 18:21:44 -070067 status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
Craig Donner6ebc46a2016-10-21 15:23:44 -070068 uint32_t layerCount, uint32_t usage, buffer_handle_t* handle,
69 uint32_t* stride, uint64_t graphicBufferId,
70 std::string requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071
72 status_t free(buffer_handle_t handle);
73
Mathias Agopian076b1cc2009-04-10 14:24:30 -070074 void dump(String8& res) const;
Mathias Agopian678bdd62010-12-03 17:33:09 -080075 static void dumpToSystemLog();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076
77private:
78 struct alloc_rec_t {
Dan Stoza303b9a52014-11-17 12:03:59 -080079 uint32_t width;
80 uint32_t height;
81 uint32_t stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082 PixelFormat format;
Craig Donner6ebc46a2016-10-21 15:23:44 -070083 uint32_t layerCount;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 uint32_t usage;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070085 size_t size;
Dan Stoza024e9312016-08-24 12:17:29 -070086 std::string requestorName;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070087 };
Dan Stoza303b9a52014-11-17 12:03:59 -080088
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089 static Mutex sLock;
90 static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
Dan Stoza303b9a52014-11-17 12:03:59 -080091
Mathias Agopian3330b202009-10-05 17:07:12 -070092 friend class Singleton<GraphicBufferAllocator>;
93 GraphicBufferAllocator();
94 ~GraphicBufferAllocator();
Dan Stoza303b9a52014-11-17 12:03:59 -080095
Chia-I Wu9ba189d2016-09-22 17:13:08 +080096 const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
97 GraphicBufferMapper& mMapper;
98
Dan Stoza8deb4da2016-06-01 18:21:44 -070099 std::unique_ptr<Gralloc1::Loader> mLoader;
100 std::unique_ptr<Gralloc1::Device> mDevice;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101};
102
103// ---------------------------------------------------------------------------
104}; // namespace android
105
106#endif // ANDROID_BUFFER_ALLOCATOR_H