hc
2023-11-06 e3e12f52b214121840b44c91de5b3e5af5d3eb84
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
From 667f51ecc8273c1ef72260a1cf19dc97d113e624 Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Wed, 24 Jun 2020 11:59:42 +0800
Subject: [PATCH 14/74] backend-drm: Support virtual screen size
 
Support setting virtual screen size, for example:
export WESTON_DRM_VIRTUAL_SIZE=1024x768
 
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 libweston/backend-drm/drm-internal.h |  23 ++++++
 libweston/backend-drm/drm.c          |  28 ++++++-
 libweston/backend-drm/kms.c          | 115 ++++++++++++++++++++++-----
 libweston/backend-drm/modes.c        |  22 ++++-
 4 files changed, 162 insertions(+), 26 deletions(-)
 
diff --git a/libweston/backend-drm/drm-internal.h b/libweston/backend-drm/drm-internal.h
index 3f42a25..996f587 100644
--- a/libweston/backend-drm/drm-internal.h
+++ b/libweston/backend-drm/drm-internal.h
@@ -166,6 +166,7 @@ enum wdrm_plane_property {
     WDRM_PLANE_IN_FENCE_FD,
     WDRM_PLANE_FB_DAMAGE_CLIPS,
     WDRM_PLANE_ZPOS,
+    WDRM_PLANE_FEATURE,
     WDRM_PLANE__COUNT
 };
 
@@ -179,6 +180,15 @@ enum wdrm_plane_type {
     WDRM_PLANE_TYPE__COUNT
 };
 
+/**
+ * Possible values for the WDRM_PLANE_FEATURE property.
+ */
+enum wdrm_plane_feature {
+    WDRM_PLANE_FEATURE_SCALE = 0,
+    WDRM_PLANE_FEATURE_ALPHA,
+    WDRM_PLANE_FEATURE__COUNT
+};
+
 /**
  * List of properties attached to a DRM connector
  */
@@ -325,6 +335,7 @@ struct drm_backend {
     bool pending_update;
     int64_t last_update_ms;
     int64_t last_resize_ms;
+    int64_t resize_freeze_ms;
 
     bool single_head;
     bool head_fallback;
@@ -332,6 +343,9 @@ struct drm_backend {
     drm_head_match_t *head_matches;
     struct drm_head *primary_head;
     struct wl_listener output_create_listener;
+
+    int virtual_width;
+    int virtual_height;
 };
 
 struct drm_mode {
@@ -501,6 +515,8 @@ struct drm_plane {
     struct wl_list link;
 
     struct weston_drm_format_array formats;
+
+    bool can_scale;
 };
 
 struct drm_connector {
@@ -597,6 +613,9 @@ struct drm_output {
     submit_frame_cb virtual_submit_frame;
 
     bool state_invalid;
+
+    /* The dummy framebuffer for SET_CRTC. */
+    struct drm_fb *fb_dummy;
 };
 
 static inline struct drm_head *
@@ -694,6 +713,10 @@ uint64_t
 drm_property_get_value(struct drm_property_info *info,
                const drmModeObjectProperties *props,
                uint64_t def);
+bool
+drm_property_has_feature(struct drm_property_info *infos,
+             const drmModeObjectProperties *props,
+             enum wdrm_plane_feature feature);
 uint64_t *
 drm_property_get_range_values(struct drm_property_info *info,
                   const drmModeObjectProperties *props);
diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
index 4e00933..1c839c5 100644
--- a/libweston/backend-drm/drm.c
+++ b/libweston/backend-drm/drm.c
@@ -324,6 +324,11 @@ drm_output_update_complete(struct drm_output *output, uint32_t flags,
         output->state_last = NULL;
     }
 
+    if (output->fb_dummy) {
+        drm_fb_unref(output->fb_dummy);
+        output->fb_dummy = NULL;
+    }
+
     if (output->destroy_pending) {
         output->destroy_pending = false;
         output->disable_pending = false;
@@ -397,6 +402,7 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
     struct drm_property_info *damage_info =
         &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
     struct drm_backend *b = to_drm_backend(c);
+    struct drm_mode *mode;
     struct drm_fb *fb;
     pixman_region32_t scanout_damage;
     pixman_box32_t *rects;
@@ -440,10 +446,11 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
     scanout_state->src_w = fb->width << 16;
     scanout_state->src_h = fb->height << 16;
 
+    mode = to_drm_mode(output->base.current_mode);
     scanout_state->dest_x = 0;
     scanout_state->dest_y = 0;
-    scanout_state->dest_w = output->base.current_mode->width;
-    scanout_state->dest_h = output->base.current_mode->height;
+    scanout_state->dest_w = mode->mode_info.hdisplay;
+    scanout_state->dest_h = mode->mode_info.vdisplay;
 
     pixman_region32_subtract(&c->primary_plane.damage,
                  &c->primary_plane.damage, damage);
@@ -516,7 +523,7 @@ drm_output_repaint(struct weston_output *output_base,
 
     weston_compositor_read_presentation_clock(b->compositor, &now);
     now_ms = timespec_to_msec(&now);
-    if (now_ms < b->last_resize_ms + DRM_RESIZE_FREEZE_MS) {
+    if (now_ms < b->last_resize_ms + b->resize_freeze_ms) {
         /* Resize fullscreen/maxmium views(not always success) */
         if (now_ms < b->last_resize_ms + DRM_RESIZE_FREEZE_MS)
             wl_signal_emit(&b->compositor->output_resized_signal,
@@ -852,6 +859,11 @@ drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
                        props,
                        WDRM_PLANE_TYPE__COUNT);
 
+    plane->can_scale =
+        drm_property_has_feature(plane->props,
+                     props,
+                     WDRM_PLANE_FEATURE_SCALE);
+
     zpos_range_values =
         drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS],
                           props);
@@ -3298,6 +3310,16 @@ drm_backend_create(struct weston_compositor *compositor,
 
     b->head_matches = drm_head_matches[head_mode];
 
+    buf = getenv("WESTON_DRM_VIRTUAL_SIZE");
+    if (buf)
+        sscanf(buf, "%dx%d", &b->virtual_width, &b->virtual_height);
+
+    buf = getenv("WESTON_DRM_RESIZE_FREEZE_MS");
+    if (buf)
+        b->resize_freeze_ms = atoi(buf);
+    else
+        b->resize_freeze_ms = DRM_RESIZE_FREEZE_MS;
+
     b->state_invalid = true;
     b->drm.fd = -1;
 
diff --git a/libweston/backend-drm/kms.c b/libweston/backend-drm/kms.c
index 4426af4..1fcbdeb 100644
--- a/libweston/backend-drm/kms.c
+++ b/libweston/backend-drm/kms.c
@@ -56,6 +56,15 @@ struct drm_property_enum_info plane_type_enums[] = {
     },
 };
 
+struct drm_property_enum_info plane_feature_enums[] = {
+    [WDRM_PLANE_FEATURE_SCALE] = {
+        .name = "scale",
+    },
+    [WDRM_PLANE_FEATURE_ALPHA] = {
+        .name = "alpha",
+    },
+};
+
 const struct drm_property_info plane_props[] = {
     [WDRM_PLANE_TYPE] = {
         .name = "type",
@@ -76,6 +85,11 @@ const struct drm_property_info plane_props[] = {
     [WDRM_PLANE_IN_FENCE_FD] = { .name = "IN_FENCE_FD" },
     [WDRM_PLANE_FB_DAMAGE_CLIPS] = { .name = "FB_DAMAGE_CLIPS" },
     [WDRM_PLANE_ZPOS] = { .name = "zpos" },
+    [WDRM_PLANE_FEATURE] = {
+        .name = "FEATURE",
+        .enum_values = plane_feature_enums,
+        .num_enum_values = WDRM_PLANE_FEATURE__COUNT,
+    },
 };
 
 struct drm_property_enum_info dpms_state_enums[] = {
@@ -213,6 +227,31 @@ drm_property_get_value(struct drm_property_info *info,
     return def;
 }
 
+bool
+drm_property_has_feature(struct drm_property_info *infos,
+             const drmModeObjectProperties *props,
+             enum wdrm_plane_feature feature)
+{
+    struct drm_property_info *info = &infos[WDRM_PLANE_FEATURE];
+    unsigned int i;
+
+    if (info->prop_id == 0 ||
+        feature >= info->num_enum_values ||
+        !info->enum_values[feature].valid)
+        return false;
+
+    for (i = 0; i < props->count_props; i++) {
+        if (props->props[i] != info->prop_id)
+            continue;
+
+        if (props->prop_values[i] &
+            (1LL << info->enum_values[feature].value))
+            return true;
+    }
+
+    return false;
+}
+
 /**
  * Get the current range values of a KMS property
  *
@@ -333,9 +372,11 @@ drm_property_info_populate(struct drm_backend *b,
         }
 
         if (info[j].num_enum_values == 0 &&
-            (prop->flags & DRM_MODE_PROP_ENUM)) {
+            (prop->flags & DRM_MODE_PROP_ENUM ||
+             prop->flags & DRM_MODE_PROP_BITMASK)) {
             weston_log("DRM: expected property %s to not be an"
-                       " enum, but it is; ignoring\n", prop->name);
+                       " enum or bitmask, but it is; ignoring\n",
+                   prop->name);
             drmModeFreeProperty(prop);
             continue;
         }
@@ -356,9 +397,11 @@ drm_property_info_populate(struct drm_backend *b,
             continue;
         }
 
-        if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
-            weston_log("DRM: expected property %s to be an enum,"
-                   " but it is not; ignoring\n", prop->name);
+        if (!(prop->flags & DRM_MODE_PROP_ENUM ||
+              prop->flags & DRM_MODE_PROP_BITMASK)) {
+            weston_log("DRM: expected property %s to be an enum or "
+                   "bitmask, but it is not; ignoring\n",
+                   prop->name);
             drmModeFreeProperty(prop);
             info[j].prop_id = 0;
             continue;
@@ -660,6 +703,7 @@ drm_output_apply_state_legacy(struct drm_output_state *state)
     int n_conn = 0;
     struct timespec now;
     int ret = 0;
+    bool scaling;
 
     wl_list_for_each(head, &output->base.head_list, base.output_link) {
         assert(n_conn < MAX_CLONED_CONNECTORS);
@@ -707,30 +751,36 @@ drm_output_apply_state_legacy(struct drm_output_state *state)
     if (!scanout_state || !scanout_state->fb)
         return 0;
 
-    /* The legacy SetCrtc API doesn't allow us to do scaling, and the
-     * legacy PageFlip API doesn't allow us to do clipping either. */
-    assert(scanout_state->src_x == 0);
-    assert(scanout_state->src_y == 0);
-    assert(scanout_state->src_w ==
-        (unsigned) (output->base.current_mode->width << 16));
-    assert(scanout_state->src_h ==
-        (unsigned) (output->base.current_mode->height << 16));
-    assert(scanout_state->dest_x == 0);
-    assert(scanout_state->dest_y == 0);
-    assert(scanout_state->dest_w == scanout_state->src_w >> 16);
-    assert(scanout_state->dest_h == scanout_state->src_h >> 16);
     /* The legacy SetCrtc API doesn't support fences */
     assert(scanout_state->in_fence_fd == -1);
 
     mode = to_drm_mode(output->base.current_mode);
+
+    scaling = scanout_state->src_w >> 16 != scanout_state->dest_w ||
+        scanout_state->src_h >> 16 != scanout_state->dest_h;
+
     if (output->state_invalid ||
-        !scanout_plane->state_cur->fb ||
-        scanout_plane->state_cur->fb->strides[0] !=
-        scanout_state->fb->strides[0]) {
+        !scanout_plane->state_cur->fb) {
+        int fb_id = scanout_state->fb->fb_id;
+
+        /* Use a dummy fb for initial mode setting */
+        if (!output->fb_dummy) {
+            output->fb_dummy =
+                drm_fb_create_dumb(backend,
+                           mode->mode_info.hdisplay,
+                           mode->mode_info.vdisplay,
+                           output->gbm_format);
+            if (!output->fb_dummy) {
+                weston_log("failed to create fb_dummy\n");
+                goto err;
+            }
+        }
+
+        if (n_conn == 1 || scaling)
+            fb_id = output->fb_dummy->fb_id;
 
         ret = drmModeSetCrtc(backend->drm.fd, crtc->crtc_id,
-                     scanout_state->fb->fb_id,
-                     0, 0,
+                     fb_id, 0, 0,
                      connectors, n_conn,
                      &mode->mode_info);
         if (ret) {
@@ -741,6 +791,27 @@ drm_output_apply_state_legacy(struct drm_output_state *state)
         output->state_invalid = false;
     }
 
+    if (scaling && !output->scanout_plane->can_scale) {
+        weston_log("Couldn't do scaling on output %s\n",
+               output->base.name);
+        weston_output_finish_frame(&output->base, NULL,
+                       WP_PRESENTATION_FEEDBACK_INVALID);
+        return 0;
+    }
+
+    ret = drmModeSetPlane(backend->drm.fd,
+                  scanout_state->plane->plane_id,
+                  crtc->crtc_id,
+                  scanout_state->fb->fb_id, 0,
+                  scanout_state->dest_x, scanout_state->dest_y,
+                  scanout_state->dest_w, scanout_state->dest_h,
+                  scanout_state->src_x, scanout_state->src_y,
+                  scanout_state->src_w, scanout_state->src_h);
+    if (ret) {
+        weston_log("set plane failed: %s\n", strerror(errno));
+        goto err;
+    }
+
     pinfo = scanout_state->fb->format;
     drm_debug(backend, "\t[CRTC:%u, PLANE:%u] FORMAT: %s\n",
                crtc->crtc_id, scanout_state->plane->plane_id,
diff --git a/libweston/backend-drm/modes.c b/libweston/backend-drm/modes.c
index a071375..e7bbfa5 100644
--- a/libweston/backend-drm/modes.c
+++ b/libweston/backend-drm/modes.c
@@ -378,6 +378,7 @@ drm_refresh_rate_mHz(const drmModeModeInfo *info)
 static struct drm_mode *
 drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
 {
+    struct drm_backend *b = to_drm_backend(output->base.compositor);
     struct drm_mode *mode;
 
     mode = malloc(sizeof *mode);
@@ -388,6 +389,11 @@ drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
     mode->base.width = info->hdisplay;
     mode->base.height = info->vdisplay;
 
+    if (b->virtual_width && b->virtual_height) {
+        mode->base.width = b->virtual_width;
+        mode->base.height = b->virtual_height;
+    }
+
     mode->base.refresh = drm_refresh_rate_mHz(info);
     mode->mode_info = *info;
     mode->blob_id = 0;
@@ -434,20 +440,34 @@ drm_output_print_modes(struct drm_output *output)
     struct weston_mode *m;
     struct drm_mode *dm;
     const char *aspect_ratio;
+    bool virtual_size = false;
 
     wl_list_for_each(m, &output->base.mode_list, link) {
         dm = to_drm_mode(m);
 
         aspect_ratio = aspect_ratio_to_string(m->aspect_ratio);
         weston_log_continue(STAMP_SPACE "%dx%d@%.1f%s%s%s, %.1f MHz\n",
-                    m->width, m->height, m->refresh / 1000.0,
+                    dm->mode_info.hdisplay,
+                    dm->mode_info.vdisplay,
+                    m->refresh / 1000.0,
                     aspect_ratio,
                     m->flags & WL_OUTPUT_MODE_PREFERRED ?
                     ", preferred" : "",
                     m->flags & WL_OUTPUT_MODE_CURRENT ?
                     ", current" : "",
                     dm->mode_info.clock / 1000.0);
+
+        if(m->flags & WL_OUTPUT_MODE_CURRENT &&
+           (dm->mode_info.hdisplay != m->width ||
+            dm->mode_info.vdisplay != m->height))
+            virtual_size = true;
     }
+
+    if (virtual_size)
+        weston_log("Output %s: using virtual size %dx%d\n",
+               output->base.name,
+               output->base.current_mode->width,
+               output->base.current_mode->height);
 }
 
 
-- 
2.20.1