1 /*
2  * Copyright (C) Texas Instruments Incorporated - http://www.ti.com/
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 <condition_variable>
20 #include <mutex>
21 
22 #include <cstdbool>
23 #include <cstdint>
24 
25 #include <hardware/hwcomposer.h>
26 #include <kms++/kms++.h>
27 
28 #include <xf86drm.h>
29 #include <xf86drmMode.h>
30 
31 #include "drmfb.h"
32 
33 #define MAX_DISPLAYS 4
34 #define DSS_AVAILABLE_PIPES 4
35 
36 typedef struct display_config {
37     unsigned int xres;
38     unsigned int yres;
39     unsigned int fps;
40     unsigned int xdpi;
41     unsigned int ydpi;
42 } display_config_t;
43 
44 enum disp_role {
45     DISP_ROLE_PRIMARY = 0,
46     DISP_ROLE_SECONDARY,
47 };
48 
49 typedef struct drm_plane_props {
50     hwc_layer_1_t* layer;
51 
52     kms::Plane* plane;
53     uint64_t crtc_id;
54 
55     uint64_t crtc_x;
56     uint64_t crtc_y;
57     uint64_t crtc_w;
58     uint64_t crtc_h;
59     uint64_t src_x;
60     uint64_t src_y;
61     uint64_t src_w;
62     uint64_t src_h;
63 
64     DRMFramebuffer* fb_info;
65 } drm_plane_props_t;
66 
67 class KMSDisplay {
68 public:
KMSDisplay()69     KMSDisplay() :
70         card(NULL),
71         con(NULL),
72         crtc(NULL),
73         mode() {}
74 
75     kms::Card* card;
76     kms::Connector* con;
77     kms::Crtc* crtc;
78     kms::Videomode mode;
79 };
80 
81 class HWCDisplay
82 {
83 public:
84     HWCDisplay(enum disp_role role);
~HWCDisplay()85     ~HWCDisplay(){};
86 
87     void setup_composition_pipes();
88 
89     int update_display(drm_plane_props_t* planeProp);
90 
91     std::vector<display_config_t> configs;
92     uint32_t active_config;
93 
94     enum disp_role role;
95 
96     drm_plane_props_t planeProps[DSS_AVAILABLE_PIPES];
97 
98     KMSDisplay disp_link;
99 
100     const hwc_procs_t* cb_procs;
101 
102     bool is_dummy;
103 
104     int set_vsync_state(bool state);
105     void blank(int blank);
106     int get_display_configs(uint32_t* configs, size_t* numConfigs);
107     int get_display_attributes(uint32_t cfg, const uint32_t* attributes, int32_t* values);
108 
109     static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void* data);
110     static void vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void* data);
111 
112 private:
113     std::vector<DRMFramebuffer*> pending_fb_infos;
114     std::vector<DRMFramebuffer*> current_fb_infos;
115 
116     bool vsync_on;
117     bool blanked;
118 
119     std::mutex mutex;
120     std::condition_variable cond_flip;
121     volatile bool is_flip_pending;
122 };
123