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