sandbox_osd.c 3.1 KB

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