Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 17 | #pragma once |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 18 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 19 | #include <stddef.h> |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 22 | #include <memory> |
| 23 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 24 | #include <xf86drmMode.h> |
| 25 | |
| 26 | #include "graphics.h" |
| 27 | #include "minui/minui.h" |
| 28 | |
| 29 | class GRSurfaceDrm : public GRSurface { |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 30 | public: |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 31 | ~GRSurfaceDrm() override; |
| 32 | |
| 33 | // Creates a GRSurfaceDrm instance. |
| 34 | static std::unique_ptr<GRSurfaceDrm> Create(int drm_fd, int width, int height); |
| 35 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 36 | uint8_t* data() override { |
| 37 | return mmapped_buffer_; |
| 38 | } |
| 39 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 40 | private: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 41 | friend class MinuiBackendDrm; |
| 42 | |
Tao Bao | dd78982 | 2018-11-26 16:28:07 -0800 | [diff] [blame] | 43 | GRSurfaceDrm(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes, int drm_fd, |
| 44 | uint32_t handle) |
Tao Bao | 710bc53 | 2018-10-24 07:59:49 -0700 | [diff] [blame] | 45 | : GRSurface(width, height, row_bytes, pixel_bytes), drm_fd_(drm_fd), handle(handle) {} |
| 46 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 47 | const int drm_fd_; |
| 48 | |
| 49 | uint32_t fb_id{ 0 }; |
| 50 | uint32_t handle{ 0 }; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 51 | uint8_t* mmapped_buffer_{ nullptr }; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | class MinuiBackendDrm : public MinuiBackend { |
| 55 | public: |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 56 | MinuiBackendDrm() = default; |
| 57 | ~MinuiBackendDrm() override; |
| 58 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 59 | GRSurface* Init() override; |
| 60 | GRSurface* Flip() override; |
| 61 | void Blank(bool) override; |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 62 | void Blank(bool blank, DrmConnector index) override; |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 63 | bool HasMultipleConnectors() override; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc); |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 67 | bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface, |
| 68 | uint32_t* conntcors); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 69 | void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc); |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 70 | bool FindAndSetMonitor(int fd, drmModeRes* resources); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 71 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 72 | struct DrmInterface { |
| 73 | std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2]; |
| 74 | int current_buffer{ 0 }; |
| 75 | drmModeCrtc* monitor_crtc{ nullptr }; |
| 76 | drmModeConnector* monitor_connector{ nullptr }; |
| 77 | uint32_t selected_mode{ 0 }; |
| 78 | } drm[DRM_MAX]; |
| 79 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 80 | int drm_fd{ -1 }; |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 81 | DrmConnector active_display = DRM_MAIN; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 82 | }; |