blob: 7f075ce776ab4a9646c7ecc16d79886997e4a6ce [file] [log] [blame]
pbrook714fa302009-04-01 12:27:59 +00001/*
2 * Framebuffer device helper routines
3 *
4 * Copyright (c) 2009 CodeSourcery
5 * Written by Paul Brook <paul@codesourcery.com>
6 *
7 * This code is licensed under the GNU GPLv2.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01008 *
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
pbrook714fa302009-04-01 12:27:59 +000011 */
12
13/* TODO:
14 - Do something similar for framebuffers with local ram
15 - Handle rotation here instead of hacking dest_pitch
16 - Use common pixel conversion routines instead of per-device drawfn
17 - Remove all DisplayState knowledge from devices.
18 */
19
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010020#include "hw/hw.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010021#include "ui/console.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010022#include "framebuffer.h"
pbrook714fa302009-04-01 12:27:59 +000023
Paolo Bonzinic1076c32015-07-13 12:00:29 +020024void framebuffer_update_memory_section(
25 MemoryRegionSection *mem_section,
26 MemoryRegion *root,
27 hwaddr base,
28 unsigned rows,
29 unsigned src_width)
30{
31 hwaddr src_len = (hwaddr)rows * src_width;
32
33 if (mem_section->mr) {
34 memory_region_set_log(mem_section->mr, false, DIRTY_MEMORY_VGA);
35 memory_region_unref(mem_section->mr);
36 mem_section->mr = NULL;
37 }
38
39 *mem_section = memory_region_find(root, base, src_len);
40 if (!mem_section->mr) {
41 return;
42 }
43
44 if (int128_get64(mem_section->size) < src_len ||
45 !memory_region_is_ram(mem_section->mr)) {
46 memory_region_unref(mem_section->mr);
47 mem_section->mr = NULL;
48 return;
49 }
50
51 memory_region_set_log(mem_section->mr, true, DIRTY_MEMORY_VGA);
52}
53
pbrook714fa302009-04-01 12:27:59 +000054/* Render an image from a shared memory framebuffer. */
pbrook714fa302009-04-01 12:27:59 +000055void framebuffer_update_display(
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010056 DisplaySurface *ds,
Paolo Bonzinic1076c32015-07-13 12:00:29 +020057 MemoryRegionSection *mem_section,
pbrook714fa302009-04-01 12:27:59 +000058 int cols, /* Width in pixels. */
Stefan Weil9c6bb552012-08-11 21:32:02 +020059 int rows, /* Height in pixels. */
pbrook714fa302009-04-01 12:27:59 +000060 int src_width, /* Length of source line, in bytes. */
61 int dest_row_pitch, /* Bytes between adjacent horizontal output pixels. */
62 int dest_col_pitch, /* Bytes between adjacent vertical output pixels. */
63 int invalidate, /* nonzero to redraw the whole image. */
64 drawfn fn,
65 void *opaque,
66 int *first_row, /* Input and output. */
67 int *last_row /* Output only */)
68{
Avi Kivitya8170e52012-10-23 12:30:10 +020069 hwaddr src_len;
pbrook714fa302009-04-01 12:27:59 +000070 uint8_t *dest;
71 uint8_t *src;
pbrook714fa302009-04-01 12:27:59 +000072 int first, last = 0;
73 int dirty;
74 int i;
Anthony Liguoric227f092009-10-01 16:12:16 -050075 ram_addr_t addr;
Avi Kivity75c9d6c2011-12-08 16:00:54 +020076 MemoryRegion *mem;
pbrook714fa302009-04-01 12:27:59 +000077
78 i = *first_row;
79 *first_row = -1;
80 src_len = src_width * rows;
81
Paolo Bonzinic1076c32015-07-13 12:00:29 +020082 mem = mem_section->mr;
83 if (!mem) {
84 return;
pbrook714fa302009-04-01 12:27:59 +000085 }
Avi Kivityc1cd0b22011-12-15 16:07:48 +020086 memory_region_sync_dirty_bitmap(mem);
Paolo Bonzinid55d4202015-03-23 10:46:52 +010087
Paolo Bonzinic1076c32015-07-13 12:00:29 +020088 addr = mem_section->offset_within_region;
89 src = memory_region_get_ram_ptr(mem) + addr;
90
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010091 dest = surface_data(ds);
Paolo Bonzinic1076c32015-07-13 12:00:29 +020092 if (dest_col_pitch < 0) {
pbrook714fa302009-04-01 12:27:59 +000093 dest -= dest_col_pitch * (cols - 1);
Paolo Bonzinic1076c32015-07-13 12:00:29 +020094 }
Vasily Khoruzhick93128052011-06-17 13:04:36 +030095 if (dest_row_pitch < 0) {
96 dest -= dest_row_pitch * (rows - 1);
97 }
pbrook714fa302009-04-01 12:27:59 +000098 first = -1;
pbrook714fa302009-04-01 12:27:59 +000099
100 addr += i * src_width;
101 src += i * src_width;
102 dest += i * dest_row_pitch;
103
104 for (; i < rows; i++) {
Blue Swirld1f3dd32012-02-04 17:09:14 +0000105 dirty = memory_region_get_dirty(mem, addr, src_width,
Avi Kivity75c9d6c2011-12-08 16:00:54 +0200106 DIRTY_MEMORY_VGA);
pbrook714fa302009-04-01 12:27:59 +0000107 if (dirty || invalidate) {
108 fn(opaque, dest, src, cols, dest_col_pitch);
109 if (first == -1)
110 first = i;
111 last = i;
112 }
113 addr += src_width;
114 src += src_width;
115 dest += dest_row_pitch;
116 }
pbrook714fa302009-04-01 12:27:59 +0000117 if (first < 0) {
Paolo Bonzinic1076c32015-07-13 12:00:29 +0200118 return;
pbrook714fa302009-04-01 12:27:59 +0000119 }
Paolo Bonzinic1076c32015-07-13 12:00:29 +0200120 memory_region_reset_dirty(mem, mem_section->offset_within_region, src_len,
Avi Kivity75c9d6c2011-12-08 16:00:54 +0200121 DIRTY_MEMORY_VGA);
pbrook714fa302009-04-01 12:27:59 +0000122 *first_row = first;
123 *last_row = last;
pbrook714fa302009-04-01 12:27:59 +0000124}