framebuffer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Framebuffer device helper routines
  3. *
  4. * Copyright (c) 2009 CodeSourcery
  5. * Written by Paul Brook <paul@codesourcery.com>
  6. *
  7. * This code is licensed under the GNU GPLv2.
  8. */
  9. /* TODO:
  10. - Do something similar for framebuffers with local ram
  11. - Handle rotation here instead of hacking dest_pitch
  12. - Use common pixel conversion routines instead of per-device drawfn
  13. - Remove all DisplayState knowledge from devices.
  14. */
  15. #include "hw.h"
  16. #include "console.h"
  17. #include "framebuffer.h"
  18. /* Render an image from a shared memory framebuffer. */
  19. void framebuffer_update_display(
  20. DisplayState *ds,
  21. target_phys_addr_t base,
  22. int cols, /* Width in pixels. */
  23. int rows, /* Leight in pixels. */
  24. int src_width, /* Length of source line, in bytes. */
  25. int dest_row_pitch, /* Bytes between adjacent horizontal output pixels. */
  26. int dest_col_pitch, /* Bytes between adjacent vertical output pixels. */
  27. int invalidate, /* nonzero to redraw the whole image. */
  28. drawfn fn,
  29. void *opaque,
  30. int *first_row, /* Input and output. */
  31. int *last_row /* Output only */)
  32. {
  33. target_phys_addr_t src_len;
  34. uint8_t *dest;
  35. uint8_t *src;
  36. uint8_t *src_base;
  37. int first, last = 0;
  38. int dirty;
  39. int i;
  40. ram_addr_t addr;
  41. ram_addr_t pd;
  42. ram_addr_t pd2;
  43. i = *first_row;
  44. *first_row = -1;
  45. src_len = src_width * rows;
  46. cpu_physical_sync_dirty_bitmap(base, base + src_len);
  47. pd = cpu_get_physical_page_desc(base);
  48. pd2 = cpu_get_physical_page_desc(base + src_len - 1);
  49. /* We should reall check that this is a continuous ram region.
  50. Instead we just check that the first and last pages are
  51. both ram, and the right distance apart. */
  52. if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM
  53. || (pd2 & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
  54. return;
  55. }
  56. pd = (pd & TARGET_PAGE_MASK) + (base & ~TARGET_PAGE_MASK);
  57. if (((pd + src_len - 1) & TARGET_PAGE_MASK) != (pd2 & TARGET_PAGE_MASK)) {
  58. return;
  59. }
  60. src_base = cpu_physical_memory_map(base, &src_len, 0);
  61. /* If we can't map the framebuffer then bail. We could try harder,
  62. but it's not really worth it as dirty flag tracking will probably
  63. already have failed above. */
  64. if (!src_base)
  65. return;
  66. if (src_len != src_width * rows) {
  67. cpu_physical_memory_unmap(src_base, src_len, 0, 0);
  68. return;
  69. }
  70. src = src_base;
  71. dest = ds_get_data(ds);
  72. if (dest_col_pitch < 0)
  73. dest -= dest_col_pitch * (cols - 1);
  74. if (dest_row_pitch < 0) {
  75. dest -= dest_row_pitch * (rows - 1);
  76. }
  77. first = -1;
  78. addr = pd;
  79. addr += i * src_width;
  80. src += i * src_width;
  81. dest += i * dest_row_pitch;
  82. for (; i < rows; i++) {
  83. target_phys_addr_t dirty_offset;
  84. dirty = 0;
  85. dirty_offset = 0;
  86. while (addr + dirty_offset < TARGET_PAGE_ALIGN(addr + src_width)) {
  87. dirty |= cpu_physical_memory_get_dirty(addr + dirty_offset,
  88. VGA_DIRTY_FLAG);
  89. dirty_offset += TARGET_PAGE_SIZE;
  90. }
  91. if (dirty || invalidate) {
  92. fn(opaque, dest, src, cols, dest_col_pitch);
  93. if (first == -1)
  94. first = i;
  95. last = i;
  96. }
  97. addr += src_width;
  98. src += src_width;
  99. dest += dest_row_pitch;
  100. }
  101. cpu_physical_memory_unmap(src_base, src_len, 0, 0);
  102. if (first < 0) {
  103. return;
  104. }
  105. cpu_physical_memory_reset_dirty(pd, pd + src_len, VGA_DIRTY_FLAG);
  106. *first_row = first;
  107. *last_row = last;
  108. return;
  109. }