lfb.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "uart.h"
  26. #include "mbox.h"
  27. #include "homer.h"
  28. #include "delays.h"
  29. unsigned int width, height, pitch, isrgb; /* dimensions and channel order */
  30. unsigned char *lfb; /* raw frame buffer address */
  31. /**
  32. * Set screen resolution to 1024x768
  33. */
  34. void lfb_init()
  35. {
  36. /* newer qemu segfaults if we don't wait here a bit */
  37. wait_msec(100000);
  38. mbox[0] = 35*4;
  39. mbox[1] = MBOX_REQUEST;
  40. mbox[2] = 0x48003; //set phy wh
  41. mbox[3] = 8;
  42. mbox[4] = 8;
  43. mbox[5] = 1024; //FrameBufferInfo.width
  44. mbox[6] = 768; //FrameBufferInfo.height
  45. mbox[7] = 0x48004; //set virt wh
  46. mbox[8] = 8;
  47. mbox[9] = 8;
  48. mbox[10] = 1024; //FrameBufferInfo.virtual_width
  49. mbox[11] = 768; //FrameBufferInfo.virtual_height
  50. mbox[12] = 0x48009; //set virt offset
  51. mbox[13] = 8;
  52. mbox[14] = 8;
  53. mbox[15] = 0; //FrameBufferInfo.x_offset
  54. mbox[16] = 0; //FrameBufferInfo.y.offset
  55. mbox[17] = 0x48005; //set depth
  56. mbox[18] = 4;
  57. mbox[19] = 4;
  58. mbox[20] = 32; //FrameBufferInfo.depth
  59. mbox[21] = 0x48006; //set pixel order
  60. mbox[22] = 4;
  61. mbox[23] = 4;
  62. mbox[24] = 1; //RGB, not BGR preferably
  63. mbox[25] = 0x40001; //get framebuffer, gets alignment on request
  64. mbox[26] = 8;
  65. mbox[27] = 8;
  66. mbox[28] = 4096; //FrameBufferInfo.pointer
  67. mbox[29] = 0; //FrameBufferInfo.size
  68. mbox[30] = 0x40008; //get pitch
  69. mbox[31] = 4;
  70. mbox[32] = 4;
  71. mbox[33] = 0; //FrameBufferInfo.pitch
  72. mbox[34] = MBOX_TAG_LAST;
  73. //this might not return exactly what we asked for, could be
  74. //the closest supported resolution instead
  75. if(mbox_call(MBOX_CH_PROP) && mbox[20]==32 && mbox[28]!=0) {
  76. mbox[28]&=0x3FFFFFFF; //convert GPU address to ARM address
  77. width=mbox[5]; //get actual physical width
  78. height=mbox[6]; //get actual physical height
  79. pitch=mbox[33]; //get number of bytes per line
  80. isrgb=mbox[24]; //get the actual channel order
  81. lfb=(void*)((unsigned long)mbox[28]);
  82. } else {
  83. uart_puts("Unable to set screen resolution to 1024x768x32\n");
  84. }
  85. }
  86. /**
  87. * Show a picture
  88. */
  89. void lfb_showpicture()
  90. {
  91. int x,y;
  92. unsigned char *ptr=lfb;
  93. char *data=homer_data, pixel[4];
  94. ptr += (height-homer_height)/2*pitch + (width-homer_width)*2;
  95. for(y=0;y<homer_height;y++) {
  96. for(x=0;x<homer_width;x++) {
  97. HEADER_PIXEL(data, pixel);
  98. // the image is in RGB. So if we have an RGB framebuffer, we can copy the pixels
  99. // directly, but for BGR we must swap R (pixel[0]) and B (pixel[2]) channels.
  100. *((unsigned int*)ptr)=isrgb ? *((unsigned int *)&pixel) : (unsigned int)(pixel[0]<<16 | pixel[1]<<8 | pixel[2]);
  101. ptr+=4;
  102. }
  103. ptr+=pitch-homer_width*4;
  104. }
  105. }