drawing_test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // This file is Copyright (c) 2023 Victor Suarez Rovere <suarezvictor@gmail.com>
  2. // SPDX-License-Identifier: AGPL-3.0-only
  3. #define RAND_SEED 0xABCDEF
  4. static uint32_t lfsr = RAND_SEED; //won't work until suitable seed is set
  5. void srand32(uint32_t seed)
  6. {
  7. lfsr = seed;
  8. }
  9. uint32_t rand32(void)
  10. {
  11. const uint32_t POLY_MASK = 0xB4BCD35C;
  12. int feedback = lfsr & 1;
  13. lfsr >>= 1;
  14. if (feedback)
  15. lfsr ^= POLY_MASK;
  16. return lfsr;
  17. }
  18. // drawing tests -----------------------------------------------------------------------------------
  19. static accel_rectangle_fill32_layout_t *rectangle_regs = accel_rectangle_fill32_regs;
  20. static accel_ellipse_fill32_layout_t *ellipse_regs = accel_ellipse_fill32_regs;
  21. static accel_line32_layout_t *line_regs = accel_line32_regs;
  22. void draw_char(char ch, int x, int y, int width, int height, uint32_t rgba)
  23. {
  24. if(ch == ':')
  25. {
  26. int x0 = x+4*width;
  27. int y0 = y+2*height;
  28. accel_ellipse_fill(ellipse_regs, x0, y0, x0+2*width, y0+4*height, rgba);
  29. y0 += 6*width;
  30. #if defined(FRAMEBUFFER_CACHE_HANDLING) && !defined(CSR_ACCEL_RECTANGLE_FILL32_BASE) && defined(CSR_ACCEL_ELLIPSE_FILL32_BASE)
  31. flush_l2_cache(); //corrects screen pixels
  32. flush_cpu_dcache();
  33. #endif
  34. accel_ellipse_fill(ellipse_regs, x0, y0, x0+2*width, y0+4*height, rgba);
  35. return;
  36. }
  37. if(ch == '.')
  38. {
  39. int x0 = x+4*width;
  40. int y0 = y+12*height;
  41. #if defined(FRAMEBUFFER_CACHE_HANDLING) && !defined(CSR_ACCEL_RECTANGLE_FILL32_BASE) && defined(CSR_ACCEL_ELLIPSE_FILL32_BASE)
  42. flush_l2_cache(); //corrects screen pixels
  43. flush_cpu_dcache();
  44. #endif
  45. accel_ellipse_fill(ellipse_regs, x0, y0, x0+2*width, y0+2*height, rgba);
  46. return;
  47. }
  48. if(ch < '0' || ch > '9')
  49. return;
  50. /*
  51. 012345
  52. WWWW 0
  53. WW WW 2
  54. WW WW 4
  55. WWWW 6
  56. WW WW 8
  57. WW WW 10
  58. WWWW 12
  59. */
  60. struct segment
  61. {
  62. const char *mask;
  63. int x, y, w, h;
  64. } const segments[7]={
  65. {"1011011111", 1, 0, 4, 2}, //A (top)
  66. {"1111100111", 4, 1, 2, 6}, //B (top right)
  67. {"1101111111", 4, 7, 2, 6}, //C (bottom right)
  68. {"1011011011", 1, 12, 4, 2}, //D (bottom)
  69. {"1010001010", 0, 7, 2, 6}, //E (bottom left)
  70. {"1000111011", 0, 1, 2, 6}, //F (top left)
  71. {"0011111011", 1, 6, 4, 2}, //G (middle)
  72. };
  73. const struct segment *s = segments;
  74. for(int i=0; i < 7; ++i, ++s)
  75. {
  76. if(s->mask[ch-'0'] != '0')
  77. {
  78. int x0 = x+width*s->x;
  79. int y0 = y+height*s->y;
  80. int x1 = x0+width*s->w;
  81. int y1 = y0+height*s->h;
  82. if(x0 >= 0 && x1 < FRAME_WIDTH && y0 >= 0 && y1 < FRAME_HEIGHT)
  83. {
  84. #if defined(FRAMEBUFFER_CACHE_HANDLING) && !defined(CSR_ACCEL_ELLIPSE_FILL32_BASE) && defined(CSR_ACCEL_RECTANGLE_FILL32_BASE)
  85. //FIXME: recheck condition...
  86. flush_l2_cache(); //corrects screen pixels
  87. flush_cpu_dcache();
  88. #endif
  89. accel_rectangle_fill(rectangle_regs, x0, y0, x1, y1, rgba);
  90. }
  91. }
  92. }
  93. struct corner
  94. {
  95. const char *mask;
  96. int x, y, w, h;
  97. } const corners[6]={
  98. {"1000111011", 0, 0, 2, 2}, //top-left
  99. {"1011000111", 4, 0, 2, 2}, //top-right
  100. {"0010011000", 4, 6, 2, 2}, //center-right
  101. {"1001011011", 4, 12, 2, 2}, //bottom-right
  102. {"1010001010", 0, 12, 2, 2}, //bottom-left
  103. {"0010110001", 0, 6, 2, 2}, //center-left
  104. };
  105. const struct corner *c = corners;
  106. for(int i=0; i < 6; ++i, ++c)
  107. {
  108. if(c->mask[ch-'0'] != '0')
  109. {
  110. int x0 = x+width*c->x;
  111. int y0 = y+height*c->y;
  112. int x1 = x0+width*c->w;
  113. int y1 = y0+height*c->h;
  114. if(x0 >= 0 && x1 < FRAME_WIDTH && y0 >= 0 && y1 < FRAME_HEIGHT)
  115. {
  116. accel_ellipse_fill(ellipse_regs, x0, y0, x1, y1, rgba);
  117. }
  118. }
  119. }
  120. //accel_rectangle(line_regs, x-2, y-2, x+width*6+2, y+height*15-5, rgba); //optionally draw some lines around digits
  121. }
  122. void draw_clock(void)
  123. {
  124. static uint64_t ttarget;
  125. static int init = 0;
  126. static uint32_t bkcolor = 0xFF800000;
  127. if(!init)
  128. {
  129. ttarget = higres_ticks();
  130. init=1;
  131. accel_rectangle_fill(rectangle_regs, 0, 0, FRAME_WIDTH-1, FRAME_HEIGHT-1, bkcolor);
  132. srand32(RAND_SEED);
  133. }
  134. #define DIGITS 7 //7, 9 or 10
  135. int64_t dt = higres_ticks() - ttarget;
  136. const uint64_t period = higres_ticks_freq()/(DIGITS == 10 ? 100 : DIGITS == 9 ? 10 : 1);
  137. static char timedigits[11]=
  138. { //01234567890
  139. //XX:XX:XX.00
  140. __TIME__ ".00"
  141. };
  142. static int x0 = 10, y0 = 350;
  143. static int dx = 1, dy = 1;
  144. const int sz = 7;
  145. if(dt >= 0)
  146. {
  147. ttarget += period;
  148. int incr = 1;
  149. // 0123456789012345
  150. // XXYY:XXYY:XXYY.ZZZZ
  151. static const int pos[11]={0,2,3, 5,7,8, 10,12,13, 15,17};
  152. uint32_t color = rand32() | 0xFF004000;
  153. for(int i = DIGITS; i >= 0; --i) //9 for 1/10th seconds, 10 for 1/100th seconds
  154. {
  155. int isnum = timedigits[i] >= '0' && timedigits[i] <= '9';
  156. if(incr && isnum)
  157. {
  158. char ovf = (i==6 || i==3) ? '5':'9'; //overflow value (decimal or sexagesimal)
  159. if(timedigits[i] == ovf)
  160. timedigits[i]='0';
  161. else
  162. {
  163. ++timedigits[i];
  164. incr = 0; //stop carry
  165. }
  166. }
  167. int x = x0+pos[i]*4*sz;
  168. int y = y0;
  169. if(isnum) //FIXME: to avoid problems with superposition of characters, all previous digits should be drawn prior to current ones
  170. draw_char('8', x-dx, y-dy, sz, sz, bkcolor); //clear previous digit
  171. else
  172. draw_char(timedigits[i], x-dx, y-dy, sz, sz, bkcolor); //clear previous symbol
  173. //if(timedigits[i]==':')
  174. // color = timedigits[9]<'5' ? 0xFFC0C0C0 : bkcolor;
  175. draw_char(timedigits[i], x, y, sz, sz, color);
  176. }
  177. const int xlimit = FRAME_WIDTH-(pos[DIGITS]*4+7)*sz;
  178. const int ylimit = FRAME_HEIGHT-sz*(14+1);
  179. #if DIGITS == 7
  180. int x0_new = ((uint32_t)ttarget*37*41) % xlimit; //random like
  181. int y0_new = ((uint32_t)ttarget*43*67) % ylimit; //random like
  182. dx = x0_new-x0;
  183. dy = y0_new-y0;
  184. #else
  185. //bouncing
  186. if(x0 >= xlimit)
  187. dx = -1;
  188. if(x0 <= 0)
  189. dx = 1;
  190. if(y0 >= ylimit)
  191. dy = -1;
  192. if(y0 <= 1*sz)
  193. dy = 1;
  194. #endif
  195. x0 += dx;
  196. y0 += dy;
  197. #ifdef FRAMEBUFFER_CACHE_HANDLING
  198. flush_l2_cache(); //corrects screen pixels
  199. flush_cpu_dcache();
  200. #endif
  201. }
  202. }
  203. unsigned draw_shape(void)
  204. {
  205. unsigned count;
  206. pix_t pix;
  207. int x0 = rand32() % FRAME_WIDTH;
  208. int x1 = rand32() % FRAME_WIDTH;
  209. int y0 = rand32() % FRAME_HEIGHT;
  210. int y1 = rand32() % FRAME_HEIGHT;
  211. pix.rgba = rand32();
  212. pix.a |= 0xFF; //0x80 for semi opaque
  213. pix.r = (pix.r * pix.a) >> 8;
  214. pix.g = (pix.g * pix.a) >> 8;
  215. pix.b = (pix.b * pix.a) >> 8;
  216. #if defined(CSR_ACCEL_RECTANGLE_FILL32_BASE) && defined(CSR_ACCEL_ELLIPSE_FILL32_BASE)
  217. if(rand32() & 1)
  218. count = accel_ellipse_fill(ellipse_regs, x0, y0, x1, y1, pix.rgba);
  219. else
  220. {
  221. #ifdef CSR_ACCEL_LINE32_BASE
  222. count = accel_rectangle(line_regs, x0, y0, x1, y1, pix.rgba^0xFFFFFF);
  223. count += accel_line(line_regs, x0, y0, x1, y1, pix.rgba^0xFFFFFF);
  224. count += accel_line(line_regs, x1, y0, x0, y1, pix.rgba^0xFFFFFF);
  225. #else //FIXME: there are pixel errors if using both rectangles and lines (maybe when they overlap)
  226. count = accel_rectangle_fill(rectangle_regs, x0, y0, x1, y1, pix.rgba);
  227. #endif
  228. }
  229. #else
  230. #ifdef CSR_ACCEL_ELLIPSE_FILL32_BASE
  231. count = accel_ellipse_fill(ellipse_regs, x0, y0, x1, y1, pix.rgba);
  232. #endif
  233. #ifdef CSR_ACCEL_LINE32_BASE
  234. count = accel_rectangle(line_regs, x0, y0, x1, y1, pix.rgba^0xFFFFFF);
  235. count += accel_line(line_regs, x0, y0, x1, y1, pix.rgba^0xFFFFFF);
  236. count += accel_line(line_regs, x1, y0, x0, y1, pix.rgba^0xFFFFFF);
  237. #else
  238. count = accel_rectangle_fill(rectangle_regs, x0, y0, x1, y1, pix.rgba);
  239. #endif
  240. #endif
  241. //printf("count %d\n", count);
  242. return count;
  243. }
  244. void draw_shapes(int pixcount)
  245. {
  246. srand32(RAND_SEED); //reset pseudorandom function generator
  247. if(0)
  248. accel_rectangle_fill(rectangle_regs, 0, 0, FRAME_WIDTH, FRAME_HEIGHT, 0x404040); //avoids cache issues
  249. else
  250. {
  251. memset((void*)VIDEO_FRAMEBUFFER_BASE, 0x40, FRAME_HEIGHT*FRAME_PITCH); //set background to solid color
  252. #ifdef FRAMEBUFFER_CACHE_HANDLING
  253. flush_l2_cache(); //flush cache to correct issues, if not the test fails!
  254. //flush_cpu_dcache(); //seems optional
  255. #endif
  256. }
  257. uint64_t start = higres_ticks();
  258. uint64_t c = 0;
  259. while(c < pixcount)
  260. {
  261. uint32_t ops = draw_shape();
  262. c += ops;
  263. }
  264. int64_t dt = higres_ticks() - start;
  265. int elapsed = dt*1000000/higres_ticks_freq();
  266. int opspersec = c*higres_ticks_freq()/dt;
  267. printf("elapsed %d us, ops/s: %d (%d FPS @%dx%d)\n",
  268. elapsed, opspersec, opspersec/(FRAME_WIDTH*FRAME_HEIGHT), FRAME_WIDTH, FRAME_HEIGHT);
  269. }
  270. //test ---------------------------------------------------------------------------------------------
  271. int drawing_test(void)
  272. {
  273. pix_t *fb_tmp = (pix_t*) malloc(FRAME_HEIGHT*FRAME_PITCH);
  274. assert(fb_tmp != NULL);
  275. int pixcount = higres_ticks_freq()/10; //FIXME: this is just to compensate slow simulations
  276. printf("\nCurrent test: draw %d pixels\n", pixcount);
  277. // disable acellerators
  278. rectangle_regs = NULL;
  279. ellipse_regs = NULL;
  280. line_regs = NULL;
  281. printf("Start software rendering\n");
  282. draw_shapes(pixcount);
  283. /*
  284. #ifdef FRAMEBUFFER_CACHE_HANDLING //seems optional since test passes anyways
  285. printf("flush caches and save data\n");
  286. flush_l2_cache();
  287. flush_cpu_dcache();
  288. #endif
  289. */
  290. memcpy(fb_tmp, (void*)VIDEO_FRAMEBUFFER_BASE, FRAME_HEIGHT*FRAME_PITCH);
  291. printf("Switch to hardware rendering\n");
  292. rectangle_regs = accel_rectangle_fill32_regs;
  293. ellipse_regs = accel_ellipse_fill32_regs;
  294. line_regs = accel_line32_regs;
  295. draw_shapes(pixcount);
  296. printf("Just waiting a bit to evaluate image...\n");
  297. uint64_t ttarget = higres_ticks()+higres_ticks_freq()*5;
  298. while(higres_ticks() < ttarget);
  299. int errors = 0;
  300. pix_t *src = fb_tmp, *dst = (pix_t*) VIDEO_FRAMEBUFFER_BASE;
  301. while((void*) dst < (void*)(VIDEO_FRAMEBUFFER_BASE + FRAME_HEIGHT*FRAME_PITCH))
  302. {
  303. int match = dst->rgba == src->rgba;
  304. dst->rgba = match ? 0x404040 : 0x000000FF;
  305. ++src;
  306. ++dst;
  307. errors += (match == 0);
  308. }
  309. #ifdef FRAMEBUFFER_CACHE_HANDLING
  310. flush_l2_cache(); //corrects screen pixels
  311. //flush_cpu_dcache(); //no effect on screen
  312. #endif
  313. free(fb_tmp);
  314. printf("Pixel errors: %d (screen should have no red pixels)\n", errors);
  315. return errors;
  316. }