types.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_API_TYPES
  24. #define DFPSR_API_TYPES
  25. #include <stdint.h>
  26. #include <memory>
  27. #include "../image/Color.h"
  28. #include "../math/IRect.h"
  29. #include "../base/text.h"
  30. #ifdef DFPSR_INTERNAL_ACCESS
  31. #define IMPL_ACCESS public
  32. #else
  33. #define IMPL_ACCESS protected
  34. #endif
  35. namespace dsr {
  36. enum class PackOrderIndex {
  37. RGBA, // Windows
  38. BGRA, // Ubuntu
  39. ARGB,
  40. ABGR
  41. };
  42. enum class Sampler {
  43. Nearest,
  44. Linear
  45. };
  46. enum class ReturnCode {
  47. Good,
  48. KeyNotFound,
  49. ParsingFailure
  50. };
  51. // A handle to a model.
  52. class ModelImpl;
  53. using Model = std::shared_ptr<ModelImpl>;
  54. // A handle to a multi-threaded rendering context.
  55. class RendererImpl;
  56. using Renderer = std::shared_ptr<RendererImpl>;
  57. // A handle to a window.
  58. // The Window wraps itself around native window backends to abstract away platform specific details.
  59. // It also makes it easy to load and use a graphical interface using the optional component system.
  60. class DsrWindow;
  61. using Window = std::shared_ptr<DsrWindow>;
  62. // A handle to a GUI component.
  63. // Components are an abstraction for graphical user interfaces, which might not always be powerful enough.
  64. // * If you're making something advanced that components cannot do,
  65. // you can also use draw calls and input events directly against the window without using Component.
  66. class VisualComponent;
  67. using Component = std::shared_ptr<VisualComponent>;
  68. // A handle to a GUI theme.
  69. // Themes describes the visual appearance of an interface.
  70. // By having more than one theme for your interface, you can let the user select one.
  71. class VisualThemeImpl;
  72. using VisualTheme = std::shared_ptr<VisualThemeImpl>;
  73. // A handle to a media machine.
  74. // Media machines can be used to generate, filter and analyze images.
  75. // Everything running in a media machine is guaranteed to be 100% deterministic to the last bit.
  76. // This reduces the amount of code where maintenance has to be performed during porting.
  77. // It also means that any use of float or double is forbidden.
  78. class VirtualMachine;
  79. struct MediaMachine : IMPL_ACCESS std::shared_ptr<VirtualMachine> {
  80. MediaMachine(); // Defaults to null
  81. IMPL_ACCESS:
  82. explicit MediaMachine(const std::shared_ptr<VirtualMachine>& machine);
  83. };
  84. // Images
  85. // Points to a buffer region holding at least height * stride bytes.
  86. // Each row contains:
  87. // * A number of visible pixels
  88. // * A number of unused bytes
  89. // New or cloned images have their stride aligned to 16-bytes
  90. // Stride is the number of bytes from the start of one row to the next
  91. // Sub-images have the same stride and buffer as their parent
  92. // Some unused pixels may be visible somewhere else
  93. // 8-bit unsigned integer grayscale image
  94. class ImageU8Impl;
  95. struct ImageU8 : IMPL_ACCESS std::shared_ptr<ImageU8Impl> {
  96. ImageU8(); // Defaults to null
  97. IMPL_ACCESS:
  98. explicit ImageU8(const std::shared_ptr<ImageU8Impl>& image);
  99. explicit ImageU8(const ImageU8Impl& image);
  100. };
  101. // Invariant:
  102. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  103. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  104. // * No other image can displays pixels from its padding
  105. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  106. struct AlignedImageU8 : public ImageU8 {
  107. AlignedImageU8() {} // Defaults to null
  108. IMPL_ACCESS:
  109. explicit AlignedImageU8(const std::shared_ptr<ImageU8Impl>& image) : ImageU8(image) {}
  110. explicit AlignedImageU8(const ImageU8Impl& image) : ImageU8(image) {}
  111. };
  112. // 16-bit unsigned integer grayscale image
  113. class ImageU16Impl;
  114. struct ImageU16 : IMPL_ACCESS std::shared_ptr<ImageU16Impl> {
  115. ImageU16(); // Defaults to null
  116. IMPL_ACCESS:
  117. explicit ImageU16(const std::shared_ptr<ImageU16Impl>& image);
  118. explicit ImageU16(const ImageU16Impl& image);
  119. };
  120. // Invariant:
  121. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  122. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  123. // * No other image can displays pixels from its padding
  124. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  125. struct AlignedImageU16 : public ImageU16 {
  126. AlignedImageU16() {} // Defaults to null
  127. IMPL_ACCESS:
  128. explicit AlignedImageU16(const std::shared_ptr<ImageU16Impl>& image) : ImageU16(image) {}
  129. explicit AlignedImageU16(const ImageU16Impl& image) : ImageU16(image) {}
  130. };
  131. // 32-bit floating-point grayscale image
  132. class ImageF32Impl;
  133. struct ImageF32 : IMPL_ACCESS std::shared_ptr<ImageF32Impl> {
  134. ImageF32(); // Defaults to null
  135. IMPL_ACCESS:
  136. explicit ImageF32(const std::shared_ptr<ImageF32Impl>& image);
  137. explicit ImageF32(const ImageF32Impl& image);
  138. };
  139. // Invariant:
  140. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  141. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  142. // * No other image can displays pixels from its padding
  143. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  144. struct AlignedImageF32 : public ImageF32 {
  145. AlignedImageF32() {} // Defaults to null
  146. IMPL_ACCESS:
  147. explicit AlignedImageF32(const std::shared_ptr<ImageF32Impl>& image) : ImageF32(image) {}
  148. explicit AlignedImageF32(const ImageF32Impl& image) : ImageF32(image) {}
  149. };
  150. // 4x8-bit unsigned integer RGBA color image
  151. class ImageRgbaU8Impl;
  152. struct ImageRgbaU8 : IMPL_ACCESS std::shared_ptr<ImageRgbaU8Impl> {
  153. ImageRgbaU8(); // Defaults to null
  154. IMPL_ACCESS:
  155. explicit ImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image);
  156. explicit ImageRgbaU8(const ImageRgbaU8Impl& image);
  157. };
  158. // Invariant:
  159. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  160. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  161. // * No other image can displays pixels from its padding
  162. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  163. struct AlignedImageRgbaU8 : public ImageRgbaU8 {
  164. AlignedImageRgbaU8() {} // Defaults to null
  165. IMPL_ACCESS:
  166. explicit AlignedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : ImageRgbaU8(image) {}
  167. explicit AlignedImageRgbaU8(const ImageRgbaU8Impl& image) : ImageRgbaU8(image) {}
  168. };
  169. // Invariant:
  170. // * Using the default RGBA pack order
  171. // This removes the need to implement filters for different pack orders when RGBA can be safely assumed
  172. // Just use AlignedImageRgbaU8 if channels don't have to be aligned
  173. struct OrderedImageRgbaU8 : public AlignedImageRgbaU8 {
  174. OrderedImageRgbaU8() {} // Defaults to null
  175. IMPL_ACCESS:
  176. explicit OrderedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : AlignedImageRgbaU8(image) {}
  177. explicit OrderedImageRgbaU8(const ImageRgbaU8Impl& image) : AlignedImageRgbaU8(image) {}
  178. };
  179. }
  180. #endif