1 /* 2 * Copyright (C) 2022 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 #ifndef ANDROID_HWC_GRALLOC_H 18 #define ANDROID_HWC_GRALLOC_H 19 20 #include <aidl/android/hardware/graphics/common/PlaneLayout.h> 21 #include <hardware/gralloc.h> 22 #include <system/graphics.h> 23 #include <utils/StrongPointer.h> 24 25 #include <memory> 26 #include <optional> 27 #include <vector> 28 29 namespace aidl::android::hardware::graphics::composer3::impl { 30 31 class Gralloc; 32 class GrallocBuffer; 33 34 // An RAII object that will Unlock() a GrallocBuffer upon destruction. 35 class GrallocBufferView { 36 public: 37 virtual ~GrallocBufferView(); 38 39 GrallocBufferView(const GrallocBufferView& rhs) = delete; 40 GrallocBufferView& operator=(const GrallocBufferView& rhs) = delete; 41 42 GrallocBufferView(GrallocBufferView&& rhs); 43 GrallocBufferView& operator=(GrallocBufferView&& rhs); 44 45 const std::optional<void*> Get() const; 46 47 const std::optional<android_ycbcr>& GetYCbCr() const; 48 49 private: 50 friend class GrallocBuffer; 51 GrallocBufferView(GrallocBuffer* buffer, void* raw); 52 GrallocBufferView(GrallocBuffer* buffer, android_ycbcr raw); 53 54 // The GrallocBuffer that should be unlocked upon destruction of this object. 55 GrallocBuffer* gralloc_buffer_ = nullptr; 56 57 std::optional<void*> locked_; 58 std::optional<android_ycbcr> locked_ycbcr_; 59 }; 60 61 // A gralloc 4.0 buffer that has been imported in the current process and 62 // that will be released upon destruction. Users must ensure that the Gralloc 63 // instance that this buffer is created with out lives this buffer. 64 class GrallocBuffer { 65 public: 66 GrallocBuffer(Gralloc* gralloc, buffer_handle_t buffer); 67 virtual ~GrallocBuffer(); 68 69 GrallocBuffer(const GrallocBuffer& rhs) = delete; 70 GrallocBuffer& operator=(const GrallocBuffer& rhs) = delete; 71 72 GrallocBuffer(GrallocBuffer&& rhs); 73 GrallocBuffer& operator=(GrallocBuffer&& rhs); 74 75 // Locks the buffer for reading and returns a view if successful. 76 std::optional<GrallocBufferView> Lock(); 77 78 std::optional<uint32_t> GetWidth(); 79 std::optional<uint32_t> GetHeight(); 80 std::optional<uint32_t> GetDrmFormat(); 81 82 // Returns the stride of the buffer if it is a single plane buffer or fails 83 // and returns nullopt if the buffer is for a multi plane buffer. 84 std::optional<uint32_t> GetMonoPlanarStrideBytes(); 85 86 std::optional<std::vector<aidl::android::hardware::graphics::common::PlaneLayout>> 87 GetPlaneLayouts(); 88 89 private: 90 // Internal visibility for Unlock(). 91 friend class GrallocBufferView; 92 93 // Unlocks the buffer from reading. 94 void Unlock(); 95 96 void Release(); 97 98 Gralloc* gralloc_ = nullptr; 99 buffer_handle_t buffer_ = nullptr; 100 }; 101 102 class Gralloc { 103 public: 104 virtual ~Gralloc() = default; 105 106 // Imports the given buffer handle into the current process and returns an 107 // imported buffer which can be used for reading. Users must ensure that the 108 // Gralloc instance outlives any GrallocBuffers. 109 std::optional<GrallocBuffer> Import(buffer_handle_t buffer); 110 111 private: 112 // The below functions are made available only to GrallocBuffer so that 113 // users only call gralloc functions on *imported* buffers. 114 friend class GrallocBuffer; 115 116 // See GrallocBuffer::Release. 117 void Release(buffer_handle_t buffer); 118 119 // See GrallocBuffer::Lock. 120 std::optional<void*> Lock(buffer_handle_t buffer); 121 122 // See GrallocBuffer::LockYCbCr. 123 std::optional<android_ycbcr> LockYCbCr(buffer_handle_t buffer); 124 125 // See GrallocBuffer::Unlock. 126 void Unlock(buffer_handle_t buffer); 127 128 // See GrallocBuffer::GetWidth. 129 std::optional<uint32_t> GetWidth(buffer_handle_t buffer); 130 131 // See GrallocBuffer::GetHeight. 132 std::optional<uint32_t> GetHeight(buffer_handle_t buffer); 133 134 // See GrallocBuffer::GetDrmFormat. 135 std::optional<uint32_t> GetDrmFormat(buffer_handle_t buffer); 136 137 // See GrallocBuffer::GetPlaneLayouts. 138 std::optional<std::vector<aidl::android::hardware::graphics::common::PlaneLayout>> 139 GetPlaneLayouts(buffer_handle_t buffer); 140 141 // Returns the stride of the buffer if it is a single plane buffer or fails 142 // and returns nullopt if the buffer is for a multi plane buffer. 143 std::optional<uint32_t> GetMonoPlanarStrideBytes(buffer_handle_t); 144 }; 145 146 } // namespace aidl::android::hardware::graphics::composer3::impl 147 148 #endif 149