DataSurfaceHelpers.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include <string.h>
  6. #include "2D.h"
  7. #include "DataSurfaceHelpers.h"
  8. #include "Logging.h"
  9. #include "mozilla/MathAlgorithms.h"
  10. #include "mozilla/PodOperations.h"
  11. #include "Tools.h"
  12. namespace mozilla {
  13. namespace gfx {
  14. already_AddRefed<DataSourceSurface>
  15. CreateDataSourceSurfaceFromData(const IntSize& aSize,
  16. SurfaceFormat aFormat,
  17. const uint8_t* aData,
  18. int32_t aDataStride)
  19. {
  20. RefPtr<DataSourceSurface> srcSurface =
  21. Factory::CreateWrappingDataSourceSurface(const_cast<uint8_t*>(aData),
  22. aDataStride,
  23. aSize,
  24. aFormat);
  25. RefPtr<DataSourceSurface> destSurface =
  26. Factory::CreateDataSourceSurface(aSize, aFormat, false);
  27. if (!srcSurface || !destSurface) {
  28. return nullptr;
  29. }
  30. if (CopyRect(srcSurface,
  31. destSurface,
  32. IntRect(IntPoint(), srcSurface->GetSize()),
  33. IntPoint())) {
  34. return destSurface.forget();
  35. }
  36. return nullptr;
  37. }
  38. already_AddRefed<DataSourceSurface>
  39. CreateDataSourceSurfaceWithStrideFromData(const IntSize &aSize,
  40. SurfaceFormat aFormat,
  41. int32_t aStride,
  42. const uint8_t* aData,
  43. int32_t aDataStride)
  44. {
  45. RefPtr<DataSourceSurface> srcSurface =
  46. Factory::CreateWrappingDataSourceSurface(const_cast<uint8_t*>(aData),
  47. aDataStride,
  48. aSize,
  49. aFormat);
  50. RefPtr<DataSourceSurface> destSurface =
  51. Factory::CreateDataSourceSurfaceWithStride(aSize, aFormat, aStride, false);
  52. if (!srcSurface || !destSurface) {
  53. return nullptr;
  54. }
  55. if (CopyRect(srcSurface,
  56. destSurface,
  57. IntRect(IntPoint(), srcSurface->GetSize()),
  58. IntPoint())) {
  59. return destSurface.forget();
  60. }
  61. return nullptr;
  62. }
  63. uint8_t*
  64. DataAtOffset(DataSourceSurface* aSurface,
  65. const DataSourceSurface::MappedSurface* aMap,
  66. IntPoint aPoint)
  67. {
  68. if (!SurfaceContainsPoint(aSurface, aPoint)) {
  69. MOZ_CRASH("GFX: sample position needs to be inside surface!");
  70. }
  71. MOZ_ASSERT(Factory::CheckSurfaceSize(aSurface->GetSize()),
  72. "surface size overflows - this should have been prevented when the surface was created");
  73. uint8_t* data = aMap->mData + aPoint.y * aMap->mStride +
  74. aPoint.x * BytesPerPixel(aSurface->GetFormat());
  75. if (data < aMap->mData) {
  76. MOZ_CRASH("GFX: out-of-range data access");
  77. }
  78. return data;
  79. }
  80. // This check is safe against integer overflow.
  81. bool
  82. SurfaceContainsPoint(SourceSurface* aSurface, const IntPoint& aPoint)
  83. {
  84. IntSize size = aSurface->GetSize();
  85. return aPoint.x >= 0 && aPoint.x < size.width &&
  86. aPoint.y >= 0 && aPoint.y < size.height;
  87. }
  88. void
  89. ConvertBGRXToBGRA(uint8_t* aData, const IntSize &aSize, const int32_t aStride)
  90. {
  91. int height = aSize.height, width = aSize.width * 4;
  92. for (int row = 0; row < height; ++row) {
  93. for (int column = 0; column < width; column += 4) {
  94. #ifdef IS_BIG_ENDIAN
  95. aData[column] = 0xFF;
  96. #else
  97. aData[column + 3] = 0xFF;
  98. #endif
  99. }
  100. aData += aStride;
  101. }
  102. }
  103. void
  104. CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, IntSize aSrcSize,
  105. int32_t aSrcStride, int32_t aBytesPerPixel)
  106. {
  107. MOZ_ASSERT(aBytesPerPixel > 0,
  108. "Negative stride for aDst not currently supported");
  109. MOZ_ASSERT(BufferSizeFromStrideAndHeight(aSrcStride, aSrcSize.height) > 0,
  110. "How did we end up with a surface with such a big buffer?");
  111. int packedStride = aSrcSize.width * aBytesPerPixel;
  112. if (aSrcStride == packedStride) {
  113. // aSrc is already packed, so we can copy with a single memcpy.
  114. memcpy(aDst, aSrc, packedStride * aSrcSize.height);
  115. } else {
  116. // memcpy one row at a time.
  117. for (int row = 0; row < aSrcSize.height; ++row) {
  118. memcpy(aDst, aSrc, packedStride);
  119. aSrc += aSrcStride;
  120. aDst += packedStride;
  121. }
  122. }
  123. }
  124. void
  125. CopyBGRXSurfaceDataToPackedBGRArray(uint8_t* aSrc, uint8_t* aDst,
  126. IntSize aSrcSize, int32_t aSrcStride)
  127. {
  128. int packedStride = aSrcSize.width * 3;
  129. uint8_t* srcPx = aSrc;
  130. uint8_t* dstPx = aDst;
  131. for (int row = 0; row < aSrcSize.height; ++row) {
  132. for (int col = 0; col < aSrcSize.width; ++col) {
  133. dstPx[0] = srcPx[0];
  134. dstPx[1] = srcPx[1];
  135. dstPx[2] = srcPx[2];
  136. // srcPx[3] (unused or alpha component) dropped on floor
  137. srcPx += 4;
  138. dstPx += 3;
  139. }
  140. srcPx = aSrc += aSrcStride;
  141. dstPx = aDst += packedStride;
  142. }
  143. }
  144. UniquePtr<uint8_t[]>
  145. SurfaceToPackedBGRA(DataSourceSurface *aSurface)
  146. {
  147. SurfaceFormat format = aSurface->GetFormat();
  148. if (format != SurfaceFormat::B8G8R8A8 && format != SurfaceFormat::B8G8R8X8) {
  149. return nullptr;
  150. }
  151. IntSize size = aSurface->GetSize();
  152. UniquePtr<uint8_t[]> imageBuffer(
  153. new (std::nothrow) uint8_t[size.width * size.height * sizeof(uint32_t)]);
  154. if (!imageBuffer) {
  155. return nullptr;
  156. }
  157. DataSourceSurface::MappedSurface map;
  158. if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
  159. return nullptr;
  160. }
  161. CopySurfaceDataToPackedArray(map.mData, imageBuffer.get(), size,
  162. map.mStride, 4 * sizeof(uint8_t));
  163. aSurface->Unmap();
  164. if (format == SurfaceFormat::B8G8R8X8) {
  165. // Convert BGRX to BGRA by setting a to 255.
  166. ConvertBGRXToBGRA(imageBuffer.get(), size, size.width * sizeof(uint32_t));
  167. }
  168. return imageBuffer;
  169. }
  170. uint8_t*
  171. SurfaceToPackedBGR(DataSourceSurface *aSurface)
  172. {
  173. SurfaceFormat format = aSurface->GetFormat();
  174. MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8, "Format not supported");
  175. if (format != SurfaceFormat::B8G8R8X8) {
  176. // To support B8G8R8A8 we'd need to un-pre-multiply alpha
  177. return nullptr;
  178. }
  179. IntSize size = aSurface->GetSize();
  180. uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * 3 * sizeof(uint8_t)];
  181. if (!imageBuffer) {
  182. return nullptr;
  183. }
  184. DataSourceSurface::MappedSurface map;
  185. if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
  186. delete [] imageBuffer;
  187. return nullptr;
  188. }
  189. CopyBGRXSurfaceDataToPackedBGRArray(map.mData, imageBuffer, size,
  190. map.mStride);
  191. aSurface->Unmap();
  192. return imageBuffer;
  193. }
  194. void
  195. ClearDataSourceSurface(DataSourceSurface *aSurface)
  196. {
  197. DataSourceSurface::MappedSurface map;
  198. if (!aSurface->Map(DataSourceSurface::MapType::WRITE, &map)) {
  199. MOZ_ASSERT(false, "Failed to map DataSourceSurface");
  200. return;
  201. }
  202. // We avoid writing into the gaps between the rows here since we can't be
  203. // sure that some drivers don't use those bytes.
  204. uint32_t width = aSurface->GetSize().width;
  205. uint32_t bytesPerRow = width * BytesPerPixel(aSurface->GetFormat());
  206. uint8_t* row = map.mData;
  207. // converting to size_t here because otherwise the temporaries can overflow
  208. // and we can end up with |end| being a bad address!
  209. uint8_t* end = row + size_t(map.mStride) * size_t(aSurface->GetSize().height);
  210. while (row != end) {
  211. memset(row, 0, bytesPerRow);
  212. row += map.mStride;
  213. }
  214. aSurface->Unmap();
  215. }
  216. size_t
  217. BufferSizeFromStrideAndHeight(int32_t aStride,
  218. int32_t aHeight,
  219. int32_t aExtraBytes)
  220. {
  221. if (MOZ_UNLIKELY(aHeight <= 0) || MOZ_UNLIKELY(aStride <= 0)) {
  222. return 0;
  223. }
  224. // We limit the length returned to values that can be represented by int32_t
  225. // because we don't want to allocate buffers any bigger than that. This
  226. // allows for a buffer size of over 2 GiB which is already rediculously
  227. // large and will make the process janky. (Note the choice of the signed type
  228. // is deliberate because we specifically don't want the returned value to
  229. // overflow if someone stores the buffer length in an int32_t variable.)
  230. CheckedInt32 requiredBytes =
  231. CheckedInt32(aStride) * CheckedInt32(aHeight) + CheckedInt32(aExtraBytes);
  232. if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
  233. gfxWarning() << "Buffer size too big; returning zero " << aStride << ", " << aHeight << ", " << aExtraBytes;
  234. return 0;
  235. }
  236. return requiredBytes.value();
  237. }
  238. size_t
  239. BufferSizeFromDimensions(int32_t aWidth,
  240. int32_t aHeight,
  241. int32_t aDepth,
  242. int32_t aExtraBytes)
  243. {
  244. if (MOZ_UNLIKELY(aHeight <= 0) ||
  245. MOZ_UNLIKELY(aWidth <= 0) ||
  246. MOZ_UNLIKELY(aDepth <= 0)) {
  247. return 0;
  248. }
  249. // Similar to BufferSizeFromStrideAndHeight, but with an extra parameter.
  250. CheckedInt32 requiredBytes = CheckedInt32(aWidth) * CheckedInt32(aHeight) * CheckedInt32(aDepth) + CheckedInt32(aExtraBytes);
  251. if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
  252. gfxWarning() << "Buffer size too big; returning zero " << aWidth << ", " << aHeight << ", " << aDepth << ", " << aExtraBytes;
  253. return 0;
  254. }
  255. return requiredBytes.value();
  256. }
  257. /**
  258. * aSrcRect: Rect relative to the aSrc surface
  259. * aDestPoint: Point inside aDest surface
  260. */
  261. bool
  262. CopyRect(DataSourceSurface* aSrc, DataSourceSurface* aDest,
  263. IntRect aSrcRect, IntPoint aDestPoint)
  264. {
  265. if (aSrcRect.Overflows() ||
  266. IntRect(aDestPoint, aSrcRect.Size()).Overflows()) {
  267. MOZ_CRASH("GFX: we should never be getting invalid rects at this point");
  268. }
  269. MOZ_RELEASE_ASSERT(aSrc->GetFormat() == aDest->GetFormat(),
  270. "GFX: different surface formats");
  271. MOZ_RELEASE_ASSERT(IntRect(IntPoint(), aSrc->GetSize()).Contains(aSrcRect),
  272. "GFX: source rect too big for source surface");
  273. MOZ_RELEASE_ASSERT(IntRect(IntPoint(), aDest->GetSize()).Contains(IntRect(aDestPoint, aSrcRect.Size())),
  274. "GFX: dest surface too small");
  275. if (aSrcRect.IsEmpty()) {
  276. return false;
  277. }
  278. DataSourceSurface::ScopedMap srcMap(aSrc, DataSourceSurface::READ);
  279. DataSourceSurface::ScopedMap destMap(aDest, DataSourceSurface::WRITE);
  280. if (MOZ2D_WARN_IF(!srcMap.IsMapped() || !destMap.IsMapped())) {
  281. return false;
  282. }
  283. uint8_t* sourceData = DataAtOffset(aSrc, srcMap.GetMappedSurface(), aSrcRect.TopLeft());
  284. uint32_t sourceStride = srcMap.GetStride();
  285. uint8_t* destData = DataAtOffset(aDest, destMap.GetMappedSurface(), aDestPoint);
  286. uint32_t destStride = destMap.GetStride();
  287. if (BytesPerPixel(aSrc->GetFormat()) == 4) {
  288. for (int32_t y = 0; y < aSrcRect.height; y++) {
  289. PodCopy((int32_t*)destData, (int32_t*)sourceData, aSrcRect.width);
  290. sourceData += sourceStride;
  291. destData += destStride;
  292. }
  293. } else if (BytesPerPixel(aSrc->GetFormat()) == 1) {
  294. for (int32_t y = 0; y < aSrcRect.height; y++) {
  295. PodCopy(destData, sourceData, aSrcRect.width);
  296. sourceData += sourceStride;
  297. destData += destStride;
  298. }
  299. }
  300. return true;
  301. }
  302. already_AddRefed<DataSourceSurface>
  303. CreateDataSourceSurfaceByCloning(DataSourceSurface* aSource)
  304. {
  305. RefPtr<DataSourceSurface> copy =
  306. Factory::CreateDataSourceSurface(aSource->GetSize(), aSource->GetFormat(), true);
  307. if (copy) {
  308. CopyRect(aSource, copy, IntRect(IntPoint(), aSource->GetSize()), IntPoint());
  309. }
  310. return copy.forget();
  311. }
  312. } // namespace gfx
  313. } // namespace mozilla