blob: 57ba39b8375faad69a243c1641dece6b55e24bde [file] [log] [blame]
Tao Bao557fa1f2017-02-07 12:51:00 -08001/*
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 Bao92bdb5a2018-10-21 12:12:37 -070017#pragma once
Tao Bao557fa1f2017-02-07 12:51:00 -080018
Tao Baodd789822018-11-26 16:28:07 -080019#include <stddef.h>
Tao Bao557fa1f2017-02-07 12:51:00 -080020#include <stdint.h>
21
Tao Baod096d7e2018-10-23 17:24:34 -070022#include <memory>
23
Tao Bao557fa1f2017-02-07 12:51:00 -080024#include <xf86drmMode.h>
25
26#include "graphics.h"
27#include "minui/minui.h"
28
29class GRSurfaceDrm : public GRSurface {
Tao Bao92bdb5a2018-10-21 12:12:37 -070030 public:
Tao Baod096d7e2018-10-23 17:24:34 -070031 ~GRSurfaceDrm() override;
32
33 // Creates a GRSurfaceDrm instance.
34 static std::unique_ptr<GRSurfaceDrm> Create(int drm_fd, int width, int height);
35
Tao Bao92bdb5a2018-10-21 12:12:37 -070036 uint8_t* data() override {
37 return mmapped_buffer_;
38 }
39
Tao Bao557fa1f2017-02-07 12:51:00 -080040 private:
Tao Bao92bdb5a2018-10-21 12:12:37 -070041 friend class MinuiBackendDrm;
42
Tao Baodd789822018-11-26 16:28:07 -080043 GRSurfaceDrm(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes, int drm_fd,
44 uint32_t handle)
Tao Bao710bc532018-10-24 07:59:49 -070045 : GRSurface(width, height, row_bytes, pixel_bytes), drm_fd_(drm_fd), handle(handle) {}
46
Tao Baod096d7e2018-10-23 17:24:34 -070047 const int drm_fd_;
48
49 uint32_t fb_id{ 0 };
50 uint32_t handle{ 0 };
Tao Bao92bdb5a2018-10-21 12:12:37 -070051 uint8_t* mmapped_buffer_{ nullptr };
Tao Bao557fa1f2017-02-07 12:51:00 -080052};
53
54class MinuiBackendDrm : public MinuiBackend {
55 public:
Tao Baod096d7e2018-10-23 17:24:34 -070056 MinuiBackendDrm() = default;
57 ~MinuiBackendDrm() override;
58
Tao Bao557fa1f2017-02-07 12:51:00 -080059 GRSurface* Init() override;
60 GRSurface* Flip() override;
61 void Blank(bool) override;
Tao Bao557fa1f2017-02-07 12:51:00 -080062
63 private:
64 void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc);
Tao Baod096d7e2018-10-23 17:24:34 -070065 bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface);
Tao Bao557fa1f2017-02-07 12:51:00 -080066 void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc);
67 drmModeConnector* FindMainMonitor(int fd, drmModeRes* resources, uint32_t* mode_index);
68
Tao Baod096d7e2018-10-23 17:24:34 -070069 std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2];
70 int current_buffer{ 0 };
71 drmModeCrtc* main_monitor_crtc{ nullptr };
72 drmModeConnector* main_monitor_connector{ nullptr };
73 int drm_fd{ -1 };
Tao Bao557fa1f2017-02-07 12:51:00 -080074};