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/logging.h>
20 #include <android-base/unique_fd.h>
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23 
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 #include <unordered_map>
28 
29 #include "Common.h"
30 #include "DrmBuffer.h"
31 #include "DrmConnector.h"
32 #include "DrmCrtc.h"
33 #include "DrmPlane.h"
34 
35 namespace aidl::android::hardware::graphics::composer3::impl {
36 
37 enum class DrmHotplugChange {
38     kNoChange,
39     kConnected,
40     kDisconnected,
41 };
42 
43 class DrmDisplay {
44    public:
45     static std::unique_ptr<DrmDisplay> create(uint32_t id, std::unique_ptr<DrmConnector> connector,
46                                               std::unique_ptr<DrmCrtc> crtc,
47                                               std::unique_ptr<DrmPlane> plane,
48                                               ::android::base::borrowed_fd drmFd);
49 
getId()50     uint32_t getId() const { return mId; }
51 
getWidth()52     uint32_t getWidth() const { return mConnector->getWidth(); }
getHeight()53     uint32_t getHeight() const { return mConnector->getHeight(); }
54 
getDpiX()55     uint32_t getDpiX() const { return mConnector->getDpiX(); }
getDpiY()56     uint32_t getDpiY() const { return mConnector->getDpiY(); }
57 
getRefreshRateUint()58     uint32_t getRefreshRateUint() const { return mConnector->getRefreshRateUint(); }
59 
isConnected()60     bool isConnected() const { return mConnector->isConnected(); }
61 
getEdid()62     std::optional<std::vector<uint8_t>> getEdid() const { return mConnector->getEdid(); }
63 
64     std::tuple<HWC3::Error, ::android::base::unique_fd> flush(
65         ::android::base::borrowed_fd drmFd, ::android::base::borrowed_fd inWaitSyncFd,
66         const std::shared_ptr<DrmBuffer>& buffer);
67 
68     DrmHotplugChange checkAndHandleHotplug(::android::base::borrowed_fd drmFd);
69 
70    private:
DrmDisplay(uint32_t id,std::unique_ptr<DrmConnector> connector,std::unique_ptr<DrmCrtc> crtc,std::unique_ptr<DrmPlane> plane)71     DrmDisplay(uint32_t id, std::unique_ptr<DrmConnector> connector, std::unique_ptr<DrmCrtc> crtc,
72                std::unique_ptr<DrmPlane> plane)
73         : mId(id),
74           mConnector(std::move(connector)),
75           mCrtc(std::move(crtc)),
76           mPlane(std::move(plane)) {}
77 
78     bool onConnect(::android::base::borrowed_fd drmFd);
79 
80     bool onDisconnect(::android::base::borrowed_fd drmFd);
81 
82     const uint32_t mId;
83     std::unique_ptr<DrmConnector> mConnector;
84     std::unique_ptr<DrmCrtc> mCrtc;
85     std::unique_ptr<DrmPlane> mPlane;
86 
87     // The last presented buffer / DRM framebuffer is cached until
88     // the next present to avoid toggling the display on and off.
89     std::shared_ptr<DrmBuffer> mPreviousBuffer;
90 };
91 
92 }  // namespace aidl::android::hardware::graphics::composer3::impl
93