1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <utils/NativeHandle.h>
20 
21 #include <memory>
22 #include <string>
23 #include <unordered_set>
24 #include <utility>
25 
26 namespace android {
27 
28 class IGrallocHalWrapper;
29 
30 // Reference: hardware/interfaces/graphics/mapper/2.0/vts/functional/
31 class GrallocWrapper {
32    public:
33     GrallocWrapper();
34     ~GrallocWrapper();
35 
36     // After constructing this object, this function must be called to check the result. If it
37     // returns false, other methods are not safe to call.
isInitialized()38     bool isInitialized() const { return (mGrallocHal != nullptr); };
39 
40     std::string dumpDebugInfo();
41 
42     // Allocates a gralloc buffer suitable for direct channel sensors usage with the given size.
43     // The buffer should be freed using freeBuffer when it's not needed anymore; otherwise it'll
44     // be freed when this object is destroyed.
45     // Returns a handle to the buffer, and a CPU-accessible pointer for reading. On failure, both
46     // will be set to nullptr.
47     std::pair<native_handle_t*, void*> allocate(uint32_t size);
48 
49     // Releases a gralloc buffer previously returned by allocate()
50     void freeBuffer(native_handle_t* bufferHandle);
51 
52   private:
53     std::unique_ptr<IGrallocHalWrapper> mGrallocHal;
54 
55     // Keep track of all cloned and imported handles.  When a test fails with
56     // ASSERT_*, the destructor will free the handles for the test.
57     std::unordered_set<native_handle_t*> mAllocatedBuffers;
58 };
59 
60 }  // namespace android
61