more x86_64 work. started to put the mmu bits together in the (former) stage2 loader.
[newos.git] / apps / window_server / Renderer_vesa.cpp
blob38cea4bb87fdcca6e333390ab74609f9e52e697a
1 #include "Renderer_vesa.h"
2 #include "assert.h"
3 #include <win/Color.h>
5 #define MIN(a, b) ((a) < (b) ? (a) : (b))
7 const color555 kCursorBits555 [] = {
8 0x7fff,0x7fff,0x00,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
9 0x7fff,0x7fff,0x7fff,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
10 TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x00,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
11 TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
12 TRANS555COLOR,TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
13 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
14 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,0x7fff,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
15 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x7fff,0x7fff,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
16 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x7fff,0x7fff,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
17 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
18 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
19 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,
20 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,TRANS555COLOR,
21 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,0x00,TRANS555COLOR,
22 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,0x7fff,TRANS555COLOR,
23 TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR,TRANS555COLOR
26 const color565 kCursorBits565 [] = {
27 0xffff,0xffff,0x00,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
28 0xffff,0xffff,0xffff,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
29 TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0x00,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
30 TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
31 TRANS565COLOR,TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
32 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
33 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
34 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0xffff,0xffff,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
35 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0xffff,0xffff,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
36 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
37 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
38 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,
39 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,TRANS565COLOR,
40 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,0x00,TRANS565COLOR,
41 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,0xffff,TRANS565COLOR,
42 TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR,TRANS565COLOR
45 const color888 kCursorBits888 [] = {
46 0xffffff,0xffffff,0x00,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
47 0xffffff,0xffffff,0xffffff,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
48 TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0x00,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
49 TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
50 TRANS888COLOR,TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
51 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
52 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,0xffffff,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
53 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0xffffff,0xffffff,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
54 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0xffffff,0xffffff,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
55 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
56 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
57 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,
58 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,TRANS888COLOR,
59 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,0x00,TRANS888COLOR,
60 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,0xffffff,TRANS888COLOR,
61 TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR,TRANS888COLOR
65 Renderer_vesa_555::Renderer_vesa_555(char *baseAddress, int width, int height, int bytesPerRow)
66 : Renderer_RGB555(baseAddress, width, height, bytesPerRow)
68 fCursorX = width / 2;
69 fCursorY = height / 2;
70 DrawCursor();
73 const unsigned kBottom = 1;
74 const unsigned kTop = 2;
75 const unsigned kLeft = 4;
76 const unsigned kRight = 8;
78 #define clipmask(x, y, rect) \
79 ({ unsigned mask = 0; \
80 if (x < rect.left) mask |= kLeft; \
81 else if (x > rect.right) mask |= kRight; \
82 if (y < rect.top) mask |= kTop; \
83 else if (y > rect.bottom) mask |= kBottom; \
84 mask;})
86 inline int vert_intersection(int x1, int y1, int x2, int y2, int x)
88 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
91 inline int horz_intersection(int x1, int y1, int x2, int y2, int y)
93 return x1 + (x2 - x1) * (y - y1) / (y2 - y1);
96 void Renderer_vesa_555::DrawLine(int x1, int y1, int x2, int y2, color32 color, color_space cspace)
98 Rect cursorRect(fCursorX, fCursorY, fCursorX + 16 - 1, fCursorY + 16 - 1);
99 bool invalidateCursor = true;
101 int clippedX1 = x1;
102 int clippedY1 = y1;
103 int clippedX2 = x2;
104 int clippedY2 = y2;
105 unsigned point1mask = clipmask(clippedX1, clippedY1, cursorRect);
106 unsigned point2mask = clipmask(clippedX2, clippedY2, cursorRect);
107 while (point1mask != 0 || point2mask != 0) {
108 if ((point1mask & point2mask) != 0) {
109 invalidateCursor = false;
110 break;
113 unsigned mask = point1mask ? point1mask : point2mask;
114 int x = 0;
115 int y = 0;
116 if (mask & kBottom) {
117 y = cursorRect.bottom;
118 x = horz_intersection(clippedX1, clippedY1, clippedX2, clippedY2, y);
119 } else if (mask & kTop) {
120 y = cursorRect.top;
121 x = horz_intersection(clippedX1, clippedY1, clippedX2, clippedY2, y);
122 } else if (mask & kRight) {
123 x = cursorRect.right;
124 y = vert_intersection(clippedX1, clippedY1, clippedX2, clippedY2, x);
125 } else if (mask & kLeft) {
126 x = cursorRect.left;
127 y = vert_intersection(clippedX1, clippedY1, clippedX2, clippedY2, x);
130 if (point1mask) {
131 // Clip point 1
132 point1mask = clipmask(x, y, cursorRect);
133 clippedX1 = x;
134 clippedY1 = y;
135 } else {
136 // Clip point 2
137 point2mask = clipmask(x, y, cursorRect);
138 clippedX2 = x;
139 clippedY2 = y;
143 if (invalidateCursor)
144 EraseCursor();
146 Renderer_RGB555::DrawLine(x1, y1, x2, y2, color, cspace);
148 if (invalidateCursor)
149 DrawCursor();
152 void Renderer_vesa_555::FillRect(int x1, int y1, int x2, int y2, color32 color, color_space cspace)
154 bool invalidateCursor = false;
155 if (Rect(x1, y1, x2, y2).Intersects(Rect(fCursorX, fCursorY, fCursorX + 16 - 1,
156 fCursorY + 16 - 1))) {
157 invalidateCursor = true;
158 EraseCursor();
161 Renderer_RGB555::FillRect(x1, y1, x2, y2, color, cspace);
162 if (invalidateCursor)
163 DrawCursor();
166 void Renderer_vesa_555::Blit(int x, int y, const void *image, color_space cspace, int imageWidth,
167 int imageHeight, int imageStrideWidth)
170 bool invalidateCursor = false;
171 if (Rect(x, y, x + imageWidth, y + imageHeight).Intersects(Rect(fCursorX,
172 fCursorY, fCursorX + 16 - 1, fCursorY + 16 - 1))) {
173 invalidateCursor = true;
174 EraseCursor();
177 Renderer_RGB555::Blit(x, y, image, cspace, imageWidth, imageHeight, imageStrideWidth);
178 if (invalidateCursor)
179 DrawCursor();
182 void Renderer_vesa_555::StretchBlit(const Rect &imageRect, const Rect &displayRect, const void *image, color_space cspace,
183 int imageStrideWidth)
186 bool invalidateCursor = false;
187 if (displayRect.Intersects(Rect(fCursorX, fCursorY, fCursorX + 16 - 1,
188 fCursorY + 16 - 1))) {
189 invalidateCursor = true;
190 EraseCursor();
193 Renderer_RGB555::StretchBlit(imageRect, displayRect, image, cspace, imageStrideWidth);
195 if (invalidateCursor)
196 DrawCursor();
199 void Renderer_vesa_555::CopyRect(const Rect &source, const Rect &dest)
201 bool invalidateCursor = false;
202 Rect cursorRect(fCursorX, fCursorY, fCursorX + 16 - 1, fCursorY + 16 - 1);
203 if (cursorRect.Intersects(source) || cursorRect.Intersects(dest)) {
204 invalidateCursor = true;
205 EraseCursor();
208 Renderer_RGB555::CopyRect(source, dest);
209 if (invalidateCursor)
210 DrawCursor();
213 void Renderer_vesa_555::SetCursorPosition(int x, int y)
215 EraseCursor();
216 fCursorX = x;
217 fCursorY = y;
218 DrawCursor();
221 void Renderer_vesa_555::EraseCursor()
223 color555 *screen_ptr = (color555 *)BufferBaseAddress() + fCursorX + fCursorY * (BufferBytesPerRow() / 2);
224 color555 *saveBackground = fSavedBackground;
225 int cursorDisplayWidth = MIN(16, (BufferBytesPerRow() / 2) - fCursorX);
226 int cursorDisplayHeight = MIN(16, BufferHeight() - fCursorY);
227 int stride = (BufferBytesPerRow() / 2) - cursorDisplayWidth;
229 for (int y = 0; y < cursorDisplayHeight; y++) {
230 for (int x = 0; x < cursorDisplayWidth; x++) {
231 if (*saveBackground != TRANS555COLOR)
232 *screen_ptr = *saveBackground;
234 screen_ptr++;
235 saveBackground++;
238 screen_ptr += stride;
239 saveBackground += 16 - cursorDisplayWidth;
243 void Renderer_vesa_555::DrawCursor()
245 color555 *screen_ptr = (color555 *)BufferBaseAddress() + fCursorX + fCursorY * (BufferBytesPerRow() / 2);
246 color555 *cursorImage = (color555 *)kCursorBits555;
247 color555 *saveBackground = fSavedBackground;
248 int cursorDisplayWidth = MIN(16, (BufferBytesPerRow() / 2) - fCursorX);
249 int cursorDisplayHeight = MIN(16, BufferHeight() - fCursorY);
250 int stride = (BufferBytesPerRow() / 2) - cursorDisplayWidth;
252 for (int y = 0; y < cursorDisplayHeight; y++) {
253 for (int x = 0; x < cursorDisplayWidth; x++) {
254 if (*cursorImage != TRANS555COLOR) {
255 *saveBackground = *screen_ptr;
256 *screen_ptr = *cursorImage;
257 } else
258 *saveBackground = TRANS555COLOR;
260 screen_ptr++;
261 cursorImage++;
262 saveBackground++;
265 screen_ptr += stride;
266 cursorImage += 16 - cursorDisplayWidth;
267 saveBackground += 16 - cursorDisplayWidth;