blob: 414ec382a75c9ea6c24522a7ac75424e3c3c6dc2 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmede36f2242017-12-01 15:33:56 -05002 * Copyright (c) 2011-2018, 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>
Naseer Ahmede36f2242017-12-01 15:33:56 -050024#include <mutex>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053025#include <unordered_map>
Naseer Ahmede69031e2016-11-22 20:05:16 -050026#include <unordered_set>
27#include <utility>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053028
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053029#include "gr_allocator.h"
Naseer Ahmede69031e2016-11-22 20:05:16 -050030#include "gr_buf_descriptor.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050031#include "gralloc_priv.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053032
Naseer Ahmede36f2242017-12-01 15:33:56 -050033namespace gralloc {
34
35using android::hardware::graphics::mapper::V2_0::Error;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053036
37class BufferManager {
38 public:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053039 ~BufferManager();
Naseer Ahmede69031e2016-11-22 20:05:16 -050040
Naseer Ahmede36f2242017-12-01 15:33:56 -050041 Error AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
42 unsigned int bufferSize = 0);
43 Error RetainBuffer(private_handle_t const *hnd);
44 Error ReleaseBuffer(private_handle_t const *hnd);
45 Error LockBuffer(const private_handle_t *hnd, uint64_t usage);
46 Error UnlockBuffer(const private_handle_t *hnd);
47 Error Dump(std::ostringstream *os);
48 static BufferManager *GetInstance();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053049
50 private:
Naseer Ahmede69031e2016-11-22 20:05:16 -050051 BufferManager();
Naseer Ahmede36f2242017-12-01 15:33:56 -050052 Error MapBuffer(private_handle_t const *hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053053 int GetBufferType(int format);
Naseer Ahmede36f2242017-12-01 15:33:56 -050054 int GetHandleFlags(int format, uint64_t usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053055
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040056 // Imports the ion fds into the current process. Returns an error for invalid handles
Naseer Ahmede36f2242017-12-01 15:33:56 -050057 Error ImportHandleLocked(private_handle_t *hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040058
59 // Creates a Buffer from the valid private handle and adds it to the map
Naseer Ahmed378d8582017-03-28 21:56:08 -040060 void RegisterHandleLocked(const private_handle_t *hnd, int ion_handle, int ion_handle_meta);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040061
Naseer Ahmede69031e2016-11-22 20:05:16 -050062 // Wrapper structure over private handle
63 // Values associated with the private handle
64 // that do not need to go over IPC can be placed here
65 // This structure is also not expected to be ABI stable
66 // unlike private_handle_t
67 struct Buffer {
68 const private_handle_t *handle = nullptr;
69 int ref_count = 1;
70 // Hold the main and metadata ion handles
71 // Freed from the allocator process
72 // and unused in the mapping process
73 int ion_handle_main = -1;
74 int ion_handle_meta = -1;
75
76 Buffer() = delete;
Naseer Ahmede36f2242017-12-01 15:33:56 -050077 explicit Buffer(const private_handle_t *h, int ih_main = -1, int ih_meta = -1)
78 : handle(h), ion_handle_main(ih_main), ion_handle_meta(ih_meta) {}
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040079 void IncRef() { ++ref_count; }
80 bool DecRef() { return --ref_count == 0; }
Naseer Ahmede69031e2016-11-22 20:05:16 -050081 };
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040082
Naseer Ahmede36f2242017-12-01 15:33:56 -050083 Error FreeBuffer(std::shared_ptr<Buffer> buf);
Naseer Ahmede69031e2016-11-22 20:05:16 -050084
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040085 // Get the wrapper Buffer object from the handle, returns nullptr if handle is not found
Naseer Ahmed378d8582017-03-28 21:56:08 -040086 std::shared_ptr<Buffer> GetBufferFromHandleLocked(const private_handle_t *hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040087
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053088 bool map_fb_mem_ = false;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053089 Allocator *allocator_ = NULL;
Naseer Ahmed378d8582017-03-28 21:56:08 -040090 std::mutex buffer_lock_;
Naseer Ahmede36f2242017-12-01 15:33:56 -050091 std::unordered_map<const private_handle_t *, std::shared_ptr<Buffer>> handles_map_ = {};
Naseer Ahmede69031e2016-11-22 20:05:16 -050092 std::atomic<uint64_t> next_id_;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093};
94
Naseer Ahmede36f2242017-12-01 15:33:56 -050095} // namespace gralloc
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096
97#endif // __GR_BUF_MGR_H__