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