DataSurfaceHelpers.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef _MOZILLA_GFX_DATASURFACEHELPERS_H
  6. #define _MOZILLA_GFX_DATASURFACEHELPERS_H
  7. #include "2D.h"
  8. #include "mozilla/UniquePtr.h"
  9. namespace mozilla {
  10. namespace gfx {
  11. /**
  12. * Create a DataSourceSurface and init the surface with the |aData|. The stride
  13. * of this source surface might be different from the input data's |aDataStride|.
  14. * System will try to use the optimal one.
  15. */
  16. already_AddRefed<DataSourceSurface>
  17. CreateDataSourceSurfaceFromData(const IntSize& aSize,
  18. SurfaceFormat aFormat,
  19. const uint8_t* aData,
  20. int32_t aDataStride);
  21. /**
  22. * Similar to CreateDataSourceSurfaceFromData(), but could setup the stride for
  23. * this surface.
  24. */
  25. already_AddRefed<DataSourceSurface>
  26. CreateDataSourceSurfaceWithStrideFromData(const IntSize &aSize,
  27. SurfaceFormat aFormat,
  28. int32_t aStride,
  29. const uint8_t* aData,
  30. int32_t aDataStride);
  31. void
  32. ConvertBGRXToBGRA(uint8_t* aData, const IntSize &aSize, const int32_t aStride);
  33. /**
  34. * Copy the pixel data from aSrc and pack it into aDst. aSrcSize, aSrcStride
  35. * and aBytesPerPixel give the size, stride and bytes per pixel for aSrc's
  36. * surface. Callers are responsible for making sure that aDst is big enough to
  37. * contain |aSrcSize.width * aSrcSize.height * aBytesPerPixel| bytes.
  38. */
  39. void
  40. CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, IntSize aSrcSize,
  41. int32_t aSrcStride, int32_t aBytesPerPixel);
  42. /**
  43. * Convert aSurface to a packed buffer in BGRA format.
  44. */
  45. UniquePtr<uint8_t[]>
  46. SurfaceToPackedBGRA(DataSourceSurface *aSurface);
  47. /**
  48. * Convert aSurface to a packed buffer in BGR format. The pixel data is
  49. * returned in a buffer allocated with new uint8_t[]. The caller then has
  50. * ownership of the buffer and is responsible for delete[]'ing it.
  51. *
  52. * This function is currently only intended for use with surfaces of format
  53. * SurfaceFormat::B8G8R8X8 since the X components of the pixel data (if any)
  54. * are simply dropped (no attempt is made to un-pre-multiply alpha from the
  55. * color components).
  56. */
  57. uint8_t*
  58. SurfaceToPackedBGR(DataSourceSurface *aSurface);
  59. /**
  60. * Clears all the bytes in a DataSourceSurface's data array to zero (so to
  61. * transparent black for SurfaceFormat::B8G8R8A8, for example).
  62. * Note that DataSourceSurfaces can be initialized to zero, which is
  63. * more efficient than zeroing the surface after initialization.
  64. */
  65. void
  66. ClearDataSourceSurface(DataSourceSurface *aSurface);
  67. /**
  68. * Multiplies aStride and aHeight and makes sure the result is limited to
  69. * something sane. To keep things consistent, this should always be used
  70. * wherever we allocate a buffer based on surface stride and height.
  71. *
  72. * @param aExtra Optional argument to specify an additional number of trailing
  73. * bytes (useful for creating intermediate surfaces for filters, for
  74. * example).
  75. *
  76. * @return The result of the multiplication if it is acceptable, or else zero.
  77. */
  78. size_t
  79. BufferSizeFromStrideAndHeight(int32_t aStride,
  80. int32_t aHeight,
  81. int32_t aExtraBytes = 0);
  82. /**
  83. * Multiplies aWidth, aHeight, aDepth and makes sure the result is limited to
  84. * something sane. To keep things consistent, this should always be used
  85. * wherever we allocate a buffer based on surface dimensions.
  86. *
  87. * @param aExtra Optional argument to specify an additional number of trailing
  88. * bytes (useful for creating intermediate surfaces for filters, for
  89. * example).
  90. *
  91. * @return The result of the multiplication if it is acceptable, or else zero.
  92. */
  93. size_t
  94. BufferSizeFromDimensions(int32_t aWidth,
  95. int32_t aHeight,
  96. int32_t aDepth,
  97. int32_t aExtraBytes = 0);
  98. /**
  99. * Copy aSrcRect from aSrc to aDest starting at aDestPoint.
  100. * @returns false if the copy is not successful or the aSrc's size is empty.
  101. */
  102. bool
  103. CopyRect(DataSourceSurface* aSrc, DataSourceSurface* aDest,
  104. IntRect aSrcRect, IntPoint aDestPoint);
  105. /**
  106. * Create a non aliasing copy of aSource. This creates a new DataSourceSurface
  107. * using the factory and copies the bits.
  108. *
  109. * @return a dss allocated by Factory that contains a copy a aSource.
  110. */
  111. already_AddRefed<DataSourceSurface>
  112. CreateDataSourceSurfaceByCloning(DataSourceSurface* aSource);
  113. /**
  114. * Return the byte at aPoint.
  115. */
  116. uint8_t*
  117. DataAtOffset(DataSourceSurface* aSurface,
  118. const DataSourceSurface::MappedSurface* aMap,
  119. IntPoint aPoint);
  120. /**
  121. * Check if aPoint is contained by the surface.
  122. *
  123. * @returns true if and only if aPoint is inside the surface.
  124. */
  125. bool
  126. SurfaceContainsPoint(SourceSurface* aSurface, const IntPoint& aPoint);
  127. } // namespace gfx
  128. } // namespace mozilla
  129. #endif // _MOZILLA_GFX_DATASURFACEHELPERS_H