summaryrefslogtreecommitdiff
path: root/Kernel/Graphics/VirtIOGPU/FramebufferDevice.cpp
blob: 52160312d43bec5f11a5822272eccbc40454c588 (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
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
/*
 * Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Graphics/VirtIOGPU/FramebufferDevice.h>
#include <Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h>
#include <LibC/sys/ioctl_numbers.h>

namespace Kernel::Graphics::VirtIOGPU {

RefPtr<GraphicsAdapter> FramebufferDevice::adapter() const
{
    auto adapter = m_graphics_adapter.strong_ref();
    // FIXME: Propagate error gracefully
    VERIFY(adapter);
    return static_cast<GraphicsAdapter&>(*adapter);
}

ErrorOr<size_t> FramebufferDevice::buffer_length(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    SpinlockLocker locker(m_resolution_lock);
    return display_info().rect.width * display_info().rect.height * 4;
}
ErrorOr<size_t> FramebufferDevice::pitch(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    SpinlockLocker locker(m_resolution_lock);
    return display_info().rect.width * 4;
}
ErrorOr<size_t> FramebufferDevice::height(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    SpinlockLocker locker(m_resolution_lock);
    return display_info().rect.height;
}
ErrorOr<size_t> FramebufferDevice::width(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    SpinlockLocker locker(m_resolution_lock);
    return display_info().rect.width;
}
ErrorOr<size_t> FramebufferDevice::vertical_offset(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    return 0;
}
ErrorOr<bool> FramebufferDevice::vertical_offsetted(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    return false;
}

ErrorOr<void> FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t)
{
    // Note: This class doesn't support multihead setup (yet!).
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    if (width > MAX_VIRTIOGPU_RESOLUTION_WIDTH || height > MAX_VIRTIOGPU_RESOLUTION_HEIGHT)
        return Error::from_errno(ENOTSUP);

    auto& info = display_info();

    SpinlockLocker locker(adapter()->operation_lock());

    info.rect = {
        .x = 0,
        .y = 0,
        .width = (u32)width,
        .height = (u32)height,
    };

    // FIXME: Would be nice to be able to return ErrorOr here.
    TRY(create_framebuffer());
    return {};
}
ErrorOr<void> FramebufferDevice::set_head_buffer(size_t, bool)
{
    return Error::from_errno(ENOTSUP);
}
ErrorOr<void> FramebufferDevice::flush_head_buffer(size_t)
{
    // Note: This class doesn't support flushing.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally reach this code, assert.
    VERIFY_NOT_REACHED();
}
ErrorOr<void> FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& rect)
{
    SpinlockLocker locker(adapter()->operation_lock());
    Protocol::Rect dirty_rect {
        .x = rect.x,
        .y = rect.y,
        .width = rect.width,
        .height = rect.height
    };
    // FIXME: Find a better ErrorOr<void> here.
    if (!m_are_writes_active)
        return Error::from_errno(EIO);
    auto& buffer = buffer_from_index(buffer_index);
    transfer_framebuffer_data_to_host(dirty_rect, buffer);
    if (&buffer == m_current_buffer) {
        // Flushing directly to screen
        flush_displayed_image(dirty_rect, buffer);
        buffer.dirty_rect = {};
    } else {
        if (buffer.dirty_rect.width == 0 || buffer.dirty_rect.height == 0) {
            buffer.dirty_rect = dirty_rect;
        } else {
            auto current_dirty_right = buffer.dirty_rect.x + buffer.dirty_rect.width;
            auto current_dirty_bottom = buffer.dirty_rect.y + buffer.dirty_rect.height;
            buffer.dirty_rect.x = min(buffer.dirty_rect.x, dirty_rect.x);
            buffer.dirty_rect.y = min(buffer.dirty_rect.y, dirty_rect.y);
            buffer.dirty_rect.width = max(current_dirty_right, dirty_rect.x + dirty_rect.width) - buffer.dirty_rect.x;
            buffer.dirty_rect.height = max(current_dirty_bottom, dirty_rect.y + dirty_rect.height) - buffer.dirty_rect.y;
        }
    }
    return {};
}

ErrorOr<ByteBuffer> FramebufferDevice::get_edid(size_t head) const
{
    // Note: This FramebufferDevice class doesn't support multihead setup.
    // We take care to verify this at the GenericFramebufferDevice::ioctl method
    // so if we happen to accidentally have a value different than 0, assert.
    VERIFY(head == 0);
    return adapter()->get_edid(m_scanout.value());
}

FramebufferDevice::FramebufferDevice(GraphicsAdapter const& adapter, ScanoutID scanout)
    : GenericFramebufferDevice(adapter)
    , m_scanout(scanout)
{
    if (display_info().enabled) {
        // FIXME: This should be in a place where we can handle allocation failures.
        auto result = create_framebuffer();
        VERIFY(!result.is_error());
    }
}

FramebufferDevice::~FramebufferDevice()
{
}

ErrorOr<void> FramebufferDevice::create_framebuffer()
{
    // First delete any existing framebuffers to free the memory first
    m_framebuffer = nullptr;
    m_framebuffer_sink_vmobject = nullptr;

    // Allocate frame buffer for both front and back
    auto& info = display_info();
    m_buffer_size = calculate_framebuffer_size(info.rect.width, info.rect.height);
    auto region_name = TRY(KString::formatted("VirtGPU FrameBuffer #{}", m_scanout.value()));
    m_framebuffer = TRY(MM.allocate_kernel_region(m_buffer_size * 2, region_name->view(), Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow));
    auto write_sink_page = TRY(MM.allocate_user_physical_page(Memory::MemoryManager::ShouldZeroFill::No));
    auto num_needed_pages = m_framebuffer->vmobject().page_count();

    NonnullRefPtrVector<Memory::PhysicalPage> pages;
    for (auto i = 0u; i < num_needed_pages; ++i) {
        TRY(pages.try_append(write_sink_page));
    }
    m_framebuffer_sink_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_physical_pages(pages.span()));

    SpinlockLocker locker(adapter()->operation_lock());
    m_current_buffer = &buffer_from_index(m_last_set_buffer_index.load());
    create_buffer(m_main_buffer, 0, m_buffer_size);
    create_buffer(m_back_buffer, m_buffer_size, m_buffer_size);

    return {};
}

void FramebufferDevice::create_buffer(Buffer& buffer, size_t framebuffer_offset, size_t framebuffer_size)
{
    buffer.framebuffer_offset = framebuffer_offset;
    buffer.framebuffer_data = m_framebuffer->vaddr().as_ptr() + framebuffer_offset;

    auto& info = display_info();

    // 1. Create BUFFER using VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
    if (buffer.resource_id.value() != 0)
        adapter()->delete_resource(buffer.resource_id);
    buffer.resource_id = adapter()->create_2d_resource(info.rect);

    // 2. Attach backing storage using  VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
    adapter()->ensure_backing_storage(buffer.resource_id, *m_framebuffer, buffer.framebuffer_offset, framebuffer_size);
    // 3. Use VIRTIO_GPU_CMD_SET_SCANOUT to link the framebuffer to a display scanout.
    if (&buffer == m_current_buffer)
        adapter()->set_scanout_resource(m_scanout.value(), buffer.resource_id, info.rect);
    // 4. Render our test pattern
    draw_ntsc_test_pattern(buffer);
    // 5. Use VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D to update the host resource from guest memory.
    transfer_framebuffer_data_to_host(info.rect, buffer);
    // 6. Use VIRTIO_GPU_CMD_RESOURCE_FLUSH to flush the updated resource to the display.
    if (&buffer == m_current_buffer)
        flush_displayed_image(info.rect, buffer);

    // Make sure we constrain the existing dirty rect (if any)
    if (buffer.dirty_rect.width != 0 || buffer.dirty_rect.height != 0) {
        auto dirty_right = buffer.dirty_rect.x + buffer.dirty_rect.width;
        auto dirty_bottom = buffer.dirty_rect.y + buffer.dirty_rect.height;
        buffer.dirty_rect.width = min(dirty_right, info.rect.x + info.rect.width) - buffer.dirty_rect.x;
        buffer.dirty_rect.height = min(dirty_bottom, info.rect.y + info.rect.height) - buffer.dirty_rect.y;
    }

    info.enabled = 1;
}

Protocol::DisplayInfoResponse::Display const& FramebufferDevice::display_info() const
{
    return adapter()->display_info(m_scanout);
}

Protocol::DisplayInfoResponse::Display& FramebufferDevice::display_info()
{
    return adapter()->display_info(m_scanout);
}

void FramebufferDevice::transfer_framebuffer_data_to_host(Protocol::Rect const& rect, Buffer& buffer)
{
    adapter()->transfer_framebuffer_data_to_host(m_scanout, buffer.resource_id, rect);
}

void FramebufferDevice::flush_dirty_window(Protocol::Rect const& dirty_rect, Buffer& buffer)
{
    adapter()->flush_dirty_rectangle(m_scanout, buffer.resource_id, dirty_rect);
}

void FramebufferDevice::flush_displayed_image(Protocol::Rect const& dirty_rect, Buffer& buffer)
{
    adapter()->flush_displayed_image(buffer.resource_id, dirty_rect);
}

void FramebufferDevice::set_buffer(int buffer_index)
{
    auto& buffer = buffer_index == 0 ? m_main_buffer : m_back_buffer;
    SpinlockLocker locker(adapter()->operation_lock());
    if (&buffer == m_current_buffer)
        return;
    m_current_buffer = &buffer;
    adapter()->set_scanout_resource(m_scanout.value(), buffer.resource_id, display_info().rect);
    adapter()->flush_displayed_image(buffer.resource_id, buffer.dirty_rect); // QEMU SDL backend requires this (as per spec)
    buffer.dirty_rect = {};
}

ErrorOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
{
    TRY(process.require_promise(Pledge::video));
    if (!shared)
        return ENODEV;
    if (offset != 0 || !m_framebuffer)
        return ENXIO;
    if (range.size() > m_framebuffer->size())
        return EOVERFLOW;

    // We only allow one process to map the region
    if (m_userspace_mmap_region)
        return ENOMEM;

    RefPtr<Memory::VMObject> vmobject;
    if (m_are_writes_active) {
        vmobject = TRY(m_framebuffer->vmobject().try_clone());
    } else {
        vmobject = m_framebuffer_sink_vmobject;
        if (vmobject.is_null())
            return ENOMEM;
    }

    m_userspace_mmap_region = TRY(process.address_space().allocate_region_with_vmobject(
        range,
        vmobject.release_nonnull(),
        0,
        "VirtIOGPU Framebuffer",
        prot,
        shared));

    return m_userspace_mmap_region.unsafe_ptr();
}

void FramebufferDevice::deactivate_writes()
{
    m_are_writes_active = false;
    if (m_userspace_mmap_region) {
        auto* region = m_userspace_mmap_region.unsafe_ptr();
        auto maybe_vm_object = m_framebuffer_sink_vmobject->try_clone();
        // FIXME: Would be nice to be able to return a ErrorOr<void> here.
        VERIFY(!maybe_vm_object.is_error());
        region->set_vmobject(maybe_vm_object.release_value());
        region->remap();
    }
    set_buffer(0);
    clear_to_black(buffer_from_index(0));
}

void FramebufferDevice::activate_writes()
{
    m_are_writes_active = true;
    auto last_set_buffer_index = m_last_set_buffer_index.load();
    if (m_userspace_mmap_region) {
        auto* region = m_userspace_mmap_region.unsafe_ptr();
        region->set_vmobject(m_framebuffer->vmobject());
        region->remap();
    }
    set_buffer(last_set_buffer_index);
}

void FramebufferDevice::clear_to_black(Buffer& buffer)
{
    auto& info = display_info();
    size_t width = info.rect.width;
    size_t height = info.rect.height;
    u8* data = buffer.framebuffer_data;
    for (size_t i = 0; i < width * height; ++i) {
        data[4 * i + 0] = 0x00;
        data[4 * i + 1] = 0x00;
        data[4 * i + 2] = 0x00;
        data[4 * i + 3] = 0xff;
    }
}

void FramebufferDevice::draw_ntsc_test_pattern(Buffer& buffer)
{
    static constexpr u8 colors[12][4] = {
        { 0xff, 0xff, 0xff, 0xff }, // White
        { 0x00, 0xff, 0xff, 0xff }, // Primary + Composite colors
        { 0xff, 0xff, 0x00, 0xff },
        { 0x00, 0xff, 0x00, 0xff },
        { 0xff, 0x00, 0xff, 0xff },
        { 0x00, 0x00, 0xff, 0xff },
        { 0xff, 0x00, 0x00, 0xff },
        { 0xba, 0x01, 0x5f, 0xff }, // Dark blue
        { 0x8d, 0x3d, 0x00, 0xff }, // Purple
        { 0x22, 0x22, 0x22, 0xff }, // Shades of gray
        { 0x10, 0x10, 0x10, 0xff },
        { 0x00, 0x00, 0x00, 0xff },
    };
    auto& info = display_info();
    size_t width = info.rect.width;
    size_t height = info.rect.height;
    u8* data = buffer.framebuffer_data;
    // Draw NTSC test card
    for (size_t y = 0; y < height; ++y) {
        for (size_t x = 0; x < width; ++x) {
            size_t color = 0;
            if (3 * y < 2 * height) {
                // Top 2/3 of image is 7 vertical stripes of color spectrum
                color = (7 * x) / width;
            } else if (4 * y < 3 * height) {
                // 2/3 mark to 3/4 mark  is backwards color spectrum alternating with black
                auto segment = (7 * x) / width;
                color = segment % 2 ? 10 : 6 - segment;
            } else {
                if (28 * x < 5 * width) {
                    color = 8;
                } else if (28 * x < 10 * width) {
                    color = 0;
                } else if (28 * x < 15 * width) {
                    color = 7;
                } else if (28 * x < 20 * width) {
                    color = 10;
                } else if (7 * x < 6 * width) {
                    // Grayscale gradient
                    color = 26 - ((21 * x) / width);
                } else {
                    // Solid black
                    color = 10;
                }
            }
            u8* pixel = &data[4 * (y * width + x)];
            for (int i = 0; i < 4; ++i) {
                pixel[i] = colors[color][i];
            }
        }
    }
    dbgln_if(VIRTIO_DEBUG, "Finish drawing the pattern");
}

u8* FramebufferDevice::framebuffer_data()
{
    return m_current_buffer->framebuffer_data;
}

}