Surface.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/Reference.h>
  5. #include <Jolt/Core/Color.h>
  6. #include <Jolt/Core/StringTools.h>
  7. /// Possible lock modes of a Surface
  8. enum class ESurfaceLockMode : uint
  9. {
  10. None = 0 << 0, ///< Not locked, cannot be used as a parameter
  11. Read = 1 << 0,
  12. Write = 2 << 0,
  13. ReadWrite = Read | Write,
  14. };
  15. /// Possible surface formats, most significant bit (MSB) first
  16. enum class ESurfaceFormat : uint
  17. {
  18. A4L4, ///< 4 bit alpha, 4 bit luminance (grayscale)
  19. L8, ///< 8 bit luminance (grayscale)
  20. A8, ///< 8 bit alpha
  21. A8L8, ///< 8 bit luminance and 8 bit alpha
  22. R5G6B5, ///< 16 bit RGB
  23. X1R5G5B5, ///< 16 bit RGB
  24. X4R4G4B4, ///< 16 bit RGB
  25. A1R5G5B5, ///< 16 bit RGBA
  26. A4R4G4B4, ///< 16 bit RGBA
  27. R8G8B8, ///< 24 bit RGB
  28. B8G8R8, ///< 24 bit BGR
  29. X8R8G8B8, ///< 32 bit RGB
  30. X8B8G8R8, ///< 32 bit RGB
  31. A8R8G8B8, ///< 32 bit RGBA
  32. A8B8G8R8, ///< 32 bit BGRA
  33. Invalid, ///< Invalid value
  34. Count = Invalid, ///< Number of pixel formats
  35. };
  36. /// Description of a surface format
  37. class FormatDescription
  38. {
  39. public:
  40. /// Constructor
  41. FormatDescription(const char *inFormatName, int inBitsPerPixel, int inNumberOfComponents, ESurfaceFormat inClosest8BitFormat, ESurfaceFormat inClosestAlphaFormat, uint32 inRedMask, uint32 inGreenMask, uint32 inBlueMask, uint32 inAlphaMask);
  42. /// General properties
  43. const string_view & GetFormatName() const { return mFormatName; }
  44. int GetBytesPerPixel() const { return (mBitsPerPixel + 7) >> 3; }
  45. int GetNumberOfComponents() const { return mNumberOfComponents; }
  46. ESurfaceFormat GetClosest8BitFormat() const { return mClosest8BitFormat; }
  47. ESurfaceFormat GetClosestAlphaFormat() const { return mClosestAlphaFormat; }
  48. /// Bitcounts for the various components of the image
  49. int GetBitsPerPixel() const { return mBitsPerPixel; }
  50. int GetRedBitsPerPixel() const { return CountBits(mRedMask); }
  51. int GetGreenBitsPerPixel() const { return CountBits(mGreenMask); }
  52. int GetBlueBitsPerPixel() const { return CountBits(mBlueMask); }
  53. int GetAlphaBitsPerPixel() const { return CountBits(mAlphaMask); }
  54. int GetComponentBitCount(int inComponent) const { return CountBits(GetComponentMask(inComponent)); }
  55. /// Bitmasks indicating the various components of the image
  56. uint32 GetRedMask() const { return mRedMask; }
  57. uint32 GetGreenMask() const { return mGreenMask; }
  58. uint32 GetBlueMask() const { return mBlueMask; }
  59. uint32 GetAlphaMask() const { return mAlphaMask; }
  60. uint32 GetComponentMask(int inComponent) const { return *(&mRedMask + inComponent); }
  61. /// Convert a single color
  62. uint32 Encode(ColorArg inColor) const;
  63. const Color Decode(uint32 inColor) const;
  64. private:
  65. string_view mFormatName; ///< User displayable String describing the format
  66. int mBitsPerPixel; ///< Number of bits per pixel
  67. int mNumberOfComponents; ///< Number of color components per pixel
  68. ESurfaceFormat mClosest8BitFormat; ///< Closest matching format that has 8 bit color components
  69. ESurfaceFormat mClosestAlphaFormat; ///< Closest matching format that has an alpha channel
  70. uint32 mRedMask; ///< Bitmasks indicating which bits are used by which color components
  71. uint32 mGreenMask;
  72. uint32 mBlueMask;
  73. uint32 mAlphaMask;
  74. };
  75. /// Get the description for a specific surface format
  76. const FormatDescription & GetFormatDescription(ESurfaceFormat inFormat);
  77. /// Class that contains an image in arbitrary format
  78. class Surface : public RefTarget<Surface>
  79. {
  80. public:
  81. /// Constructor
  82. Surface(int inWidth, int inHeight, ESurfaceFormat inFormat);
  83. virtual ~Surface();
  84. /// Type of the image data
  85. const FormatDescription & GetFormatDescription() const { return ::GetFormatDescription(mFormat); }
  86. const string_view & GetFormatName() const { return GetFormatDescription().GetFormatName(); }
  87. int GetBytesPerPixel() const { return GetFormatDescription().GetBytesPerPixel(); }
  88. int GetNumberOfComponents() const { return GetFormatDescription().GetNumberOfComponents(); }
  89. ESurfaceFormat GetClosest8BitFormat() const { return GetFormatDescription().GetClosest8BitFormat(); }
  90. int GetBitsPerPixel() const { return GetFormatDescription().GetBitsPerPixel(); }
  91. int GetRedBitsPerPixel() const { return GetFormatDescription().GetRedBitsPerPixel(); }
  92. int GetGreenBitsPerPixel() const { return GetFormatDescription().GetGreenBitsPerPixel(); }
  93. int GetBlueBitsPerPixel() const { return GetFormatDescription().GetBlueBitsPerPixel(); }
  94. int GetAlphaBitsPerPixel() const { return GetFormatDescription().GetAlphaBitsPerPixel(); }
  95. int GetComponentBitCount(int inComponent) const { return GetFormatDescription().GetComponentBitCount(inComponent); }
  96. uint32 GetRedMask() const { return GetFormatDescription().GetRedMask(); }
  97. uint32 GetGreenMask() const { return GetFormatDescription().GetGreenMask(); }
  98. uint32 GetBlueMask() const { return GetFormatDescription().GetBlueMask(); }
  99. uint32 GetAlphaMask() const { return GetFormatDescription().GetAlphaMask(); }
  100. uint32 GetComponentMask(int inComponent) const { return GetFormatDescription().GetComponentMask(inComponent); }
  101. /// Get properties of this surface
  102. inline ESurfaceFormat GetFormat() const { return mFormat; }
  103. inline int GetWidth() const { return mWidth; }
  104. inline int GetHeight() const { return mHeight; }
  105. /// Sets the image to a specific color
  106. void Clear(ColorArg inColor = Color::sBlack);
  107. /// Locking functions
  108. void Lock(ESurfaceLockMode inMode) const;
  109. void UnLock() const;
  110. /// Current lock state
  111. inline ESurfaceLockMode GetLockMode() const { return mLockMode; }
  112. inline bool IsLocked() const { return mLockMode != ESurfaceLockMode::None; }
  113. inline bool IsLockedForRead() const { return (uint(mLockMode) & uint(ESurfaceLockMode::Read)) != 0; }
  114. inline bool IsLockedForWrite() const { return (uint(mLockMode) & uint(ESurfaceLockMode::Write)) != 0; }
  115. inline bool IsLockedForReadWrite() const { return IsLockedForRead() && IsLockedForWrite(); }
  116. /// Access to the image data
  117. inline const uint8 * GetData() const { JPH_ASSERT(IsLockedForRead()); return mData; }
  118. inline uint8 * GetData() { JPH_ASSERT(IsLockedForWrite()); return mData; }
  119. inline int GetStride() const { JPH_ASSERT(IsLocked()); return mStride; }
  120. inline int GetLength() const { JPH_ASSERT(IsLocked()); return mLength; }
  121. /// Get start of a specific scanline
  122. inline const uint8 * GetScanLine(int inScanLine) const { JPH_ASSERT(inScanLine >= 0 && inScanLine < GetHeight()); return GetData() + inScanLine * GetStride(); }
  123. inline uint8 * GetScanLine(int inScanLine) { JPH_ASSERT(inScanLine >= 0 && inScanLine < GetHeight()); return GetData() + inScanLine * GetStride(); }
  124. protected:
  125. /// These functions must be overridden by the hardware buffer
  126. virtual void HardwareLock() const = 0;
  127. virtual void HardwareUnLock() const = 0;
  128. /// Data
  129. ESurfaceFormat mFormat; ///< Pixel format of the surface
  130. int mWidth; ///< Width of the image
  131. int mHeight; ///< Height of the image
  132. mutable int mLength; ///< Length in bytes of the image
  133. mutable ESurfaceLockMode mLockMode;
  134. mutable int mStride; ///< Width of one scanline in bytes
  135. mutable uint8 * mData; ///< Pointer to image data, starting at top-left of locked rectangle
  136. };
  137. /// Class that contains an image in arbitrary format, backed by normal memory (not device specific)
  138. class SoftwareSurface : public Surface
  139. {
  140. public:
  141. /// Constructor
  142. SoftwareSurface(int inWidth, int inHeight, ESurfaceFormat inFormat, int inStride = 0);
  143. virtual ~SoftwareSurface() override;
  144. protected:
  145. /// These functions must be overridden by the hardware buffer
  146. virtual void HardwareLock() const override;
  147. virtual void HardwareUnLock() const override;
  148. uint8 * mPixelData;
  149. int mPixelStride;
  150. int mPixelLength;
  151. };