WebGLTexelConversions.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. //cjh #include "WebGLContext.h"
  5. #include "WebGLTexelConversions.h"
  6. #include <memory.h>
  7. namespace mozilla {
  8. using namespace WebGLTexelConversions;
  9. namespace {
  10. /** @class WebGLImageConverter
  11. *
  12. * This class is just a helper to implement WebGLContext::ConvertImage below.
  13. *
  14. * Design comments:
  15. *
  16. * WebGLContext::ConvertImage has to handle hundreds of format conversion paths.
  17. * It is important to minimize executable code size here. Instead of passing around
  18. * a large number of function parameters hundreds of times, we create a
  19. * WebGLImageConverter object once, storing these parameters, and then we call
  20. * the run() method on it.
  21. */
  22. class WebGLImageConverter
  23. {
  24. const size_t mWidth, mHeight;
  25. const void* const mSrcStart;
  26. void* const mDstStart;
  27. const ptrdiff_t mSrcStride, mDstStride;
  28. bool mAlreadyRun;
  29. bool mSuccess;
  30. /*
  31. * Returns sizeof(texel)/sizeof(type). The point is that we will iterate over
  32. * texels with typed pointers and this value will tell us by how much we need
  33. * to increment these pointers to advance to the next texel.
  34. */
  35. template<WebGLTexelFormat Format>
  36. static size_t NumElementsPerTexelForFormat() {
  37. switch (Format) {
  38. case WebGLTexelFormat::A8:
  39. case WebGLTexelFormat::A16F:
  40. case WebGLTexelFormat::A32F:
  41. case WebGLTexelFormat::R8:
  42. case WebGLTexelFormat::R16F:
  43. case WebGLTexelFormat::R32F:
  44. case WebGLTexelFormat::RGB565:
  45. case WebGLTexelFormat::RGB11F11F10F:
  46. case WebGLTexelFormat::RGBA4444:
  47. case WebGLTexelFormat::RGBA5551:
  48. return 1;
  49. case WebGLTexelFormat::RA8:
  50. case WebGLTexelFormat::RA16F:
  51. case WebGLTexelFormat::RA32F:
  52. case WebGLTexelFormat::RG8:
  53. case WebGLTexelFormat::RG16F:
  54. case WebGLTexelFormat::RG32F:
  55. return 2;
  56. case WebGLTexelFormat::RGB8:
  57. case WebGLTexelFormat::RGB16F:
  58. case WebGLTexelFormat::RGB32F:
  59. return 3;
  60. case WebGLTexelFormat::RGBA8:
  61. case WebGLTexelFormat::RGBA16F:
  62. case WebGLTexelFormat::RGBA32F:
  63. case WebGLTexelFormat::BGRX8:
  64. case WebGLTexelFormat::BGRA8:
  65. return 4;
  66. default:
  67. MOZ_ASSERT(false, "Unknown texel format. Coding mistake?");
  68. return 0;
  69. }
  70. }
  71. /*
  72. * This is the completely format-specific templatized conversion function,
  73. * that will be instantiated hundreds of times for all different combinations.
  74. * It is important to avoid generating useless code here. In particular, many
  75. * instantiations of this function template will never be called, so we try
  76. * to return immediately in these cases to allow the compiler to avoid generating
  77. * useless code.
  78. */
  79. template<WebGLTexelFormat SrcFormat,
  80. WebGLTexelFormat DstFormat,
  81. WebGLTexelPremultiplicationOp PremultiplicationOp>
  82. void run()
  83. {
  84. // check for never-called cases. We early-return to allow the compiler
  85. // to avoid generating this code. It would be tempting to abort() instead,
  86. // as returning early does leave the destination surface with uninitialized
  87. // data, but that would not allow the compiler to avoid generating this code.
  88. // So instead, we return early, so Success() will return false, and the caller
  89. // must check that and abort in that case. See WebGLContext::ConvertImage.
  90. if (SrcFormat == DstFormat &&
  91. PremultiplicationOp == WebGLTexelPremultiplicationOp::None)
  92. {
  93. // Should have used a fast exit path earlier, rather than entering this function.
  94. // we explicitly return here to allow the compiler to avoid generating this code
  95. return;
  96. }
  97. // Only textures uploaded from DOM elements or ImageData can allow DstFormat != SrcFormat.
  98. // DOM elements can only give BGRA8, BGRX8, A8, RGB565 formats. See DOMElementToImageSurface.
  99. // ImageData is always RGBA8. So all other SrcFormat will always satisfy DstFormat==SrcFormat,
  100. // so we can avoid compiling the code for all the unreachable paths.
  101. //cjh const bool CanSrcFormatComeFromDOMElementOrImageData
  102. // = SrcFormat == WebGLTexelFormat::BGRA8 ||
  103. // SrcFormat == WebGLTexelFormat::BGRX8 ||
  104. // SrcFormat == WebGLTexelFormat::A8 ||
  105. // SrcFormat == WebGLTexelFormat::RGB565 ||
  106. // SrcFormat == WebGLTexelFormat::RGBA8;
  107. // if (!CanSrcFormatComeFromDOMElementOrImageData &&
  108. // SrcFormat != DstFormat)
  109. // {
  110. // return;
  111. // }
  112. //
  113. // // Likewise, only textures uploaded from DOM elements or ImageData can possibly have to be unpremultiplied.
  114. // if (!CanSrcFormatComeFromDOMElementOrImageData &&
  115. // PremultiplicationOp == WebGLTexelPremultiplicationOp::Unpremultiply)
  116. // {
  117. // return;
  118. // }
  119. // there is no point in premultiplication/unpremultiplication
  120. // in the following cases:
  121. // - the source format has no alpha
  122. // - the source format has no color
  123. // - the destination format has no color
  124. if (!HasAlpha(SrcFormat) ||
  125. !HasColor(SrcFormat) ||
  126. !HasColor(DstFormat))
  127. {
  128. if (PremultiplicationOp != WebGLTexelPremultiplicationOp::None)
  129. {
  130. return;
  131. }
  132. }
  133. // end of early return cases.
  134. MOZ_ASSERT(!mAlreadyRun, "converter should be run only once!");
  135. mAlreadyRun = true;
  136. // gather some compile-time meta-data about the formats at hand.
  137. typedef
  138. typename DataTypeForFormat<SrcFormat>::Type
  139. SrcType;
  140. typedef
  141. typename DataTypeForFormat<DstFormat>::Type
  142. DstType;
  143. const WebGLTexelFormat IntermediateSrcFormat
  144. = IntermediateFormat<SrcFormat>::Value;
  145. const WebGLTexelFormat IntermediateDstFormat
  146. = IntermediateFormat<DstFormat>::Value;
  147. typedef
  148. typename DataTypeForFormat<IntermediateSrcFormat>::Type
  149. IntermediateSrcType;
  150. typedef
  151. typename DataTypeForFormat<IntermediateDstFormat>::Type
  152. IntermediateDstType;
  153. const size_t NumElementsPerSrcTexel = NumElementsPerTexelForFormat<SrcFormat>();
  154. const size_t NumElementsPerDstTexel = NumElementsPerTexelForFormat<DstFormat>();
  155. const size_t MaxElementsPerTexel = 4;
  156. MOZ_ASSERT(NumElementsPerSrcTexel <= MaxElementsPerTexel, "unhandled format");
  157. MOZ_ASSERT(NumElementsPerDstTexel <= MaxElementsPerTexel, "unhandled format");
  158. // we assume that the strides are multiples of the sizeof of respective types.
  159. // this assumption will allow us to iterate over src and dst images using typed
  160. // pointers, e.g. uint8_t* or uint16_t* or float*, instead of untyped pointers.
  161. // So this assumption allows us to write cleaner and safer code, but it might
  162. // not be true forever and if it eventually becomes wrong, we'll have to revert
  163. // to always iterating using uint8_t* pointers regardless of the types at hand.
  164. MOZ_ASSERT(mSrcStride % sizeof(SrcType) == 0 &&
  165. mDstStride % sizeof(DstType) == 0,
  166. "Unsupported: texture stride is not a multiple of sizeof(type)");
  167. const ptrdiff_t srcStrideInElements = mSrcStride / sizeof(SrcType);
  168. const ptrdiff_t dstStrideInElements = mDstStride / sizeof(DstType);
  169. const SrcType* srcRowStart = static_cast<const SrcType*>(mSrcStart);
  170. DstType* dstRowStart = static_cast<DstType*>(mDstStart);
  171. // the loop performing the texture format conversion
  172. for (size_t i = 0; i < mHeight; ++i) {
  173. const SrcType* srcRowEnd = srcRowStart + mWidth * NumElementsPerSrcTexel;
  174. const SrcType* srcPtr = srcRowStart;
  175. DstType* dstPtr = dstRowStart;
  176. while (srcPtr != srcRowEnd) {
  177. // convert a single texel. We proceed in 3 steps: unpack the source texel
  178. // so the corresponding interchange format (e.g. unpack RGB565 to RGBA8),
  179. // convert the resulting data type to the destination type (e.g. convert
  180. // from RGBA8 to RGBA32F), and finally pack the destination texel
  181. // (e.g. pack RGBA32F to RGB32F).
  182. IntermediateSrcType unpackedSrc[MaxElementsPerTexel];
  183. IntermediateDstType unpackedDst[MaxElementsPerTexel];
  184. // unpack a src texel to corresponding intermediate src format.
  185. // for example, unpack RGB565 to RGBA8
  186. unpack<SrcFormat>(srcPtr, unpackedSrc);
  187. // convert the data type to the destination type, if needed.
  188. // for example, convert RGBA8 to RGBA32F
  189. convertType(unpackedSrc, unpackedDst);
  190. // pack the destination texel.
  191. // for example, pack RGBA32F to RGB32F
  192. // pack<DstFormat, PremultiplicationOp>(unpackedDst, dstPtr);
  193. srcPtr += NumElementsPerSrcTexel;
  194. dstPtr += NumElementsPerDstTexel;
  195. }
  196. srcRowStart += srcStrideInElements;
  197. dstRowStart += dstStrideInElements;
  198. }
  199. mSuccess = true;
  200. }
  201. template<WebGLTexelFormat SrcFormat,
  202. WebGLTexelFormat DstFormat>
  203. void run(WebGLTexelPremultiplicationOp premultiplicationOp)
  204. {
  205. #define WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(PremultiplicationOp) \
  206. case PremultiplicationOp: \
  207. return run<SrcFormat, DstFormat, PremultiplicationOp>();
  208. switch (premultiplicationOp) {
  209. WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(WebGLTexelPremultiplicationOp::None)
  210. WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(WebGLTexelPremultiplicationOp::Premultiply)
  211. WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(WebGLTexelPremultiplicationOp::Unpremultiply)
  212. default:
  213. MOZ_ASSERT(false, "unhandled case. Coding mistake?");
  214. }
  215. #undef WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP
  216. }
  217. template<WebGLTexelFormat SrcFormat>
  218. void run(WebGLTexelFormat dstFormat,
  219. WebGLTexelPremultiplicationOp premultiplicationOp)
  220. {
  221. #define WEBGLIMAGECONVERTER_CASE_DSTFORMAT(DstFormat) \
  222. case DstFormat: \
  223. return run<SrcFormat, DstFormat>(premultiplicationOp);
  224. switch (dstFormat) {
  225. // 1-channel formats
  226. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A8)
  227. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A16F)
  228. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A32F)
  229. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R8)
  230. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R16F)
  231. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R32F)
  232. // 2-channel formats
  233. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA8)
  234. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA16F)
  235. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA32F)
  236. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG8)
  237. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG16F)
  238. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG32F)
  239. // 3-channel formats
  240. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB565)
  241. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB8)
  242. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB11F11F10F)
  243. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB16F)
  244. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB32F)
  245. // 4-channel formats
  246. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA4444)
  247. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA5551)
  248. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA8)
  249. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA16F)
  250. WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA32F)
  251. default:
  252. MOZ_ASSERT(false, "unhandled case. Coding mistake?");
  253. }
  254. #undef WEBGLIMAGECONVERTER_CASE_DSTFORMAT
  255. }
  256. public:
  257. void run(WebGLTexelFormat srcFormat,
  258. WebGLTexelFormat dstFormat,
  259. WebGLTexelPremultiplicationOp premultiplicationOp)
  260. {
  261. #define WEBGLIMAGECONVERTER_CASE_SRCFORMAT(SrcFormat) \
  262. case SrcFormat: \
  263. return run<SrcFormat>(dstFormat, premultiplicationOp);
  264. switch (srcFormat) {
  265. // 1-channel formats
  266. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A8)
  267. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A16F)
  268. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A32F)
  269. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R8)
  270. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R16F)
  271. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R32F)
  272. // 2-channel formats
  273. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA8)
  274. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA16F)
  275. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA32F)
  276. // 3-channel formats
  277. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB565)
  278. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB8)
  279. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB16F)
  280. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB32F)
  281. // 4-channel formats
  282. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA4444)
  283. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA5551)
  284. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA8)
  285. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA16F)
  286. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA32F)
  287. // DOM element source formats
  288. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::BGRX8)
  289. WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::BGRA8)
  290. default:
  291. MOZ_ASSERT(false, "unhandled case. Coding mistake?");
  292. }
  293. #undef WEBGLIMAGECONVERTER_CASE_SRCFORMAT
  294. }
  295. WebGLImageConverter(size_t width, size_t height,
  296. const void* srcStart, void* dstStart,
  297. ptrdiff_t srcStride, ptrdiff_t dstStride)
  298. : mWidth(width), mHeight(height),
  299. mSrcStart(srcStart), mDstStart(dstStart),
  300. mSrcStride(srcStride), mDstStride(dstStride),
  301. mAlreadyRun(false), mSuccess(false)
  302. {}
  303. bool Success() const {
  304. return mSuccess;
  305. }
  306. };
  307. } // end anonymous namespace
  308. bool
  309. ConvertImage(size_t width, size_t height,
  310. const void* srcBegin, size_t srcStride, gl::OriginPos srcOrigin,
  311. WebGLTexelFormat srcFormat, bool srcPremultiplied,
  312. void* dstBegin, size_t dstStride, gl::OriginPos dstOrigin,
  313. WebGLTexelFormat dstFormat, bool dstPremultiplied,
  314. bool* const out_wasTrivial)
  315. {
  316. *out_wasTrivial = true;
  317. if (srcFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion ||
  318. dstFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion)
  319. {
  320. return false;
  321. }
  322. if (!width || !height)
  323. return true;
  324. const bool shouldYFlip = (srcOrigin != dstOrigin);
  325. const bool canSkipPremult = (!HasAlpha(srcFormat) ||
  326. !HasColor(srcFormat) ||
  327. !HasColor(dstFormat));
  328. WebGLTexelPremultiplicationOp premultOp;
  329. if (canSkipPremult) {
  330. premultOp = WebGLTexelPremultiplicationOp::None;
  331. } else if (!srcPremultiplied && dstPremultiplied) {
  332. premultOp = WebGLTexelPremultiplicationOp::Premultiply;
  333. } else if (srcPremultiplied && !dstPremultiplied) {
  334. premultOp = WebGLTexelPremultiplicationOp::Unpremultiply;
  335. } else {
  336. premultOp = WebGLTexelPremultiplicationOp::None;
  337. }
  338. const uint8_t* srcItr = (const uint8_t*)srcBegin;
  339. const uint8_t* const srcEnd = srcItr + srcStride * height;
  340. uint8_t* dstItr = (uint8_t*)dstBegin;
  341. ptrdiff_t dstItrStride = dstStride;
  342. if (shouldYFlip) {
  343. dstItr = dstItr + dstStride * (height - 1);
  344. dstItrStride = -dstItrStride;
  345. }
  346. if (srcFormat == dstFormat && premultOp == WebGLTexelPremultiplicationOp::None) {
  347. // Fast exit path: we just have to memcpy all the rows.
  348. //
  349. // The case where absolutely nothing needs to be done is supposed to have
  350. // been handled earlier (in TexImage2D_base, etc).
  351. //
  352. // So the case we're handling here is when even though no format conversion is
  353. // needed, we still might have to flip vertically and/or to adjust to a different
  354. // stride.
  355. // We ignore canSkipPremult for this perf trap, since it's an avoidable perf cliff
  356. // under the WebGL API user's control.
  357. MOZ_ASSERT((srcPremultiplied != dstPremultiplied ||
  358. shouldYFlip ||
  359. srcStride != dstStride),
  360. "Performance trap -- should handle this case earlier to avoid memcpy");
  361. const auto bytesPerPixel = TexelBytesForFormat(srcFormat);
  362. const size_t bytesPerRow = bytesPerPixel * width;
  363. while (srcItr != srcEnd) {
  364. memcpy(dstItr, srcItr, bytesPerRow);
  365. srcItr += srcStride;
  366. dstItr += dstItrStride;
  367. }
  368. return true;
  369. }
  370. *out_wasTrivial = false;
  371. WebGLImageConverter converter(width, height, srcItr, dstItr, srcStride, dstItrStride);
  372. converter.run(srcFormat, dstFormat, premultOp);
  373. if (!converter.Success()) {
  374. // the dst image may be left uninitialized, so we better not try to
  375. // continue even in release builds. This should never happen anyway,
  376. // and would be a bug in our code.
  377. MOZ_CRASH("programming mistake in WebGL texture conversions");
  378. }
  379. return true;
  380. }
  381. } // end namespace mozilla