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 // Allocates a gralloc buffer suitable for direct channel sensors usage with the given size. 41 // The buffer should be freed using freeBuffer when it's not needed anymore; otherwise it'll 42 // be freed when this object is destroyed. 43 // Returns a handle to the buffer, and a CPU-accessible pointer for reading. On failure, both 44 // will be set to nullptr. 45 std::pair<native_handle_t*, void*> allocate(uint32_t size); 46 47 // Releases a gralloc buffer previously returned by allocate() 48 void freeBuffer(native_handle_t* bufferHandle); 49 50 private: 51 std::unique_ptr<IGrallocHalWrapper> mGrallocHal; 52 53 // Keep track of all cloned and imported handles. When a test fails with 54 // ASSERT_*, the destructor will free the handles for the test. 55 std::unordered_set<native_handle_t*> mAllocatedBuffers; 56 }; 57 58 } // namespace android 59