sandbox_osd.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2018
  4. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  5. */
  6. #include <common.h>
  7. #include <display.h>
  8. #include <dm.h>
  9. #include <video_osd.h>
  10. #include "sandbox_osd.h"
  11. struct sandbox_osd_priv {
  12. uint width;
  13. uint height;
  14. u16 *buf;
  15. };
  16. static const struct udevice_id sandbox_osd_ids[] = {
  17. { .compatible = "sandbox,sandbox_osd" },
  18. { }
  19. };
  20. inline u16 make_memval(u8 chr, u8 color)
  21. {
  22. return chr * 0x100 + color;
  23. }
  24. int sandbox_osd_get_info(struct udevice *dev, struct video_osd_info *info)
  25. {
  26. struct sandbox_osd_priv *priv = dev_get_priv(dev);
  27. info->width = priv->width;
  28. info->height = priv->height;
  29. info->major_version = 1;
  30. info->minor_version = 0;
  31. return 0;
  32. }
  33. int sandbox_osd_set_mem(struct udevice *dev, uint col, uint row, u8 *buf,
  34. size_t buflen, uint count)
  35. {
  36. struct sandbox_osd_priv *priv = dev_get_priv(dev);
  37. int pos;
  38. u8 *mem = (u8 *)priv->buf;
  39. int i;
  40. pos = 2 * (row * priv->width + col);
  41. if (pos >= 2 * (priv->width * priv->height))
  42. return -EINVAL;
  43. for (i = 0; i < count; i++)
  44. memcpy(mem + pos + (i * buflen), buf, buflen);
  45. return 0;
  46. }
  47. int _sandbox_osd_set_size(struct udevice *dev, uint col, uint row)
  48. {
  49. struct sandbox_osd_priv *priv = dev_get_priv(dev);
  50. int i;
  51. uint size;
  52. priv->width = col;
  53. priv->height = row;
  54. size = priv->width * priv->height;
  55. if (!priv->buf)
  56. priv->buf = calloc(size, sizeof(u16));
  57. else
  58. priv->buf = realloc(priv->buf, size * sizeof(u16));
  59. if (!priv->buf)
  60. return -ENOMEM;
  61. /* Fill OSD with black spaces */
  62. for (i = 0; i < size; i++)
  63. priv->buf[i] = make_memval(' ', 'k');
  64. return 0;
  65. }
  66. int sandbox_osd_set_size(struct udevice *dev, uint col, uint row)
  67. {
  68. return _sandbox_osd_set_size(dev, col, row);
  69. }
  70. int sandbox_osd_print(struct udevice *dev, uint col, uint row, ulong color,
  71. char *text)
  72. {
  73. struct sandbox_osd_priv *priv = dev_get_priv(dev);
  74. char cval;
  75. char *p;
  76. int pos;
  77. if (col >= priv->width || row >= priv->height)
  78. return -EINVAL;
  79. switch (color) {
  80. case COLOR_BLACK:
  81. cval = 'k';
  82. break;
  83. case COLOR_WHITE:
  84. cval = 'w';
  85. break;
  86. case COLOR_RED:
  87. cval = 'r';
  88. break;
  89. case COLOR_GREEN:
  90. cval = 'g';
  91. break;
  92. case COLOR_BLUE:
  93. cval = 'b';
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. p = text;
  99. pos = row * priv->width + col;
  100. while (*p)
  101. priv->buf[pos++] = make_memval(*(p++), cval);
  102. return 0;
  103. }
  104. int sandbox_osd_get_mem(struct udevice *dev, u8 *buf, size_t buflen)
  105. {
  106. struct sandbox_osd_priv *priv = dev_get_priv(dev);
  107. uint memsize = 2 * (priv->width * priv->height);
  108. if (buflen < memsize)
  109. return -EINVAL;
  110. memcpy(buf, priv->buf, memsize);
  111. return 0;
  112. }
  113. static const struct video_osd_ops sandbox_osd_ops = {
  114. .get_info = sandbox_osd_get_info,
  115. .set_mem = sandbox_osd_set_mem,
  116. .set_size = sandbox_osd_set_size,
  117. .print = sandbox_osd_print,
  118. };
  119. int sandbox_osd_probe(struct udevice *dev)
  120. {
  121. return _sandbox_osd_set_size(dev, 10, 10);
  122. }
  123. U_BOOT_DRIVER(sandbox_osd_drv) = {
  124. .name = "sandbox_osd_drv",
  125. .id = UCLASS_VIDEO_OSD,
  126. .ops = &sandbox_osd_ops,
  127. .of_match = sandbox_osd_ids,
  128. .probe = sandbox_osd_probe,
  129. .priv_auto_alloc_size = sizeof(struct sandbox_osd_priv),
  130. };