-
Notifications
You must be signed in to change notification settings - Fork 2
/
framebuffer.c
150 lines (124 loc) · 3.7 KB
/
framebuffer.c
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
#include "framebuffer.h"
#include "mailbox.h"
#include "mmu.h"
#include "linker.h"
#include "mem.h"
#include "dma.h"
#define USE_DMA
#define VIDEOBUS_OFFSET 0x40000000
static struct FBInfo doubleFb;
static struct FBInfo physicalFb;
static void *physicalBuffer;
/* Please see
* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
*/
struct FBInfo *FBInit(uint32_t width, uint32_t height)
{
uint32_t __attribute__((aligned(16))) sequence[] = {
0, // size of the whole structure
0, // request
// TAG 0
0x48003, // Set width/height
8, // size
8, // request
width,
height,
// TAG 1
0x48004, // Set virtual width/height (??)
8, // size
8, // request
width,
height,
// TAG 2
0x48005, // Set depth
4, // size
4, // request
32, // depth
// TAG 3
0x40008, // Get pitch
4, // size
4, //request
0,
// TAG 4
0x40001, // Allocate framebuffer (cross your fingers)
8, // size
8, // request
16, // framebuffer alignment
0,
0 // End TAG
};
sequence[0] = sizeof(sequence);
// Send the requested values
MailboxSend(8, ((uint32_t)sequence) + VIDEOBUS_OFFSET);
// Now get the real values
if(MailboxReceive(8) == 0 || sequence[1] == 0x80000000) // Ok
{
const uint32_t *ptr = sequence + 2;
while(*ptr)
{
switch(*ptr++)
{
case 0x40008:
physicalFb.pitch = ptr[2];
break;
case 0x48003:
physicalFb.width = ptr[2];
physicalFb.height = ptr[3];
break;
case 0x40001:
physicalBuffer = (uint32_t*) ptr[2];
physicalFb.ptr = physicalBuffer;
break;
case 0x48004:
case 0x48005:
break;
default: // Panic
return 0;
}
ptr += *ptr / 4 + 2;
}
return &physicalFb;
}
return 0;
}
void FBPutColor(uint32_t x, uint32_t y, uint32_t color)
{
doubleFb.ptr[x + y * (doubleFb.pitch >> 2)] = color;
}
void FBConvertBufferToVirtualSpace(void)
{
size_t bufferSize = physicalFb.height * physicalFb.pitch;
uint32_t *newPtr = Memalloc(bufferSize, 0x1000);
newPtr = (uint32_t*) VirtualHeapAddressOf(newPtr);
MMUPopulateRange((uint32_t) newPtr, (uint32_t) physicalFb.ptr,
physicalFb.height * physicalFb.pitch, READWRITE);
physicalFb.ptr = newPtr;
doubleFb.ptr = (uint32_t*) VirtualHeapAddressOf(doubleFb.ptr);
}
struct FBInfo *FBCreateDoubleBuffer(void)
{
size_t bufferSize = physicalFb.height * physicalFb.pitch;
uint32_t *doubleBuffer = Memalloc(bufferSize, 0x1000);
uint32_t VADoubleBuffer = VirtualHeapAddressOf(doubleBuffer);
MMUPopulateRange(VADoubleBuffer, (uint32_t) doubleBuffer,
physicalFb.height * physicalFb.pitch, READWRITE);
doubleFb.ptr = doubleBuffer;
doubleFb.width = physicalFb.width;
doubleFb.height = physicalFb.height;
doubleFb.pitch = physicalFb.pitch;
return &doubleFb;
}
void FBCopyDoubleBuffer(void)
{
#ifdef USE_DMA
size_t bufferSize = (physicalFb.height * physicalFb.pitch); // Unit: bytes
DMACopy32(physicalBuffer, (uint32_t*)PhysicalHeapAddressOf(doubleFb.ptr),
bufferSize);
#else
size_t i = (physicalFb.height * physicalFb.pitch) >> 2; // Unit: 32bit words
while(i--)
{
physicalFb.ptr[i] = doubleFb.ptr[i];
}
#endif
}