2012-05-07 21:05:32 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-12-04 04:13:47 +04:00
|
|
|
#include "WebGLContext.h"
|
2012-05-07 21:05:32 +04:00
|
|
|
#include "WebGLTexelConversions.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace WebGLTexelConversions;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/** @class WebGLImageConverter
|
|
|
|
*
|
|
|
|
* This class is just a helper to implement WebGLContext::ConvertImage below.
|
|
|
|
*
|
|
|
|
* Design comments:
|
2014-01-24 07:52:33 +04:00
|
|
|
*
|
2012-05-07 21:05:32 +04:00
|
|
|
* WebGLContext::ConvertImage has to handle hundreds of format conversion paths.
|
|
|
|
* It is important to minimize executable code size here. Instead of passing
|
|
|
|
* around a large number of function parameters hundreds of times, we create a
|
|
|
|
* WebGLImageConverter object once, storing these parameters, and then we call
|
|
|
|
* the run() method on it.
|
|
|
|
*/
|
|
|
|
class WebGLImageConverter {
|
|
|
|
const size_t mWidth, mHeight;
|
|
|
|
const void* const mSrcStart;
|
|
|
|
void* const mDstStart;
|
|
|
|
const ptrdiff_t mSrcStride, mDstStride;
|
|
|
|
bool mAlreadyRun;
|
|
|
|
bool mSuccess;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
/*
|
|
|
|
* Returns sizeof(texel)/sizeof(type). The point is that we will iterate over
|
|
|
|
* texels with typed pointers and this value will tell us by how much we need
|
|
|
|
* to increment these pointers to advance to the next texel.
|
|
|
|
*/
|
2015-01-26 01:22:08 +03:00
|
|
|
template <WebGLTexelFormat Format>
|
2012-05-07 21:05:32 +04:00
|
|
|
static size_t NumElementsPerTexelForFormat() {
|
|
|
|
switch (Format) {
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::A8:
|
2014-01-24 01:47:37 +04:00
|
|
|
case WebGLTexelFormat::A16F:
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::A32F:
|
2015-12-22 21:59:00 +03:00
|
|
|
case WebGLTexelFormat::R8:
|
|
|
|
case WebGLTexelFormat::R16F:
|
|
|
|
case WebGLTexelFormat::R32F:
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RGB565:
|
2015-12-23 00:02:00 +03:00
|
|
|
case WebGLTexelFormat::RGB11F11F10F:
|
2015-12-22 21:59:00 +03:00
|
|
|
case WebGLTexelFormat::RGBA4444:
|
|
|
|
case WebGLTexelFormat::RGBA5551:
|
2012-05-07 21:05:32 +04:00
|
|
|
return 1;
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RA8:
|
2014-01-24 01:47:37 +04:00
|
|
|
case WebGLTexelFormat::RA16F:
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RA32F:
|
2015-12-22 23:56:00 +03:00
|
|
|
case WebGLTexelFormat::RG8:
|
2015-12-22 23:57:00 +03:00
|
|
|
case WebGLTexelFormat::RG16F:
|
2015-12-23 00:01:00 +03:00
|
|
|
case WebGLTexelFormat::RG32F:
|
2012-05-07 21:05:32 +04:00
|
|
|
return 2;
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RGB8:
|
2014-01-24 01:47:37 +04:00
|
|
|
case WebGLTexelFormat::RGB16F:
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RGB32F:
|
2012-05-07 21:05:32 +04:00
|
|
|
return 3;
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RGBA8:
|
2014-01-24 01:47:37 +04:00
|
|
|
case WebGLTexelFormat::RGBA16F:
|
2013-10-11 17:16:43 +04:00
|
|
|
case WebGLTexelFormat::RGBA32F:
|
2015-12-22 21:59:00 +03:00
|
|
|
case WebGLTexelFormat::BGRX8:
|
|
|
|
case WebGLTexelFormat::BGRA8:
|
2012-05-07 21:05:32 +04:00
|
|
|
return 4;
|
|
|
|
default:
|
2013-10-11 17:16:43 +04:00
|
|
|
MOZ_ASSERT(false, "Unknown texel format. Coding mistake?");
|
2012-05-07 21:05:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
/*
|
|
|
|
* This is the completely format-specific templatized conversion function,
|
|
|
|
* that will be instantiated hundreds of times for all different combinations.
|
|
|
|
* It is important to avoid generating useless code here. In particular, many
|
|
|
|
* instantiations of this function template will never be called, so we try
|
|
|
|
* to return immediately in these cases to allow the compiler to avoid
|
|
|
|
* generating useless code.
|
|
|
|
*/
|
2013-10-11 17:16:44 +04:00
|
|
|
template <WebGLTexelFormat SrcFormat, WebGLTexelFormat DstFormat,
|
|
|
|
WebGLTexelPremultiplicationOp PremultiplicationOp>
|
2012-05-07 21:05:32 +04:00
|
|
|
void run() {
|
|
|
|
// check for never-called cases. We early-return to allow the compiler
|
|
|
|
// to avoid generating this code. It would be tempting to abort() instead,
|
|
|
|
// as returning early does leave the destination surface with uninitialized
|
|
|
|
// data, but that would not allow the compiler to avoid generating this
|
|
|
|
// code. So instead, we return early, so Success() will return false, and
|
|
|
|
// the caller must check that and abort in that case. See
|
|
|
|
// WebGLContext::ConvertImage.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
if (SrcFormat == DstFormat &&
|
|
|
|
PremultiplicationOp == WebGLTexelPremultiplicationOp::None) {
|
|
|
|
// Should have used a fast exit path earlier, rather than entering this
|
|
|
|
// function. we explicitly return here to allow the compiler to avoid
|
|
|
|
// generating this code
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 01:22:08 +03:00
|
|
|
// Only textures uploaded from DOM elements or ImageData can allow DstFormat
|
2012-05-07 21:05:32 +04:00
|
|
|
// != SrcFormat. DOM elements can only give BGRA8, BGRX8, A8, RGB565
|
|
|
|
// formats. See DOMElementToImageSurface. ImageData is always RGBA8. So all
|
2015-01-26 01:22:08 +03:00
|
|
|
// other SrcFormat will always satisfy DstFormat==SrcFormat, so we can avoid
|
|
|
|
// compiling the code for all the unreachable paths.
|
2012-05-07 21:05:32 +04:00
|
|
|
const bool CanSrcFormatComeFromDOMElementOrImageData =
|
2015-01-26 01:22:08 +03:00
|
|
|
SrcFormat == WebGLTexelFormat::BGRA8 ||
|
|
|
|
SrcFormat == WebGLTexelFormat::BGRX8 ||
|
|
|
|
SrcFormat == WebGLTexelFormat::A8 ||
|
2012-05-07 21:05:32 +04:00
|
|
|
SrcFormat == WebGLTexelFormat::RGB565 ||
|
|
|
|
SrcFormat == WebGLTexelFormat::RGBA8;
|
|
|
|
if (!CanSrcFormatComeFromDOMElementOrImageData && SrcFormat != DstFormat) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Likewise, only textures uploaded from DOM elements or ImageData can
|
|
|
|
// possibly have to be unpremultiplied.
|
2015-12-22 23:56:00 +03:00
|
|
|
if (!CanSrcFormatComeFromDOMElementOrImageData &&
|
2015-12-22 21:59:00 +03:00
|
|
|
PremultiplicationOp == WebGLTexelPremultiplicationOp::Unpremultiply) {
|
|
|
|
return;
|
2012-05-07 21:05:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// there is no point in premultiplication/unpremultiplication
|
|
|
|
// in the following cases:
|
2015-12-22 21:59:00 +03:00
|
|
|
// - the source format has no alpha
|
|
|
|
// - the source format has no color
|
|
|
|
// - the destination format has no color
|
|
|
|
if (!HasAlpha(SrcFormat) || !HasColor(SrcFormat) || !HasColor(DstFormat)) {
|
|
|
|
if (PremultiplicationOp != WebGLTexelPremultiplicationOp::None) {
|
|
|
|
return;
|
2012-05-07 21:05:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of early return cases.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
MOZ_ASSERT(!mAlreadyRun, "converter should be run only once!");
|
|
|
|
mAlreadyRun = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
// gather some compile-time meta-data about the formats at hand.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-07-30 18:30:52 +03:00
|
|
|
using SrcType = typename DataTypeForFormat<SrcFormat>::Type;
|
|
|
|
using DstType = typename DataTypeForFormat<DstFormat>::Type;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
const WebGLTexelFormat IntermediateSrcFormat =
|
|
|
|
IntermediateFormat<SrcFormat>::Value;
|
2015-01-26 01:22:08 +03:00
|
|
|
const WebGLTexelFormat IntermediateDstFormat =
|
2012-05-07 21:05:32 +04:00
|
|
|
IntermediateFormat<DstFormat>::Value;
|
2021-07-30 18:30:52 +03:00
|
|
|
using IntermediateSrcType =
|
|
|
|
typename DataTypeForFormat<IntermediateSrcFormat>::Type;
|
|
|
|
using IntermediateDstType =
|
|
|
|
typename DataTypeForFormat<IntermediateDstFormat>::Type;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
const size_t NumElementsPerSrcTexel =
|
|
|
|
NumElementsPerTexelForFormat<SrcFormat>();
|
|
|
|
const size_t NumElementsPerDstTexel =
|
|
|
|
NumElementsPerTexelForFormat<DstFormat>();
|
|
|
|
const size_t MaxElementsPerTexel = 4;
|
2013-10-11 17:16:43 +04:00
|
|
|
MOZ_ASSERT(NumElementsPerSrcTexel <= MaxElementsPerTexel,
|
|
|
|
"unhandled format");
|
|
|
|
MOZ_ASSERT(NumElementsPerDstTexel <= MaxElementsPerTexel,
|
|
|
|
"unhandled format");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
// we assume that the strides are multiples of the sizeof of respective
|
|
|
|
// types. this assumption will allow us to iterate over src and dst images
|
|
|
|
// using typed pointers, e.g. uint8_t* or uint16_t* or float*, instead of
|
|
|
|
// untyped pointers. So this assumption allows us to write cleaner and safer
|
|
|
|
// code, but it might not be true forever and if it eventually becomes
|
|
|
|
// wrong, we'll have to revert to always iterating using uint8_t* pointers
|
|
|
|
// regardless of the types at hand.
|
|
|
|
MOZ_ASSERT(
|
|
|
|
mSrcStride % sizeof(SrcType) == 0 && mDstStride % sizeof(DstType) == 0,
|
2013-10-11 17:16:43 +04:00
|
|
|
"Unsupported: texture stride is not a multiple of sizeof(type)");
|
2019-10-23 20:29:30 +03:00
|
|
|
const ptrdiff_t srcStrideInElements =
|
|
|
|
mSrcStride / static_cast<ptrdiff_t>(sizeof(SrcType));
|
|
|
|
const ptrdiff_t dstStrideInElements =
|
|
|
|
mDstStride / static_cast<ptrdiff_t>(sizeof(DstType));
|
|
|
|
// Pop quiz: What's `ptrdiff_t(-16) / sizeof(int32_t)`?
|
|
|
|
// Did you guess +4611686018427387900?
|
|
|
|
MOZ_ASSERT(bool(srcStrideInElements < 0) == bool(mSrcStride < 0));
|
|
|
|
MOZ_ASSERT(bool(dstStrideInElements < 0) == bool(mDstStride < 0));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
const SrcType* srcRowStart = static_cast<const SrcType*>(mSrcStart);
|
2014-11-14 07:03:50 +03:00
|
|
|
DstType* dstRowStart = static_cast<DstType*>(mDstStart);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
// the loop performing the texture format conversion
|
|
|
|
for (size_t i = 0; i < mHeight; ++i) {
|
2014-11-14 07:03:50 +03:00
|
|
|
const SrcType* srcRowEnd = srcRowStart + mWidth * NumElementsPerSrcTexel;
|
2012-05-07 21:05:32 +04:00
|
|
|
const SrcType* srcPtr = srcRowStart;
|
2014-11-14 07:03:50 +03:00
|
|
|
DstType* dstPtr = dstRowStart;
|
2012-05-07 21:05:32 +04:00
|
|
|
while (srcPtr != srcRowEnd) {
|
|
|
|
// convert a single texel. We proceed in 3 steps: unpack the source
|
|
|
|
// texel so the corresponding interchange format (e.g. unpack RGB565 to
|
|
|
|
// RGBA8), convert the resulting data type to the destination type (e.g.
|
|
|
|
// convert from RGBA8 to RGBA32F), and finally pack the destination
|
|
|
|
// texel (e.g. pack RGBA32F to RGB32F).
|
|
|
|
IntermediateSrcType unpackedSrc[MaxElementsPerTexel];
|
|
|
|
IntermediateDstType unpackedDst[MaxElementsPerTexel];
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
// unpack a src texel to corresponding intermediate src format.
|
|
|
|
// for example, unpack RGB565 to RGBA8
|
|
|
|
unpack<SrcFormat>(srcPtr, unpackedSrc);
|
|
|
|
// convert the data type to the destination type, if needed.
|
|
|
|
// for example, convert RGBA8 to RGBA32F
|
|
|
|
convertType(unpackedSrc, unpackedDst);
|
|
|
|
// pack the destination texel.
|
|
|
|
// for example, pack RGBA32F to RGB32F
|
|
|
|
pack<DstFormat, PremultiplicationOp>(unpackedDst, dstPtr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
srcPtr += NumElementsPerSrcTexel;
|
|
|
|
dstPtr += NumElementsPerDstTexel;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-05-07 21:05:32 +04:00
|
|
|
srcRowStart += srcStrideInElements;
|
|
|
|
dstRowStart += dstStrideInElements;
|
|
|
|
}
|
|
|
|
|
2016-06-01 05:17:52 +03:00
|
|
|
mSuccess = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
template <WebGLTexelFormat SrcFormat, WebGLTexelFormat DstFormat>
|
2012-05-07 21:05:32 +04:00
|
|
|
void run(WebGLTexelPremultiplicationOp premultiplicationOp) {
|
2015-11-25 07:15:29 +03:00
|
|
|
#define WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(PremultiplicationOp) \
|
|
|
|
case PremultiplicationOp: \
|
|
|
|
return run<SrcFormat, DstFormat, PremultiplicationOp>();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
switch (premultiplicationOp) {
|
2013-10-11 17:16:44 +04:00
|
|
|
WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(
|
|
|
|
WebGLTexelPremultiplicationOp::None)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(
|
|
|
|
WebGLTexelPremultiplicationOp::Premultiply)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP(
|
|
|
|
WebGLTexelPremultiplicationOp::Unpremultiply)
|
2018-11-30 13:46:48 +03:00
|
|
|
default:
|
2015-11-25 07:15:29 +03:00
|
|
|
MOZ_ASSERT(false, "unhandled case. Coding mistake?");
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef WEBGLIMAGECONVERTER_CASE_PREMULTIPLICATIONOP
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
template <WebGLTexelFormat SrcFormat>
|
|
|
|
void run(WebGLTexelFormat dstFormat,
|
|
|
|
WebGLTexelPremultiplicationOp premultiplicationOp) {
|
|
|
|
#define WEBGLIMAGECONVERTER_CASE_DSTFORMAT(DstFormat) \
|
|
|
|
case DstFormat: \
|
|
|
|
return run<SrcFormat, DstFormat>(premultiplicationOp);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-05-07 21:05:32 +04:00
|
|
|
switch (dstFormat) {
|
2015-12-22 21:59:00 +03:00
|
|
|
// 1-channel formats
|
2015-11-25 07:15:29 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::A32F)
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::R32F)
|
|
|
|
// 2-channel formats
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RA32F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG8)
|
2015-12-22 23:57:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG16F)
|
2015-12-23 00:01:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RG32F)
|
2015-12-22 21:59:00 +03:00
|
|
|
// 3-channel formats
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB565)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB8)
|
2015-12-23 00:02:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB11F11F10F)
|
2015-11-25 07:15:29 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB16F)
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGB32F)
|
2015-11-25 07:15:29 +03:00
|
|
|
// 4-channel formats
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA4444)
|
2015-11-25 07:15:29 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA5551)
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA16F)
|
2015-11-25 07:15:29 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_DSTFORMAT(WebGLTexelFormat::RGBA32F)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "unhandled case. Coding mistake?");
|
|
|
|
}
|
2015-11-24 06:27:13 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
#undef WEBGLIMAGECONVERTER_CASE_DSTFORMAT
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-11-25 07:15:29 +03:00
|
|
|
void run(WebGLTexelFormat srcFormat, WebGLTexelFormat dstFormat,
|
|
|
|
WebGLTexelPremultiplicationOp premultiplicationOp) {
|
|
|
|
#define WEBGLIMAGECONVERTER_CASE_SRCFORMAT(SrcFormat) \
|
2012-05-07 21:05:32 +04:00
|
|
|
case SrcFormat: \
|
2015-11-25 07:15:29 +03:00
|
|
|
return run<SrcFormat>(dstFormat, premultiplicationOp);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
switch (srcFormat) {
|
|
|
|
// 1-channel formats
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::A32F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::R32F)
|
|
|
|
// 2-channel formats
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RA32F)
|
|
|
|
// 3-channel formats
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB565)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGB32F)
|
|
|
|
// 4-channel formats
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA4444)
|
2015-11-25 07:15:29 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA5551)
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA16F)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::RGBA32F)
|
2015-11-25 07:15:29 +03:00
|
|
|
// DOM element source formats
|
2015-12-22 21:59:00 +03:00
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::BGRX8)
|
|
|
|
WEBGLIMAGECONVERTER_CASE_SRCFORMAT(WebGLTexelFormat::BGRA8)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "unhandled case. Coding mistake?");
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef WEBGLIMAGECONVERTER_CASE_SRCFORMAT
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLImageConverter(size_t width, size_t height, const void* srcStart,
|
|
|
|
void* dstStart, ptrdiff_t srcStride, ptrdiff_t dstStride)
|
2012-05-07 21:05:32 +04:00
|
|
|
: mWidth(width),
|
|
|
|
mHeight(height),
|
|
|
|
mSrcStart(srcStart),
|
|
|
|
mDstStart(dstStart),
|
|
|
|
mSrcStride(srcStride),
|
|
|
|
mDstStride(dstStride),
|
|
|
|
mAlreadyRun(false),
|
|
|
|
mSuccess(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
bool Success() const { return mSuccess; }
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
} // end anonymous namespace
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
bool ConvertImage(size_t width, size_t height, const void* srcBegin,
|
|
|
|
size_t srcStride, gl::OriginPos srcOrigin,
|
|
|
|
WebGLTexelFormat srcFormat, bool srcPremultiplied,
|
|
|
|
void* dstBegin, size_t dstStride, gl::OriginPos dstOrigin,
|
|
|
|
WebGLTexelFormat dstFormat, bool dstPremultiplied,
|
2012-05-07 21:05:32 +04:00
|
|
|
bool* const out_wasTrivial) {
|
2015-11-25 07:15:29 +03:00
|
|
|
*out_wasTrivial = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (srcFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion ||
|
|
|
|
dstFormat == WebGLTexelFormat::FormatNotSupportingAnyConversion) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (!width || !height) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const bool shouldYFlip = (srcOrigin != dstOrigin);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const bool canSkipPremult =
|
|
|
|
(!HasAlpha(srcFormat) || !HasColor(srcFormat) || !HasColor(dstFormat));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLTexelPremultiplicationOp premultOp;
|
2016-12-16 02:50:35 +03:00
|
|
|
if (canSkipPremult) {
|
|
|
|
premultOp = WebGLTexelPremultiplicationOp::None;
|
|
|
|
} else if (!srcPremultiplied && dstPremultiplied) {
|
|
|
|
premultOp = WebGLTexelPremultiplicationOp::Premultiply;
|
|
|
|
} else if (srcPremultiplied && !dstPremultiplied) {
|
2013-10-11 17:16:44 +04:00
|
|
|
premultOp = WebGLTexelPremultiplicationOp::Unpremultiply;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2013-10-11 17:16:44 +04:00
|
|
|
premultOp = WebGLTexelPremultiplicationOp::None;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const uint8_t* srcItr = (const uint8_t*)srcBegin;
|
2016-12-16 02:50:35 +03:00
|
|
|
const uint8_t* const srcEnd = srcItr + srcStride * height;
|
2015-11-25 07:15:29 +03:00
|
|
|
uint8_t* dstItr = (uint8_t*)dstBegin;
|
2016-12-16 02:50:35 +03:00
|
|
|
ptrdiff_t dstItrStride = dstStride;
|
2015-11-25 07:15:29 +03:00
|
|
|
if (shouldYFlip) {
|
|
|
|
dstItr = dstItr + dstStride * (height - 1);
|
|
|
|
dstItrStride = -dstItrStride;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-12-16 02:50:35 +03:00
|
|
|
if (srcFormat == dstFormat &&
|
2013-10-11 17:16:44 +04:00
|
|
|
premultOp == WebGLTexelPremultiplicationOp::None) {
|
2016-12-16 02:50:35 +03:00
|
|
|
// Fast exit path: we just have to memcpy all the rows.
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2016-12-16 02:50:35 +03:00
|
|
|
// The case where absolutely nothing needs to be done is supposed to have
|
|
|
|
// been handled earlier (in TexImage2D_base, etc).
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2016-12-16 02:50:35 +03:00
|
|
|
// So the case we're handling here is when even though no format conversion
|
|
|
|
// is needed, we still might have to flip vertically and/or to adjust to a
|
|
|
|
// different stride.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-16 02:50:35 +03:00
|
|
|
// We ignore canSkipPremult for this perf trap, since it's an avoidable perf
|
2012-05-07 21:05:32 +04:00
|
|
|
// cliff under the WebGL API user's control.
|
2016-12-16 02:50:35 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
(srcPremultiplied != dstPremultiplied || shouldYFlip ||
|
|
|
|
srcStride != dstStride),
|
2015-11-25 07:15:29 +03:00
|
|
|
"Performance trap -- should handle this case earlier to avoid memcpy");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const auto bytesPerPixel = TexelBytesForFormat(srcFormat);
|
|
|
|
const size_t bytesPerRow = bytesPerPixel * width;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
while (srcItr != srcEnd) {
|
|
|
|
memcpy(dstItr, srcItr, bytesPerRow);
|
|
|
|
srcItr += srcStride;
|
|
|
|
dstItr += dstItrStride;
|
2014-10-10 00:07:07 +04:00
|
|
|
}
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-10-10 00:07:07 +04:00
|
|
|
|
2016-06-01 05:17:52 +03:00
|
|
|
*out_wasTrivial = false;
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLImageConverter converter(width, height, srcItr, dstItr, srcStride,
|
|
|
|
dstItrStride);
|
|
|
|
converter.run(srcFormat, dstFormat, premultOp);
|
2012-05-07 21:05:32 +04:00
|
|
|
|
|
|
|
if (!converter.Success()) {
|
|
|
|
// the dst image may be left uninitialized, so we better not try to
|
|
|
|
// continue even in release builds. This should never happen anyway,
|
|
|
|
// and would be a bug in our code.
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("programming mistake in WebGL texture conversions");
|
2012-05-07 21:05:32 +04:00
|
|
|
}
|
2014-10-10 00:07:07 +04:00
|
|
|
|
|
|
|
return true;
|
2012-05-07 21:05:32 +04:00
|
|
|
}
|
|
|
|
|
2014-01-24 07:52:33 +04:00
|
|
|
} // end namespace mozilla
|