added license
[rpikern] / src / video.c
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include "rpi.h"
6 #include "video.h"
7 #include "mem.h"
8 #include "debug.h"
9 #include "sysctl.h"
10
11 static void *fb_pixels;
12 static int scr_width, scr_height;
13 static int fb_width, fb_height, fb_depth, fb_size, fb_pitch;
14 static int fb_xoffs, fb_yoffs;
15
16 int video_init(void)
17 {
18         int i, j;
19         struct rpi_prop *prop;
20         uint32_t *fbptr;
21
22         scr_width = 1024;
23         scr_height = 576;
24         /*fb_width = 1920;
25         fb_height = 1024;*/
26         fb_width = scr_width;
27         fb_height = scr_height;
28         fb_depth = 32;
29
30         printf("Requesting video mode: %dx%d %d bpp (fb:%dx%d)\n", scr_width, scr_height,
31                         fb_depth, fb_width, fb_height);
32
33         rpi_prop(RPI_TAG_ALLOCFB, 16);
34         rpi_prop(RPI_TAG_SETFBVIRT, scr_width, scr_height);
35         rpi_prop(RPI_TAG_SETFBPHYS, fb_width, fb_height);
36         rpi_prop(RPI_TAG_SETFBDEPTH, fb_depth);
37
38         rpi_prop_send();
39
40         while((prop = rpi_prop_next())) {
41                 switch(prop->id) {
42                 case RPI_TAG_SETFBPHYS:
43                         fb_width = prop->data[0];
44                         fb_height = prop->data[1];
45                         break;
46
47                 case RPI_TAG_SETFBVIRT:
48                         scr_width = prop->data[0];
49                         scr_height = prop->data[1];
50                         break;
51
52                 case RPI_TAG_SETFBDEPTH:
53                         fb_depth = prop->data[0];
54                         break;
55
56                 case RPI_TAG_ALLOCFB:
57                         fb_pixels = (void*)(prop->data[0] & 0x3fffffff);
58                         fb_size = prop->data[1];
59                         break;
60
61                 default:
62                         break;
63                 }
64         }
65
66         if(!fb_pixels) {
67                 rpi_prop(RPI_TAG_ALLOCFB, 16);
68                 if(rpi_prop_send() == -1 || !(prop = rpi_prop_find(RPI_TAG_ALLOCFB))) {
69                         printf("Failed to allocate framebuffer\n");
70                         return -1;
71                 }
72                 fb_pixels = (void*)(prop->data[0] & 0x3fffffff);
73                 fb_size = prop->data[1];
74         }
75
76         rpi_prop(RPI_TAG_GETFBPITCH);
77         rpi_prop(RPI_TAG_GETFBOFFS);
78         rpi_prop_send();
79         /*
80         if(rpi_prop_send() == -1) {
81                 printf("Failed to get pitch\n");
82                 return -1;
83         }
84         */
85
86         while((prop = rpi_prop_next())) {
87                 switch(prop->id) {
88                 case RPI_TAG_GETFBPITCH:
89                         fb_pitch = prop->data[0];
90                         break;
91
92                 case RPI_TAG_GETFBOFFS:
93                         fb_xoffs = prop->data[0];
94                         fb_yoffs = prop->data[1];
95                         break;
96
97                 default:
98                         break;
99                 }
100         }
101
102         printf("Got video mode: %dx%d (%d bpp)\n", scr_width, scr_height, fb_depth);
103         printf("Framebuffer: %dx%d at %p (%d bytes)\n", fb_width, fb_height, fb_pixels, fb_size);
104         printf("  virtual offset: %d, %d\n", fb_xoffs, fb_yoffs);
105         printf("  scanline pitch: %d\n", fb_pitch);
106
107         fbptr = fb_pixels;
108         for(i=0; i<fb_height; i++) {
109                 for(j=0; j<fb_width; j++) {
110                         int r = i ^ j;
111                         int g = (i ^ j) >> 1;
112                         int b = (i ^ j) >> 2;
113                         fbptr[j] = b | (g << 8) | (r << 16) | 0xff000000;
114                 }
115                 fbptr += fb_pitch >> 2;
116         }
117
118         sysctl_dcache_clean_inval((uint32_t)fb_pixels, fb_size);
119         return 0;
120 }
121
122 int video_scroll(int x, int y)
123 {
124         struct rpi_prop *prop;
125
126         rpi_prop(RPI_TAG_SETFBOFFS, x, y);
127         if(rpi_prop_send() == -1 || !(prop = rpi_prop_find(RPI_TAG_SETFBOFFS))) {
128                 return -1;
129         }
130
131         fb_xoffs = prop->data[0];
132         fb_yoffs = prop->data[1];
133         return 0;
134 }
135
136 void video_update(int dt)
137 {
138         static int dirx = 1, diry = 1;
139         int nx, ny;
140
141         nx = fb_xoffs + dirx * dt;
142         ny = fb_yoffs + diry * dt;
143
144         if(nx < 0) {
145                 nx = 0;
146                 dirx = -dirx;
147         } else if(nx + scr_width >= fb_width) {
148                 nx = fb_width - scr_width;
149                 dirx = -dirx;
150         }
151         if(ny < 0) {
152                 ny = 0;
153                 diry = -diry;
154         } else if(ny + scr_height >= fb_height) {
155                 ny = fb_height - scr_height;
156                 diry = -diry;
157         }
158
159         video_scroll(nx, ny);
160 }