1 /*
2  * Copyright (C) 2020 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 <memory>
20 #include <optional>
21 #include <vector>
22 
23 #include <android/hardware/graphics/mapper/3.0/IMapper.h>
24 #include <hardware/gralloc.h>
25 #include <system/graphics.h>
26 #include <utils/StrongPointer.h>
27 
28 namespace cuttlefish {
29 
30 class Gralloc;
31 
32 // A gralloc buffer that has been imported in the current process and
33 // that will be released upon destruction. Users must ensure that the Gralloc
34 // instance that this buffer is created with outlives this buffer.
35 class GrallocBuffer {
36  public:
37   GrallocBuffer(Gralloc* gralloc, buffer_handle_t buffer);
38   virtual ~GrallocBuffer();
39 
40   GrallocBuffer(const GrallocBuffer& rhs) = delete;
41   GrallocBuffer& operator=(const GrallocBuffer& rhs) = delete;
42 
43   GrallocBuffer(GrallocBuffer&& rhs);
44   GrallocBuffer& operator=(GrallocBuffer&& rhs);
45 
46   // Locks the buffer for reading and returns the mapped address if successful.
47   // Fails and returns nullopt if the underlying buffer is a YCbCr buffer.
48   //
49   // TODO(b/159834777): wrap lock result into a RAII object that restricts
50   // usage of the mapped buffer to the lifetime of the RAII object.
51   std::optional<void*> Lock();
52 
53   // Locks the buffer for reading and returns the mapped addresses and strides
54   // of each plane if successful. Fails and returns nullopt if the underlying
55   // buffer is not a YCbCr buffer.
56   //
57   // TODO(b/159834777): wrap lock result into a RAII object that restricts
58   // usage of the mapped buffer to the lifetime of the RAII object.
59   std::optional<android_ycbcr> LockYCbCr();
60 
61   // Unlocks the buffer from reading.
62   void Unlock();
63 
64   std::optional<uint32_t> GetWidth();
65   std::optional<uint32_t> GetHeight();
66   std::optional<uint32_t> GetDrmFormat();
67 
68   // Returns the stride of the buffer if it is a single plane buffer or fails
69   // and returns nullopt if the buffer is for a multi plane buffer.
70   std::optional<uint32_t> GetMonoPlanarStrideBytes();
71 
72  private:
73   void Release();
74 
75   Gralloc* gralloc_ = nullptr;
76   buffer_handle_t buffer_ = nullptr;
77 };
78 
79 class Gralloc {
80  public:
81   Gralloc();
82   virtual ~Gralloc() = default;
83 
84   // Imports the given buffer handle into the current process and returns an
85   // imported buffer which can be used for reading. Users must ensure that the
86   // Gralloc instance outlives any GrallocBuffers.
87   std::optional<GrallocBuffer> Import(buffer_handle_t buffer);
88 
89  private:
90   // The below functions are made avaialble only to GrallocBuffer so that
91   // users only call gralloc functions on *imported* buffers.
92   friend class GrallocBuffer;
93 
94   // See GrallocBuffer::Release.
95   void Release(buffer_handle_t buffer);
96 
97   // See GrallocBuffer::Lock.
98   std::optional<void*> Lock(buffer_handle_t buffer);
99 
100   // See GrallocBuffer::LockYCbCr.
101   std::optional<android_ycbcr> LockYCbCr(buffer_handle_t buffer);
102 
103   // See GrallocBuffer::Unlock.
104   void Unlock(buffer_handle_t buffer);
105 
106   // See GrallocBuffer::GetWidth.
107   std::optional<uint32_t> GetWidth(buffer_handle_t buffer);
108 
109   // See GrallocBuffer::GetHeight.
110   std::optional<uint32_t> GetHeight(buffer_handle_t buffer);
111 
112   // See GrallocBuffer::GetDrmFormat.
113   std::optional<uint32_t> GetDrmFormat(buffer_handle_t buffer);
114 
115   // Returns the stride of the buffer if it is a single plane buffer or fails
116   // and returns nullopt if the buffer is for a multi plane buffer.
117   std::optional<uint32_t> GetMonoPlanarStrideBytes(buffer_handle_t);
118 
119   const gralloc_module_t* gralloc0_ = nullptr;
120   android::sp<android::hardware::graphics::mapper::V3_0::IMapper> gralloc3_;
121 };
122 
123 }  // namespace cvd