1 /*
2  * Copyright 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 #pragma once
18 
19 #include <android-base/unique_fd.h>
20 #include <cutils/native_handle.h>
21 
22 #include <memory>
23 #include <tuple>
24 #include <vector>
25 
26 #include "Common.h"
27 #include "DrmAtomicRequest.h"
28 #include "DrmBuffer.h"
29 #include "DrmConnector.h"
30 #include "DrmCrtc.h"
31 #include "DrmDisplay.h"
32 #include "DrmEventListener.h"
33 #include "DrmMode.h"
34 #include "DrmPlane.h"
35 #include "DrmProperty.h"
36 #include "LruCache.h"
37 #include "aemu/base/synchronization/AndroidLock.h"
38 
39 namespace aidl::android::hardware::graphics::composer3::impl {
40 
41 class DrmClient {
42    public:
43     DrmClient() = default;
44     ~DrmClient();
45 
46     DrmClient(const DrmClient&) = delete;
47     DrmClient& operator=(const DrmClient&) = delete;
48 
49     DrmClient(DrmClient&&) = delete;
50     DrmClient& operator=(DrmClient&&) = delete;
51 
52     ::android::base::unique_fd OpenVirtioGpuDrmFd();
53 
54     HWC3::Error init();
55 
56     struct DisplayConfig {
57         uint32_t id;
58         uint32_t width;
59         uint32_t height;
60         uint32_t dpiX;
61         uint32_t dpiY;
62         uint32_t refreshRateHz;
63     };
64 
65     HWC3::Error getDisplayConfigs(std::vector<DisplayConfig>* configs) const;
66 
67     using HotplugCallback = std::function<void(bool /*connected*/,   //
68                                                uint32_t /*id*/,      //
69                                                uint32_t /*width*/,   //
70                                                uint32_t /*height*/,  //
71                                                uint32_t /*dpiX*/,    //
72                                                uint32_t /*dpiY*/,    //
73                                                uint32_t /*refreshRate*/)>;
74 
75     HWC3::Error registerOnHotplugCallback(const HotplugCallback& cb);
76     HWC3::Error unregisterOnHotplugCallback();
77 
refreshRate()78     uint32_t refreshRate() const { return mDisplays[0]->getRefreshRateUint(); }
79 
80     std::tuple<HWC3::Error, std::shared_ptr<DrmBuffer>> create(const native_handle_t* handle);
81 
82     std::tuple<HWC3::Error, ::android::base::unique_fd> flushToDisplay(
83         uint32_t displayId, const std::shared_ptr<DrmBuffer>& buffer,
84         ::android::base::borrowed_fd inWaitSyncFd);
85 
86     std::optional<std::vector<uint8_t>> getEdid(uint32_t displayId);
87 
88    private:
89     using DrmPrimeBufferHandle = uint32_t;
90 
91     // Grant visibility to destroyDrmFramebuffer to DrmBuffer.
92     friend class DrmBuffer;
93     HWC3::Error destroyDrmFramebuffer(DrmBuffer* buffer);
94 
95     // Grant visibility for handleHotplug to DrmEventListener.
96     bool handleHotplug();
97 
98     bool loadDrmDisplays();
99 
100     // Drm device.
101     ::android::base::unique_fd mFd;
102 
103     mutable ::gfxstream::guest::ReadWriteLock mDisplaysMutex;
104     std::vector<std::unique_ptr<DrmDisplay>> mDisplays;
105 
106     std::optional<HotplugCallback> mHotplugCallback;
107 
108     std::unique_ptr<DrmEventListener> mDrmEventListener;
109 };
110 
111 }  // namespace aidl::android::hardware::graphics::composer3::impl
112