framebuffer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. /* TODO:
  13. - Do something similar for framebuffers with local ram
  14. - Handle rotation here instead of hacking dest_pitch
  15. - Use common pixel conversion routines instead of per-device drawfn
  16. - Remove all DisplayState knowledge from devices.
  17. */
  18. #include "hw.h"
  19. #include "console.h"
  20. #include "framebuffer.h"
  21. /* Render an image from a shared memory framebuffer. */
  22. void framebuffer_update_display(
  23. DisplayState *ds,
  24. MemoryRegion *address_space,
  25. target_phys_addr_t base,
  26. int cols, /* Width in pixels. */
  27. int rows, /* Height in pixels. */
  28. int src_width, /* Length of source line, in bytes. */
  29. int dest_row_pitch, /* Bytes between adjacent horizontal output pixels. */
  30. int dest_col_pitch, /* Bytes between adjacent vertical output pixels. */
  31. int invalidate, /* nonzero to redraw the whole image. */
  32. drawfn fn,
  33. void *opaque,
  34. int *first_row, /* Input and output. */
  35. int *last_row /* Output only */)
  36. {
  37. target_phys_addr_t src_len;
  38. uint8_t *dest;
  39. uint8_t *src;
  40. uint8_t *src_base;
  41. int first, last = 0;
  42. int dirty;
  43. int i;
  44. ram_addr_t addr;
  45. MemoryRegionSection mem_section;
  46. MemoryRegion *mem;
  47. i = *first_row;
  48. *first_row = -1;
  49. src_len = src_width * rows;
  50. mem_section = memory_region_find(address_space, base, src_len);
  51. if (mem_section.size != src_len || !memory_region_is_ram(mem_section.mr)) {
  52. return;
  53. }
  54. mem = mem_section.mr;
  55. assert(mem);
  56. assert(mem_section.offset_within_address_space == base);
  57. memory_region_sync_dirty_bitmap(mem);
  58. src_base = cpu_physical_memory_map(base, &src_len, 0);
  59. /* If we can't map the framebuffer then bail. We could try harder,
  60. but it's not really worth it as dirty flag tracking will probably
  61. already have failed above. */
  62. if (!src_base)
  63. return;
  64. if (src_len != src_width * rows) {
  65. cpu_physical_memory_unmap(src_base, src_len, 0, 0);
  66. return;
  67. }
  68. src = src_base;
  69. dest = ds_get_data(ds);
  70. if (dest_col_pitch < 0)
  71. dest -= dest_col_pitch * (cols - 1);
  72. if (dest_row_pitch < 0) {
  73. dest -= dest_row_pitch * (rows - 1);
  74. }
  75. first = -1;
  76. addr = mem_section.offset_within_region;
  77. addr += i * src_width;
  78. src += i * src_width;
  79. dest += i * dest_row_pitch;
  80. for (; i < rows; i++) {
  81. dirty = memory_region_get_dirty(mem, addr, src_width,
  82. DIRTY_MEMORY_VGA);
  83. if (dirty || invalidate) {
  84. fn(opaque, dest, src, cols, dest_col_pitch);
  85. if (first == -1)
  86. first = i;
  87. last = i;
  88. }
  89. addr += src_width;
  90. src += src_width;
  91. dest += dest_row_pitch;
  92. }
  93. cpu_physical_memory_unmap(src_base, src_len, 0, 0);
  94. if (first < 0) {
  95. return;
  96. }
  97. memory_region_reset_dirty(mem, mem_section.offset_within_region, src_len,
  98. DIRTY_MEMORY_VGA);
  99. *first_row = first;
  100. *last_row = last;
  101. }