blob: 0677d96172f9fee7b54e27d73fe0c0eba7563501 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmede69031e2016-11-22 20:05:16 -05002 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303 * Not a Contribution
4 *
5 * Copyright (C) 2008 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#ifndef __GR_BUF_MGR_H__
21#define __GR_BUF_MGR_H__
22
23#include <pthread.h>
24#include <unordered_map>
Naseer Ahmede69031e2016-11-22 20:05:16 -050025#include <unordered_set>
26#include <utility>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053027#include <mutex>
28
29#include "gralloc_priv.h"
30#include "gr_allocator.h"
Naseer Ahmede69031e2016-11-22 20:05:16 -050031#include "gr_buf_descriptor.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053032
33namespace gralloc1 {
34
35class BufferManager {
36 public:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053037 ~BufferManager();
Naseer Ahmede69031e2016-11-22 20:05:16 -050038 gralloc1_error_t CreateBufferDescriptor(gralloc1_buffer_descriptor_t *descriptor_id);
39 gralloc1_error_t DestroyBufferDescriptor(gralloc1_buffer_descriptor_t descriptor_id);
40 gralloc1_error_t AllocateBuffers(uint32_t num_descriptors,
41 const gralloc1_buffer_descriptor_t *descriptor_ids,
42 buffer_handle_t *out_buffers);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043 gralloc1_error_t RetainBuffer(private_handle_t const *hnd);
44 gralloc1_error_t ReleaseBuffer(private_handle_t const *hnd);
45 gralloc1_error_t LockBuffer(const private_handle_t *hnd, gralloc1_producer_usage_t prod_usage,
46 gralloc1_consumer_usage_t cons_usage);
47 gralloc1_error_t UnlockBuffer(const private_handle_t *hnd);
48 gralloc1_error_t Perform(int operation, va_list args);
Naseer Ahmede69031e2016-11-22 20:05:16 -050049 gralloc1_error_t GetFlexLayout(const private_handle_t *hnd, struct android_flex_layout *layout);
50 gralloc1_error_t GetNumFlexPlanes(const private_handle_t *hnd, uint32_t *out_num_planes);
Naseer Ahmeddc918132017-03-07 15:25:14 -050051 gralloc1_error_t Dump(std::ostringstream *os);
Naseer Ahmede69031e2016-11-22 20:05:16 -050052
53 template <typename... Args>
54 gralloc1_error_t CallBufferDescriptorFunction(gralloc1_buffer_descriptor_t descriptor_id,
55 void (BufferDescriptor::*member)(Args...),
56 Args... args) {
57 std::lock_guard<std::mutex> lock(locker_);
58 const auto map_descriptor = descriptors_map_.find(descriptor_id);
59 if (map_descriptor == descriptors_map_.end()) {
60 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
61 }
62 const auto descriptor = map_descriptor->second;
63 (descriptor.get()->*member)(std::forward<Args>(args)...);
64 return GRALLOC1_ERROR_NONE;
65 }
66
67 static BufferManager* GetInstance() {
68 static BufferManager *instance = new BufferManager();
69 return instance;
70 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053071
72 private:
Naseer Ahmede69031e2016-11-22 20:05:16 -050073 BufferManager();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053074 gralloc1_error_t MapBuffer(private_handle_t const *hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053075 int GetBufferType(int format);
76 int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
77 unsigned int bufferSize = 0);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070078 int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
79 int unaligned_h, int format, int bufferType,
80 gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
81 buffer_handle_t *handle);
Naseer Ahmede69031e2016-11-22 20:05:16 -050082 uint32_t GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083 gralloc1_consumer_usage_t cons_usage);
84 int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
85 gralloc1_consumer_usage_t cons_usage);
86 void CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
87 buffer_handle_t *out_buffer);
88
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040089 // Imports the ion fds into the current process. Returns an error for invalid handles
90 gralloc1_error_t ImportHandle(private_handle_t* hnd);
91
92 // Creates a Buffer from the valid private handle and adds it to the map
93 void RegisterHandle(const private_handle_t *hnd, int ion_handle, int ion_handle_meta);
94
Naseer Ahmede69031e2016-11-22 20:05:16 -050095 // Wrapper structure over private handle
96 // Values associated with the private handle
97 // that do not need to go over IPC can be placed here
98 // This structure is also not expected to be ABI stable
99 // unlike private_handle_t
100 struct Buffer {
101 const private_handle_t *handle = nullptr;
102 int ref_count = 1;
103 // Hold the main and metadata ion handles
104 // Freed from the allocator process
105 // and unused in the mapping process
106 int ion_handle_main = -1;
107 int ion_handle_meta = -1;
108
109 Buffer() = delete;
110 explicit Buffer(const private_handle_t* h, int ih_main = -1, int ih_meta = -1):
111 handle(h),
112 ion_handle_main(ih_main),
113 ion_handle_meta(ih_meta) {
114 }
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400115 void IncRef() { ++ref_count; }
116 bool DecRef() { return --ref_count == 0; }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500117 };
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400118
Naseer Ahmede69031e2016-11-22 20:05:16 -0500119 gralloc1_error_t FreeBuffer(std::shared_ptr<Buffer> buf);
120
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400121 // Get the wrapper Buffer object from the handle, returns nullptr if handle is not found
122 std::shared_ptr<Buffer> GetBufferFromHandle(const private_handle_t *hnd);
123
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530124 bool map_fb_mem_ = false;
125 bool ubwc_for_fb_ = false;
126 Allocator *allocator_ = NULL;
127 std::mutex locker_;
Naseer Ahmedef287dc2017-03-11 23:48:43 -0500128 // TODO(user): The private_handle_t is used as a key because the unique ID generated
129 // from next_id_ is not unique across processes. The correct way to resolve this would
130 // be to use the allocator over hwbinder
131 std::unordered_map<const private_handle_t*, std::shared_ptr<Buffer>> handles_map_ = {};
Naseer Ahmede69031e2016-11-22 20:05:16 -0500132 std::unordered_map<gralloc1_buffer_descriptor_t,
133 std::shared_ptr<BufferDescriptor>> descriptors_map_ = {};
134 std::atomic<uint64_t> next_id_;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530135};
136
137} // namespace gralloc1
138
139#endif // __GR_BUF_MGR_H__