types.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Define DFPSR_INTERNAL_ACCESS before any include to get internal access to exposed types
  31. #ifdef DFPSR_INTERNAL_ACCESS
  32. #define IMPL_ACCESS public
  33. #else
  34. #define IMPL_ACCESS protected
  35. #endif
  36. namespace dsr {
  37. enum class PackOrderIndex {
  38. RGBA, // Windows
  39. BGRA, // Ubuntu
  40. ARGB,
  41. ABGR
  42. };
  43. enum class Sampler {
  44. Nearest,
  45. Linear
  46. };
  47. enum class ReturnCode {
  48. Good,
  49. KeyNotFound,
  50. ParsingFailure
  51. };
  52. // A handle to a model.
  53. class ModelImpl;
  54. using Model = std::shared_ptr<ModelImpl>;
  55. // A handle to a multi-threaded rendering context.
  56. class RendererImpl;
  57. using Renderer = std::shared_ptr<RendererImpl>;
  58. // A handle to a window.
  59. // The Window wraps itself around native window backends to abstract away platform specific details.
  60. // It also makes it easy to load and use a graphical interface using the optional component system.
  61. class DsrWindow;
  62. using Window = std::shared_ptr<DsrWindow>;
  63. // A handle to a GUI component.
  64. // Components are an abstraction for graphical user interfaces, which might not always be powerful enough.
  65. // * If you're making something advanced that components cannot do,
  66. // you can also use draw calls and input events directly against the window without using Component.
  67. class VisualComponent;
  68. using Component = std::shared_ptr<VisualComponent>;
  69. // A handle to a GUI theme.
  70. // Themes describes the visual appearance of an interface.
  71. // By having more than one theme for your interface, you can let the user select one.
  72. class VisualThemeImpl;
  73. using VisualTheme = std::shared_ptr<VisualThemeImpl>;
  74. // A handle to a raster font
  75. class RasterFontImpl;
  76. using RasterFont = std::shared_ptr<RasterFontImpl>;
  77. // A handle to a media machine.
  78. // Media machines can be used to generate, filter and analyze images.
  79. // Everything running in a media machine is guaranteed to be 100% deterministic to the last bit.
  80. // This reduces the amount of code where maintenance has to be performed during porting.
  81. // It also means that any use of float or double is forbidden.
  82. struct VirtualMachine;
  83. struct MediaMachine : IMPL_ACCESS std::shared_ptr<VirtualMachine> {
  84. MediaMachine(); // Defaults to null
  85. IMPL_ACCESS:
  86. explicit MediaMachine(const std::shared_ptr<VirtualMachine>& machine);
  87. };
  88. // Images
  89. // Points to a buffer region holding at least height * stride bytes.
  90. // Each row contains:
  91. // * A number of visible pixels
  92. // * A number of unused bytes
  93. // New or cloned images have their stride aligned to 16-bytes
  94. // Stride is the number of bytes from the start of one row to the next
  95. // Sub-images have the same stride and buffer as their parent
  96. // Some unused pixels may be visible somewhere else
  97. // 8-bit unsigned integer grayscale image
  98. class ImageU8Impl;
  99. struct ImageU8 : IMPL_ACCESS std::shared_ptr<ImageU8Impl> {
  100. ImageU8(); // Defaults to null
  101. IMPL_ACCESS:
  102. explicit ImageU8(const std::shared_ptr<ImageU8Impl>& image);
  103. explicit ImageU8(const ImageU8Impl& image);
  104. };
  105. // Invariant:
  106. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  107. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  108. // * No other image can displays pixels from its padding
  109. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  110. struct AlignedImageU8 : public ImageU8 {
  111. AlignedImageU8() {} // Defaults to null
  112. IMPL_ACCESS:
  113. explicit AlignedImageU8(const std::shared_ptr<ImageU8Impl>& image) : ImageU8(image) {}
  114. explicit AlignedImageU8(const ImageU8Impl& image) : ImageU8(image) {}
  115. };
  116. // 16-bit unsigned integer grayscale image
  117. class ImageU16Impl;
  118. struct ImageU16 : IMPL_ACCESS std::shared_ptr<ImageU16Impl> {
  119. ImageU16(); // Defaults to null
  120. IMPL_ACCESS:
  121. explicit ImageU16(const std::shared_ptr<ImageU16Impl>& image);
  122. explicit ImageU16(const ImageU16Impl& image);
  123. };
  124. // Invariant:
  125. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 16 pixels)
  126. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  127. // * No other image can displays pixels from its padding
  128. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  129. struct AlignedImageU16 : public ImageU16 {
  130. AlignedImageU16() {} // Defaults to null
  131. IMPL_ACCESS:
  132. explicit AlignedImageU16(const std::shared_ptr<ImageU16Impl>& image) : ImageU16(image) {}
  133. explicit AlignedImageU16(const ImageU16Impl& image) : ImageU16(image) {}
  134. };
  135. // 32-bit floating-point grayscale image
  136. class ImageF32Impl;
  137. struct ImageF32 : IMPL_ACCESS std::shared_ptr<ImageF32Impl> {
  138. ImageF32(); // Defaults to null
  139. IMPL_ACCESS:
  140. explicit ImageF32(const std::shared_ptr<ImageF32Impl>& image);
  141. explicit ImageF32(const ImageF32Impl& image);
  142. };
  143. // Invariant:
  144. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  145. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  146. // * No other image can displays pixels from its padding
  147. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  148. struct AlignedImageF32 : public ImageF32 {
  149. AlignedImageF32() {} // Defaults to null
  150. IMPL_ACCESS:
  151. explicit AlignedImageF32(const std::shared_ptr<ImageF32Impl>& image) : ImageF32(image) {}
  152. explicit AlignedImageF32(const ImageF32Impl& image) : ImageF32(image) {}
  153. };
  154. // 4x8-bit unsigned integer RGBA color image
  155. class ImageRgbaU8Impl;
  156. struct ImageRgbaU8 : IMPL_ACCESS std::shared_ptr<ImageRgbaU8Impl> {
  157. ImageRgbaU8(); // Defaults to null
  158. IMPL_ACCESS:
  159. explicit ImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image);
  160. explicit ImageRgbaU8(const ImageRgbaU8Impl& image);
  161. };
  162. // Invariant:
  163. // * Each row's start and stride is aligned with 16-bytes in memory (16-byte = 4 pixels)
  164. // This allow reading a full SIMD vector at each row's end without violating memory bounds
  165. // * No other image can displays pixels from its padding
  166. // This allow writing a full SIMD vector at each row's end without making visible changes outside of the bound
  167. struct AlignedImageRgbaU8 : public ImageRgbaU8 {
  168. AlignedImageRgbaU8() {} // Defaults to null
  169. IMPL_ACCESS:
  170. explicit AlignedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : ImageRgbaU8(image) {}
  171. explicit AlignedImageRgbaU8(const ImageRgbaU8Impl& image) : ImageRgbaU8(image) {}
  172. };
  173. // Invariant:
  174. // * Using the default RGBA pack order
  175. // This removes the need to implement filters for different pack orders when RGBA can be safely assumed
  176. // Just use AlignedImageRgbaU8 if channels don't have to be aligned
  177. struct OrderedImageRgbaU8 : public AlignedImageRgbaU8 {
  178. OrderedImageRgbaU8() {} // Defaults to null
  179. IMPL_ACCESS:
  180. explicit OrderedImageRgbaU8(const std::shared_ptr<ImageRgbaU8Impl>& image) : AlignedImageRgbaU8(image) {}
  181. explicit OrderedImageRgbaU8(const ImageRgbaU8Impl& image) : AlignedImageRgbaU8(image) {}
  182. };
  183. }
  184. #endif