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 | |
| 19 | #include <stdint.h> |
| 20 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
| 23 | #include <android-base/macros.h> |
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 | explicit GRSurfaceDrm(int drm_fd) : drm_fd_(drm_fd) {} |
| 32 | ~GRSurfaceDrm() override; |
| 33 | |
| 34 | // Creates a GRSurfaceDrm instance. |
| 35 | static std::unique_ptr<GRSurfaceDrm> Create(int drm_fd, int width, int height); |
| 36 | |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 37 | uint8_t* data() override { |
| 38 | return mmapped_buffer_; |
| 39 | } |
| 40 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 41 | private: |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 42 | friend class MinuiBackendDrm; |
| 43 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 44 | const int drm_fd_; |
| 45 | |
| 46 | uint32_t fb_id{ 0 }; |
| 47 | uint32_t handle{ 0 }; |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 48 | uint8_t* mmapped_buffer_{ nullptr }; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(GRSurfaceDrm); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class MinuiBackendDrm : public MinuiBackend { |
| 54 | public: |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 55 | MinuiBackendDrm() = default; |
| 56 | ~MinuiBackendDrm() override; |
| 57 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 58 | GRSurface* Init() override; |
| 59 | GRSurface* Flip() override; |
| 60 | void Blank(bool) override; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc); |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 64 | bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 65 | void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc); |
| 66 | drmModeConnector* FindMainMonitor(int fd, drmModeRes* resources, uint32_t* mode_index); |
| 67 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 68 | std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2]; |
| 69 | int current_buffer{ 0 }; |
| 70 | drmModeCrtc* main_monitor_crtc{ nullptr }; |
| 71 | drmModeConnector* main_monitor_connector{ nullptr }; |
| 72 | int drm_fd{ -1 }; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 73 | }; |