1 /*
2  * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3  * 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 <mutex>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <utility>
28 
29 #include "gr_allocator.h"
30 #include "gr_utils.h"
31 #include "gr_buf_descriptor.h"
32 #include "gralloc_priv.h"
33 
34 namespace gralloc {
35 
36 using android::hardware::graphics::mapper::V2_0::Error;
37 
38 class BufferManager {
39  public:
40   ~BufferManager();
41 
42   Error AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
43                        unsigned int bufferSize = 0);
44   Error RetainBuffer(private_handle_t const *hnd);
45   Error ReleaseBuffer(private_handle_t const *hnd);
46   Error LockBuffer(const private_handle_t *hnd, uint64_t usage);
47   Error UnlockBuffer(const private_handle_t *hnd);
48   Error Dump(std::ostringstream *os);
49   Error ValidateBufferSize(private_handle_t const *hnd, BufferInfo info);
50   Error IsBufferImported(const private_handle_t *hnd);
51   static BufferManager *GetInstance();
52 
53  private:
54   BufferManager();
55   Error MapBuffer(private_handle_t const *hnd);
56   int GetHandleFlags(int format, uint64_t usage);
57 
58   // Imports the ion fds into the current process. Returns an error for invalid handles
59   Error ImportHandleLocked(private_handle_t *hnd);
60 
61   // Creates a Buffer from the valid private handle and adds it to the map
62   void RegisterHandleLocked(const private_handle_t *hnd, int ion_handle, int ion_handle_meta);
63 
64   // Wrapper structure over private handle
65   // Values associated with the private handle
66   // that do not need to go over IPC can be placed here
67   // This structure is also not expected to be ABI stable
68   // unlike private_handle_t
69   struct Buffer {
70     const private_handle_t *handle = nullptr;
71     int ref_count = 1;
72     // Hold the main and metadata ion handles
73     // Freed from the allocator process
74     // and unused in the mapping process
75     int ion_handle_main = -1;
76     int ion_handle_meta = -1;
77 
78     Buffer() = delete;
79     explicit Buffer(const private_handle_t *h, int ih_main = -1, int ih_meta = -1)
80         : handle(h), ion_handle_main(ih_main), ion_handle_meta(ih_meta) {}
IncRefBuffer81     void IncRef() { ++ref_count; }
DecRefBuffer82     bool DecRef() { return --ref_count == 0; }
83   };
84 
85   Error FreeBuffer(std::shared_ptr<Buffer> buf);
86 
87   // Get the wrapper Buffer object from the handle, returns nullptr if handle is not found
88   std::shared_ptr<Buffer> GetBufferFromHandleLocked(const private_handle_t *hnd);
89   Allocator *allocator_ = NULL;
90   std::mutex buffer_lock_;
91   std::unordered_map<const private_handle_t *, std::shared_ptr<Buffer>> handles_map_ = {};
92   std::atomic<uint64_t> next_id_;
93 };
94 
95 }  // namespace gralloc
96 
97 #endif  // __GR_BUF_MGR_H__
98