summaryrefslogtreecommitdiff
path: root/src/drmmode_display.h
blob: 67f5f1f0538327d8f791a1cadf6b31ed323a40d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright © 2007 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *     Dave Airlie <airlied@redhat.com>
 *
 */
#ifndef DRMMODE_DISPLAY_H
#define DRMMODE_DISPLAY_H

#include "xf86drmMode.h"
#ifdef HAVE_LIBUDEV
#include "libudev.h"
#endif

#include "amdgpu_drm_queue.h"
#include "amdgpu_probe.h"
#include "amdgpu.h"

/*
 * Enum of non-legacy color management properties, according to DRM. Note that
 * the values should be incremental (with the exception of the INVALID member),
 * as defined by C99. The ordering also matters. Some logics (such as iterators
 * and bitmasks) depend on these facts.
 */
enum drmmode_cm_prop {
	CM_DEGAMMA_LUT,
	CM_CTM,
	CM_GAMMA_LUT,
	CM_DEGAMMA_LUT_SIZE,
	CM_GAMMA_LUT_SIZE,
	CM_NUM_PROPS,
	CM_INVALID_PROP = -1,
};

typedef struct {
	ScrnInfoPtr scrn;
#ifdef HAVE_LIBUDEV
	struct udev_monitor *uevent_monitor;
	InputHandlerProc uevent_handler;
#endif
	drmEventContext event_context;
	int count_crtcs;

	Bool delete_dp_12_displays;

	Bool dri2_flipping;
	Bool present_flipping;
	uint32_t vrr_prop_id;

	/* Cache for DRM property type IDs for CRTC color management */
	uint32_t cm_prop_ids[CM_NUM_PROPS];
	/* Lookup table sizes */
	uint32_t degamma_lut_size;
	uint32_t gamma_lut_size;
} drmmode_rec, *drmmode_ptr;

typedef struct {
	void *event_data;
	int flip_count;
	unsigned int fe_frame;
	uint64_t fe_usec;
	xf86CrtcPtr fe_crtc;
	amdgpu_drm_handler_proc handler;
	amdgpu_drm_abort_proc abort;
	struct drmmode_fb *fb[0];
} drmmode_flipdata_rec, *drmmode_flipdata_ptr;

struct drmmode_fb {
	int refcnt;
	uint32_t handle;
};

enum drmmode_scanout_status {
	DRMMODE_SCANOUT_OK,
	DRMMODE_SCANOUT_FLIP_FAILED = 1u << 0,
	DRMMODE_SCANOUT_VBLANK_FAILED = 1u << 1,
};

struct drmmode_scanout {
	PixmapPtr pixmap;
};

typedef struct {
	drmmode_ptr drmmode;
	drmModeCrtcPtr mode_crtc;
	int hw_id;

	CursorPtr cursor;
	int cursor_x;
	int cursor_y;
	int cursor_xhot;
	int cursor_yhot;
	unsigned cursor_id;
	struct amdgpu_buffer *cursor_buffer[2];

	struct drmmode_scanout rotate;
	struct drmmode_scanout scanout[2];
	DamagePtr scanout_damage;
	Bool ignore_damage;
	RegionRec scanout_last_region;
	unsigned scanout_id;
	uintptr_t scanout_update_pending;
	Bool tear_free;
	enum drmmode_scanout_status scanout_status;
	Bool vrr_enabled;

	PixmapPtr prime_scanout_pixmap;

	int dpms_mode;
	CARD64 dpms_last_ust;
	uint32_t dpms_last_seq;
	int dpms_last_fps;
	uint32_t interpolated_vblanks;

	/* Modeset needed for DPMS on */
	Bool need_modeset;
	/* For keeping track of nested calls to drm_wait_pending_flip /
	 * drm_queue_handle_deferred
	 */
	int wait_flip_nesting_level;
	/* A flip to this FB is pending for this CRTC */
	struct drmmode_fb *flip_pending;
	/* The FB currently being scanned out by this CRTC, if any */
	struct drmmode_fb *fb;

	struct drm_color_lut *degamma_lut;
	struct drm_color_ctm *ctm;
	struct drm_color_lut *gamma_lut;
} drmmode_crtc_private_rec, *drmmode_crtc_private_ptr;

typedef struct {
	drmModePropertyPtr mode_prop;
	uint64_t value;
	int num_atoms;		/* if range prop, num_atoms == 1; if enum prop, num_atoms == num_enums + 1 */
	Atom *atoms;
} drmmode_prop_rec, *drmmode_prop_ptr;

typedef struct {
	drmmode_ptr drmmode;
	int output_id;
	drmModeConnectorPtr mode_output;
	drmModeEncoderPtr *mode_encoders;
	drmModePropertyBlobPtr edid_blob;
#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1, 17, 99, 901, 0)
	drmModePropertyBlobPtr tile_blob;
#endif
	int dpms_enum_id;
	int num_props;
	drmmode_prop_ptr props;
	int enc_mask;
	int enc_clone_mask;
	int tear_free;
} drmmode_output_private_rec, *drmmode_output_private_ptr;

typedef struct {
	uint32_t lessee_id;
} drmmode_lease_private_rec, *drmmode_lease_private_ptr;


enum drmmode_flip_sync {
    FLIP_VSYNC,
    FLIP_ASYNC,
};


/**
 * Return TRUE if kernel supports non-legacy color management.
 */
static inline Bool
drmmode_cm_enabled(drmmode_ptr drmmode)
{
	return drmmode->cm_prop_ids[CM_GAMMA_LUT_SIZE] != 0;
}

/* Can the page flip ioctl be used for this CRTC? */
static inline Bool
drmmode_crtc_can_flip(xf86CrtcPtr crtc)
{
	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;

	return crtc->enabled &&
		drmmode_crtc->dpms_mode == DPMSModeOn &&
		!drmmode_crtc->rotate.pixmap &&
		(drmmode_crtc->tear_free ||
		 !drmmode_crtc->scanout[drmmode_crtc->scanout_id].pixmap);
}


static inline void
drmmode_fb_reference_loc(int drm_fd, struct drmmode_fb **old, struct drmmode_fb *new,
			 const char *caller, unsigned line)
{
	if (new) {
		if (new->refcnt <= 0) {
			FatalError("New FB's refcnt was %d at %s:%u",
				   new->refcnt, caller, line);
		}

		new->refcnt++;
	}

	if (*old) {
		if ((*old)->refcnt <= 0) {
			FatalError("Old FB's refcnt was %d at %s:%u",
				   (*old)->refcnt, caller, line);
		}

		if (--(*old)->refcnt == 0) {
			drmModeRmFB(drm_fd, (*old)->handle);
			free(*old);
		}
	}

	*old = new;
}

#define drmmode_fb_reference(fd, old, new) \
	drmmode_fb_reference_loc(fd, old, new, __func__, __LINE__)


extern int drmmode_page_flip_target_absolute(AMDGPUEntPtr pAMDGPUEnt,
					     drmmode_crtc_private_ptr drmmode_crtc,
					     int fb_id, uint32_t flags,
					     uintptr_t drm_queue_seq,
					     uint32_t target_msc);
extern int drmmode_page_flip_target_relative(AMDGPUEntPtr pAMDGPUEnt,
					     drmmode_crtc_private_ptr drmmode_crtc,
					     int fb_id, uint32_t flags,
					     uintptr_t drm_queue_seq,
					     uint32_t target_msc);
extern Bool drmmode_pre_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, int cpp);
extern void drmmode_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode);
extern void drmmode_fini(ScrnInfoPtr pScrn, drmmode_ptr drmmode);
void drmmode_adjust_frame(ScrnInfoPtr pScrn, drmmode_ptr drmmode, int x, int y);
extern Bool drmmode_set_desired_modes(ScrnInfoPtr pScrn, drmmode_ptr drmmode,
				      Bool set_hw);
extern void drmmode_copy_fb(ScrnInfoPtr pScrn, drmmode_ptr drmmode);
extern Bool drmmode_setup_colormap(ScreenPtr pScreen, ScrnInfoPtr pScrn);

extern void drmmode_crtc_scanout_destroy(drmmode_ptr drmmode,
					 struct drmmode_scanout *scanout);
void drmmode_crtc_scanout_free(xf86CrtcPtr crtc);
PixmapPtr drmmode_crtc_scanout_create(xf86CrtcPtr crtc,
				      struct drmmode_scanout *scanout,
				      int width, int height);

extern void drmmode_uevent_init(ScrnInfoPtr scrn, drmmode_ptr drmmode);
extern void drmmode_uevent_fini(ScrnInfoPtr scrn, drmmode_ptr drmmode);

Bool drmmode_set_mode(xf86CrtcPtr crtc, struct drmmode_fb *fb,
		      DisplayModePtr mode, int x, int y);

extern int drmmode_get_crtc_id(xf86CrtcPtr crtc);
extern int drmmode_get_pitch_align(ScrnInfoPtr scrn, int bpe);
Bool amdgpu_do_pageflip(ScrnInfoPtr scrn, ClientPtr client,
			PixmapPtr new_front, uint64_t id, void *data,
			xf86CrtcPtr ref_crtc, amdgpu_drm_handler_proc handler,
			amdgpu_drm_abort_proc abort,
			enum drmmode_flip_sync flip_sync,
			uint32_t target_msc);
int drmmode_crtc_get_ust_msc(xf86CrtcPtr crtc, CARD64 *ust, CARD64 *msc);
int drmmode_get_current_ust(int drm_fd, CARD64 * ust);
void drmmode_crtc_set_vrr(xf86CrtcPtr crtc, Bool enabled);

Bool drmmode_wait_vblank(xf86CrtcPtr crtc, drmVBlankSeqType type,
			 uint32_t target_seq, unsigned long signal,
			 uint64_t *ust, uint32_t *result_seq);


extern miPointerSpriteFuncRec drmmode_sprite_funcs;


#endif