types.h 8.1 KB

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