blob: 6ba46e60bf283371f378f35fb32f5093c2ec789b [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
19#include <stdint.h>
20
Tao Baod096d7e2018-10-23 17:24:34 -070021#include <memory>
22
Tao Bao557fa1f2017-02-07 12:51:00 -080023#include <xf86drmMode.h>
24
25#include "graphics.h"
26#include "minui/minui.h"
27
28class GRSurfaceDrm : public GRSurface {
Tao Bao92bdb5a2018-10-21 12:12:37 -070029 public:
Tao Baod096d7e2018-10-23 17:24:34 -070030 ~GRSurfaceDrm() override;
31
32 // Creates a GRSurfaceDrm instance.
33 static std::unique_ptr<GRSurfaceDrm> Create(int drm_fd, int width, int height);
34
Tao Bao92bdb5a2018-10-21 12:12:37 -070035 uint8_t* data() override {
36 return mmapped_buffer_;
37 }
38
Tao Bao557fa1f2017-02-07 12:51:00 -080039 private:
Tao Bao92bdb5a2018-10-21 12:12:37 -070040 friend class MinuiBackendDrm;
41
Tao Bao710bc532018-10-24 07:59:49 -070042 GRSurfaceDrm(int width, int height, int row_bytes, int pixel_bytes, int drm_fd, uint32_t handle)
43 : GRSurface(width, height, row_bytes, pixel_bytes), drm_fd_(drm_fd), handle(handle) {}
44
Tao Baod096d7e2018-10-23 17:24:34 -070045 const int drm_fd_;
46
47 uint32_t fb_id{ 0 };
48 uint32_t handle{ 0 };
Tao Bao92bdb5a2018-10-21 12:12:37 -070049 uint8_t* mmapped_buffer_{ nullptr };
Tao Bao557fa1f2017-02-07 12:51:00 -080050};
51
52class MinuiBackendDrm : public MinuiBackend {
53 public:
Tao Baod096d7e2018-10-23 17:24:34 -070054 MinuiBackendDrm() = default;
55 ~MinuiBackendDrm() override;
56
Tao Bao557fa1f2017-02-07 12:51:00 -080057 GRSurface* Init() override;
58 GRSurface* Flip() override;
59 void Blank(bool) override;
Tao Bao557fa1f2017-02-07 12:51:00 -080060
61 private:
62 void DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc);
Tao Baod096d7e2018-10-23 17:24:34 -070063 bool DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, const std::unique_ptr<GRSurfaceDrm>& surface);
Tao Bao557fa1f2017-02-07 12:51:00 -080064 void DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc);
65 drmModeConnector* FindMainMonitor(int fd, drmModeRes* resources, uint32_t* mode_index);
66
Tao Baod096d7e2018-10-23 17:24:34 -070067 std::unique_ptr<GRSurfaceDrm> GRSurfaceDrms[2];
68 int current_buffer{ 0 };
69 drmModeCrtc* main_monitor_crtc{ nullptr };
70 drmModeConnector* main_monitor_connector{ nullptr };
71 int drm_fd{ -1 };
Tao Bao557fa1f2017-02-07 12:51:00 -080072};