2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-11-25 07:15:29 +03: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/. */
|
|
|
|
|
|
|
|
#include "TexUnpackBlob.h"
|
|
|
|
|
|
|
|
#include "GLBlitHelper.h"
|
|
|
|
#include "GLContext.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
2016-07-22 06:03:16 +03:00
|
|
|
#include "WebGLBuffer.h"
|
2015-11-25 07:15:29 +03:00
|
|
|
#include "WebGLContext.h"
|
2018-09-28 02:38:23 +03:00
|
|
|
#include "WebGLFormats.h"
|
2015-11-25 07:15:29 +03:00
|
|
|
#include "WebGLTexelConversions.h"
|
|
|
|
#include "WebGLTexture.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace webgl {
|
|
|
|
|
2016-12-21 02:03:21 +03:00
|
|
|
static bool IsPIValidForDOM(const webgl::PackingInfo& pi) {
|
|
|
|
// https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
|
|
|
|
|
|
|
|
// Just check for invalid individual formats and types, not combinations.
|
|
|
|
switch (pi.format) {
|
|
|
|
case LOCAL_GL_RGB:
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
case LOCAL_GL_RED:
|
|
|
|
case LOCAL_GL_RED_INTEGER:
|
|
|
|
case LOCAL_GL_RG:
|
|
|
|
case LOCAL_GL_RG_INTEGER:
|
|
|
|
case LOCAL_GL_RGB_INTEGER:
|
|
|
|
case LOCAL_GL_RGBA_INTEGER:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_SRGB:
|
|
|
|
case LOCAL_GL_SRGB_ALPHA:
|
|
|
|
// Allowed in WebGL1+EXT_srgb
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pi.type) {
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_5_6_5:
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_4_4_4_4:
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_5_5_5_1:
|
|
|
|
case LOCAL_GL_HALF_FLOAT:
|
|
|
|
case LOCAL_GL_HALF_FLOAT_OES:
|
|
|
|
case LOCAL_GL_FLOAT:
|
|
|
|
case LOCAL_GL_UNSIGNED_INT_10F_11F_11F_REV:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
static bool ValidatePIForDOM(WebGLContext* webgl,
|
|
|
|
const webgl::PackingInfo& pi) {
|
2016-12-21 02:03:21 +03:00
|
|
|
if (!IsPIValidForDOM(pi)) {
|
2019-02-02 13:02:23 +03:00
|
|
|
webgl->ErrorInvalidValue("Format or type is invalid for DOM sources.");
|
2016-12-21 02:03:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
static WebGLTexelFormat FormatForPackingInfo(const PackingInfo& pi) {
|
2015-11-25 07:15:29 +03:00
|
|
|
switch (pi.type) {
|
2016-07-12 08:51:19 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
|
|
|
switch (pi.format) {
|
|
|
|
case LOCAL_GL_RED:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
case LOCAL_GL_RED_INTEGER:
|
|
|
|
return WebGLTexelFormat::R8;
|
|
|
|
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
return WebGLTexelFormat::A8;
|
|
|
|
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
return WebGLTexelFormat::RA8;
|
|
|
|
|
|
|
|
case LOCAL_GL_RGB:
|
|
|
|
case LOCAL_GL_RGB_INTEGER:
|
2017-06-15 22:29:00 +03:00
|
|
|
case LOCAL_GL_SRGB:
|
2016-07-12 08:51:19 +03:00
|
|
|
return WebGLTexelFormat::RGB8;
|
|
|
|
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
case LOCAL_GL_RGBA_INTEGER:
|
2017-06-15 22:29:00 +03:00
|
|
|
case LOCAL_GL_SRGB_ALPHA:
|
2016-07-12 08:51:19 +03:00
|
|
|
return WebGLTexelFormat::RGBA8;
|
|
|
|
|
|
|
|
case LOCAL_GL_RG:
|
|
|
|
case LOCAL_GL_RG_INTEGER:
|
|
|
|
return WebGLTexelFormat::RG8;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_5_6_5:
|
2016-07-12 08:51:19 +03:00
|
|
|
if (pi.format == LOCAL_GL_RGB) return WebGLTexelFormat::RGB565;
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_5_5_5_1:
|
2016-07-12 08:51:19 +03:00
|
|
|
if (pi.format == LOCAL_GL_RGBA) return WebGLTexelFormat::RGBA5551;
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_4_4_4_4:
|
2016-07-12 08:51:19 +03:00
|
|
|
if (pi.format == LOCAL_GL_RGBA) return WebGLTexelFormat::RGBA4444;
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_HALF_FLOAT:
|
|
|
|
case LOCAL_GL_HALF_FLOAT_OES:
|
|
|
|
switch (pi.format) {
|
2016-07-12 08:51:19 +03:00
|
|
|
case LOCAL_GL_RED:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
return WebGLTexelFormat::R16F;
|
|
|
|
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
return WebGLTexelFormat::A16F;
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
return WebGLTexelFormat::RA16F;
|
|
|
|
case LOCAL_GL_RG:
|
|
|
|
return WebGLTexelFormat::RG16F;
|
|
|
|
case LOCAL_GL_RGB:
|
|
|
|
return WebGLTexelFormat::RGB16F;
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
return WebGLTexelFormat::RGBA16F;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_FLOAT:
|
|
|
|
switch (pi.format) {
|
2016-07-12 08:51:19 +03:00
|
|
|
case LOCAL_GL_RED:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
return WebGLTexelFormat::R32F;
|
|
|
|
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
return WebGLTexelFormat::A32F;
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
return WebGLTexelFormat::RA32F;
|
|
|
|
case LOCAL_GL_RG:
|
|
|
|
return WebGLTexelFormat::RG32F;
|
|
|
|
case LOCAL_GL_RGB:
|
|
|
|
return WebGLTexelFormat::RGB32F;
|
|
|
|
case LOCAL_GL_RGBA:
|
|
|
|
return WebGLTexelFormat::RGBA32F;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_UNSIGNED_INT_10F_11F_11F_REV:
|
|
|
|
if (pi.format == LOCAL_GL_RGB) return WebGLTexelFormat::RGB11F11F10F;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return WebGLTexelFormat::FormatNotSupportingAnyConversion;
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
////////////////////
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
static bool ValidateUnpackPixels(WebGLContext* webgl, uint32_t fullRows,
|
2016-10-21 04:03:40 +03:00
|
|
|
uint32_t tailPixels,
|
|
|
|
webgl::TexUnpackBlob* blob) {
|
|
|
|
if (!blob->mWidth || !blob->mHeight || !blob->mDepth) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-21 04:03:40 +03:00
|
|
|
const auto usedPixelsPerRow = CheckedUint32(blob->mSkipPixels) + blob->mWidth;
|
|
|
|
if (!usedPixelsPerRow.isValid() ||
|
|
|
|
usedPixelsPerRow.value() > blob->mRowLength) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"UNPACK_SKIP_PIXELS + width >"
|
|
|
|
" UNPACK_ROW_LENGTH.");
|
2016-10-21 04:03:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blob->mHeight > blob->mImageHeight) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation("height > UNPACK_IMAGE_HEIGHT.");
|
2016-10-21 04:03:40 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-10-21 04:03:40 +03:00
|
|
|
|
|
|
|
//////
|
|
|
|
|
|
|
|
// The spec doesn't bound SKIP_ROWS + height <= IMAGE_HEIGHT, unfortunately.
|
|
|
|
auto skipFullRows = CheckedUint32(blob->mSkipImages) * blob->mImageHeight;
|
|
|
|
skipFullRows += blob->mSkipRows;
|
|
|
|
|
|
|
|
MOZ_ASSERT(blob->mDepth >= 1);
|
|
|
|
MOZ_ASSERT(blob->mHeight >= 1);
|
|
|
|
auto usedFullRows = CheckedUint32(blob->mDepth - 1) * blob->mImageHeight;
|
|
|
|
usedFullRows +=
|
2018-07-27 07:46:33 +03:00
|
|
|
blob->mHeight - 1; // Full rows in the final image, excluding the tail.
|
2016-10-21 04:03:40 +03:00
|
|
|
|
|
|
|
const auto fullRowsNeeded = skipFullRows + usedFullRows;
|
|
|
|
if (!fullRowsNeeded.isValid()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorOutOfMemory("Invalid calculation for required row count.");
|
2016-10-21 04:03:40 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-10-21 04:03:40 +03:00
|
|
|
|
|
|
|
if (fullRows > fullRowsNeeded.value()) return true;
|
|
|
|
|
|
|
|
if (fullRows == fullRowsNeeded.value() &&
|
|
|
|
tailPixels >= usedPixelsPerRow.value()) {
|
|
|
|
blob->mNeedsExactUpload = true;
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Desired upload requires more data than is"
|
2016-10-21 04:03:40 +03:00
|
|
|
" available: (%u rows plus %u pixels needed, %u rows"
|
|
|
|
" plus %u pixels available)",
|
|
|
|
fullRowsNeeded.value(), usedPixelsPerRow.value(), fullRows, tailPixels);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
static bool ValidateUnpackBytes(WebGLContext* webgl,
|
2016-10-21 04:03:40 +03:00
|
|
|
const webgl::PackingInfo& pi,
|
|
|
|
size_t availByteCount,
|
|
|
|
webgl::TexUnpackBlob* blob) {
|
|
|
|
if (!blob->mWidth || !blob->mHeight || !blob->mDepth) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-21 04:03:40 +03:00
|
|
|
const auto bytesPerPixel = webgl::BytesPerPixel(pi);
|
|
|
|
const auto bytesPerRow = CheckedUint32(blob->mRowLength) * bytesPerPixel;
|
|
|
|
const auto rowStride = RoundUpToMultipleOf(bytesPerRow, blob->mAlignment);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-21 04:03:40 +03:00
|
|
|
const auto fullRows = availByteCount / rowStride;
|
|
|
|
if (!fullRows.isValid()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorOutOfMemory("Unacceptable upload size calculated.");
|
2016-10-21 04:03:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto bodyBytes = fullRows.value() * rowStride.value();
|
|
|
|
const auto tailPixels = (availByteCount - bodyBytes) / bytesPerPixel;
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
return ValidateUnpackPixels(webgl, fullRows.value(), tailPixels, blob);
|
2016-10-21 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
static uint32_t ZeroOn2D(TexImageTarget target, uint32_t val) {
|
|
|
|
return (IsTarget3D(target) ? val : 0);
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
static uint32_t FallbackOnZero(uint32_t val, uint32_t fallback) {
|
|
|
|
return (val ? val : fallback);
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
TexUnpackBlob::TexUnpackBlob(const WebGLContext* webgl, TexImageTarget target,
|
|
|
|
uint32_t rowLength, uint32_t width,
|
|
|
|
uint32_t height, uint32_t depth,
|
2017-04-12 12:55:23 +03:00
|
|
|
gfxAlphaType srcAlphaType)
|
2020-01-09 01:19:16 +03:00
|
|
|
: mAlignment(webgl->mPixelStore.mUnpackAlignment),
|
2016-07-12 08:51:19 +03:00
|
|
|
mRowLength(rowLength),
|
2016-12-14 01:51:06 +03:00
|
|
|
mImageHeight(FallbackOnZero(
|
2020-01-09 01:19:16 +03:00
|
|
|
ZeroOn2D(target, webgl->mPixelStore.mUnpackImageHeight), height))
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
,
|
2020-01-09 01:19:16 +03:00
|
|
|
mSkipPixels(webgl->mPixelStore.mUnpackSkipPixels),
|
|
|
|
mSkipRows(webgl->mPixelStore.mUnpackSkipRows),
|
|
|
|
mSkipImages(ZeroOn2D(target, webgl->mPixelStore.mUnpackSkipImages))
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
,
|
2016-07-12 08:51:19 +03:00
|
|
|
mWidth(width),
|
|
|
|
mHeight(height),
|
|
|
|
mDepth(depth)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
,
|
|
|
|
mSrcAlphaType(srcAlphaType)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 06:03:16 +03:00
|
|
|
,
|
|
|
|
mNeedsExactUpload(false) {
|
2016-07-12 08:51:19 +03:00
|
|
|
MOZ_ASSERT_IF(!IsTarget3D(target), mDepth == 1);
|
|
|
|
}
|
|
|
|
|
2017-03-23 23:44:16 +03:00
|
|
|
static bool HasColorAndAlpha(const WebGLTexelFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case WebGLTexelFormat::RA8:
|
|
|
|
case WebGLTexelFormat::RA16F:
|
|
|
|
case WebGLTexelFormat::RA32F:
|
|
|
|
case WebGLTexelFormat::RGBA8:
|
|
|
|
case WebGLTexelFormat::RGBA5551:
|
|
|
|
case WebGLTexelFormat::RGBA4444:
|
|
|
|
case WebGLTexelFormat::RGBA16F:
|
|
|
|
case WebGLTexelFormat::RGBA32F:
|
|
|
|
case WebGLTexelFormat::BGRA8:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackBlob::ConvertIfNeeded(
|
2016-12-14 01:51:06 +03:00
|
|
|
WebGLContext* webgl, const uint32_t rowLength, const uint32_t rowCount,
|
|
|
|
WebGLTexelFormat srcFormat, const uint8_t* const srcBegin,
|
|
|
|
const ptrdiff_t srcStride, WebGLTexelFormat dstFormat,
|
|
|
|
const ptrdiff_t dstStride, const uint8_t** const out_begin,
|
2016-07-12 08:51:19 +03:00
|
|
|
UniqueBuffer* const out_anchoredBuffer) const {
|
2016-12-14 01:51:06 +03:00
|
|
|
MOZ_ASSERT(srcFormat != WebGLTexelFormat::FormatNotSupportingAnyConversion);
|
|
|
|
MOZ_ASSERT(dstFormat != WebGLTexelFormat::FormatNotSupportingAnyConversion);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
*out_begin = srcBegin;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
if (!rowLength || !rowCount) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
const auto srcIsPremult = (mSrcAlphaType == gfxAlphaType::Premult);
|
2020-01-09 01:19:14 +03:00
|
|
|
const auto& dstIsPremult = webgl->mPixelStore.mPremultiplyAlpha;
|
2017-04-12 12:55:23 +03:00
|
|
|
const auto fnHasPremultMismatch = [&]() {
|
|
|
|
if (mSrcAlphaType == gfxAlphaType::Opaque) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
if (!HasColorAndAlpha(srcFormat)) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
return srcIsPremult != dstIsPremult;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
const auto srcOrigin =
|
|
|
|
(webgl->mPixelStore.mFlipY ? gl::OriginPos::TopLeft
|
|
|
|
: gl::OriginPos::BottomLeft);
|
2016-07-12 08:51:19 +03:00
|
|
|
const auto dstOrigin = gl::OriginPos::BottomLeft;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
if (srcFormat != dstFormat) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->GeneratePerfWarning(
|
|
|
|
"Conversion requires pixel reformatting. (%u->%u)", uint32_t(srcFormat),
|
2017-03-23 23:44:16 +03:00
|
|
|
uint32_t(dstFormat));
|
2017-04-12 12:55:23 +03:00
|
|
|
} else if (fnHasPremultMismatch()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->GeneratePerfWarning(
|
|
|
|
"Conversion requires change in"
|
|
|
|
" alpha-premultiplication.");
|
2016-12-14 01:51:06 +03:00
|
|
|
} else if (srcOrigin != dstOrigin) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->GeneratePerfWarning("Conversion requires y-flip.");
|
2016-12-14 01:51:06 +03:00
|
|
|
} else if (srcStride != dstStride) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->GeneratePerfWarning("Conversion requires change in stride. (%u->%u)",
|
|
|
|
uint32_t(srcStride), uint32_t(dstStride));
|
2016-12-14 01:51:06 +03:00
|
|
|
} else {
|
2016-07-13 22:36:51 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
////
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto dstTotalBytes = CheckedUint32(rowCount) * dstStride;
|
|
|
|
if (!dstTotalBytes.isValid()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorOutOfMemory("Calculation failed.");
|
2016-07-12 08:51:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-13 22:36:51 +03:00
|
|
|
|
2019-03-07 03:41:16 +03:00
|
|
|
UniqueBuffer dstBuffer = calloc(1u, (size_t)dstTotalBytes.value());
|
2016-12-14 01:51:06 +03:00
|
|
|
if (!dstBuffer.get()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorOutOfMemory("Failed to allocate dest buffer.");
|
2016-12-14 01:51:06 +03:00
|
|
|
return false;
|
2016-07-13 22:36:51 +03:00
|
|
|
}
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto dstBegin = static_cast<uint8_t*>(dstBuffer.get());
|
2016-07-13 22:36:51 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
////
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
// And go!:
|
|
|
|
bool wasTrivial;
|
2016-12-14 01:51:06 +03:00
|
|
|
if (!ConvertImage(rowLength, rowCount, srcBegin, srcStride, srcOrigin,
|
|
|
|
srcFormat, srcIsPremult, dstBegin, dstStride, dstOrigin,
|
2016-07-12 08:51:19 +03:00
|
|
|
dstFormat, dstIsPremult, &wasTrivial)) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorImplementationBug("ConvertImage failed.");
|
2016-07-12 08:51:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
*out_begin = dstBegin;
|
2018-05-30 22:15:35 +03:00
|
|
|
*out_anchoredBuffer = std::move(dstBuffer);
|
2016-07-12 08:51:19 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLenum DoTexOrSubImage(bool isSubImage, gl::GLContext* gl,
|
|
|
|
TexImageTarget target, GLint level,
|
|
|
|
const DriverUnpackInfo* dui, GLint xOffset,
|
|
|
|
GLint yOffset, GLint zOffset, GLsizei width,
|
|
|
|
GLsizei height, GLsizei depth, const void* data) {
|
|
|
|
if (isSubImage) {
|
|
|
|
return DoTexSubImage(gl, target, level, xOffset, yOffset, zOffset, width,
|
|
|
|
height, depth, dui->ToPacking(), data);
|
|
|
|
} else {
|
|
|
|
return DoTexImage(gl, target, level, dui, width, height, depth, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TexUnpackBytes
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
TexUnpackBytes::TexUnpackBytes(const WebGLContext* webgl, TexImageTarget target,
|
|
|
|
uint32_t width, uint32_t height, uint32_t depth,
|
2016-10-21 04:03:40 +03:00
|
|
|
bool isClientData, const uint8_t* ptr,
|
|
|
|
size_t availBytes)
|
2020-01-09 01:19:16 +03:00
|
|
|
: TexUnpackBlob(webgl, target,
|
|
|
|
FallbackOnZero(webgl->mPixelStore.mUnpackRowLength, width),
|
|
|
|
width, height, depth, gfxAlphaType::NonPremult),
|
|
|
|
mIsClientData(isClientData),
|
|
|
|
mPtr(ptr),
|
|
|
|
mAvailBytes(availBytes) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackBytes::Validate(WebGLContext* webgl,
|
|
|
|
const webgl::PackingInfo& pi) {
|
2020-01-09 01:19:16 +03:00
|
|
|
if (mIsClientData && !mPtr) return true;
|
|
|
|
|
|
|
|
return ValidateUnpackBytes(webgl, pi, mAvailBytes, this);
|
2016-10-21 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackBytes::TexOrSubImage(bool isSubImage, bool needsRespec,
|
2016-07-12 08:51:19 +03:00
|
|
|
WebGLTexture* tex, TexImageTarget target,
|
2016-12-14 01:51:06 +03:00
|
|
|
GLint level,
|
|
|
|
const webgl::DriverUnpackInfo* dui,
|
2018-07-27 07:46:33 +03:00
|
|
|
GLint xOffset, GLint yOffset, GLint zOffset,
|
|
|
|
const webgl::PackingInfo& pi,
|
2016-12-14 01:51:06 +03:00
|
|
|
GLenum* const out_error) const {
|
|
|
|
WebGLContext* webgl = tex->mContext;
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2016-12-15 05:38:08 +03:00
|
|
|
const auto format = FormatForPackingInfo(pi);
|
|
|
|
const auto bytesPerPixel = webgl::BytesPerPixel(pi);
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
const uint8_t* uploadPtr = mPtr;
|
2016-07-22 06:03:16 +03:00
|
|
|
UniqueBuffer tempBuffer;
|
2016-12-15 05:38:08 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
do {
|
2020-01-09 01:19:16 +03:00
|
|
|
if (!mIsClientData || !mPtr) break;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (!webgl->mPixelStore.mFlipY && !webgl->mPixelStore.mPremultiplyAlpha) {
|
2016-07-22 06:03:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (webgl->mPixelStore.mUnpackImageHeight ||
|
|
|
|
webgl->mPixelStore.mUnpackSkipImages ||
|
|
|
|
webgl->mPixelStore.mUnpackRowLength ||
|
|
|
|
webgl->mPixelStore.mUnpackSkipRows ||
|
|
|
|
webgl->mPixelStore.mUnpackSkipPixels) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Non-DOM-Element uploads with alpha-premult"
|
|
|
|
" or y-flip do not support subrect selection.");
|
2016-12-14 01:51:06 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-22 06:03:16 +03:00
|
|
|
|
|
|
|
webgl->GenerateWarning(
|
|
|
|
"Alpha-premult and y-flip are deprecated for"
|
|
|
|
" non-DOM-Element uploads.");
|
|
|
|
|
|
|
|
const uint32_t rowLength = mWidth;
|
2016-12-14 01:51:06 +03:00
|
|
|
const uint32_t rowCount = mHeight * mDepth;
|
2016-07-22 06:03:16 +03:00
|
|
|
const auto stride =
|
2016-09-26 23:38:56 +03:00
|
|
|
RoundUpToMultipleOf(rowLength * bytesPerPixel, mAlignment);
|
2020-01-09 01:19:16 +03:00
|
|
|
if (!ConvertIfNeeded(webgl, rowLength, rowCount, format, mPtr, stride,
|
|
|
|
format, stride, &uploadPtr, &tempBuffer)) {
|
2016-11-16 04:52:41 +03:00
|
|
|
return false;
|
2016-07-22 06:03:16 +03:00
|
|
|
}
|
2016-12-14 01:51:06 +03:00
|
|
|
} while (false);
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
//////
|
2016-09-26 23:38:56 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto& gl = webgl->gl;
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
bool useParanoidHandling = false;
|
|
|
|
if (mNeedsExactUpload && webgl->mBoundPixelUnpackBuffer) {
|
|
|
|
webgl->GenerateWarning(
|
|
|
|
"Uploads from a buffer with a final row with a byte"
|
|
|
|
" count smaller than the row stride can incur extra"
|
|
|
|
" overhead.");
|
|
|
|
|
2016-07-22 06:03:16 +03:00
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
useParanoidHandling |= (gl->Vendor() == gl::GLVendor::NVIDIA);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-22 06:03:16 +03:00
|
|
|
|
|
|
|
if (!useParanoidHandling) {
|
2016-07-22 09:05:52 +03:00
|
|
|
if (webgl->mBoundPixelUnpackBuffer) {
|
|
|
|
gl->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER,
|
2016-07-22 06:03:16 +03:00
|
|
|
webgl->mBoundPixelUnpackBuffer->mGLName);
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
*out_error =
|
|
|
|
DoTexOrSubImage(isSubImage, gl, target, level, dui, xOffset, yOffset,
|
|
|
|
zOffset, mWidth, mHeight, mDepth, uploadPtr);
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
if (webgl->mBoundPixelUnpackBuffer) {
|
|
|
|
gl->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
}
|
2016-07-22 09:05:52 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
//////
|
2016-07-22 06:03:16 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(webgl->mBoundPixelUnpackBuffer);
|
|
|
|
|
|
|
|
if (!isSubImage) {
|
2016-07-22 09:05:52 +03:00
|
|
|
// Alloc first to catch OOMs.
|
|
|
|
AssertUintParamCorrect(gl, LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
*out_error =
|
|
|
|
DoTexOrSubImage(false, gl, target, level, dui, xOffset, yOffset,
|
|
|
|
zOffset, mWidth, mHeight, mDepth, nullptr);
|
|
|
|
if (*out_error) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-22 06:03:16 +03:00
|
|
|
|
|
|
|
const ScopedLazyBind bindPBO(gl, LOCAL_GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
webgl->mBoundPixelUnpackBuffer);
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
//////
|
2016-07-22 06:03:16 +03:00
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
// Make our sometimes-implicit values explicit. Also this keeps them constant
|
|
|
|
// when we ask for height=mHeight-1 and such.
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, mRowLength);
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, mImageHeight);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-24 01:28:55 +03:00
|
|
|
if (mDepth > 1) {
|
2016-07-22 06:03:16 +03:00
|
|
|
*out_error =
|
|
|
|
DoTexOrSubImage(true, gl, target, level, dui, xOffset, yOffset, zOffset,
|
|
|
|
mWidth, mHeight, mDepth - 1, uploadPtr);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
// Skip the images we uploaded.
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES, mSkipImages + mDepth - 1);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 06:03:16 +03:00
|
|
|
if (mHeight > 1) {
|
|
|
|
*out_error = DoTexOrSubImage(true, gl, target, level, dui, xOffset, yOffset,
|
|
|
|
zOffset + mDepth - 1, mWidth, mHeight - 1, 1,
|
|
|
|
uploadPtr);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
const auto totalSkipRows =
|
2016-10-21 04:03:40 +03:00
|
|
|
CheckedUint32(mSkipImages) * mImageHeight + mSkipRows;
|
2016-07-22 09:05:52 +03:00
|
|
|
const auto totalFullRows =
|
|
|
|
CheckedUint32(mDepth - 1) * mImageHeight + mHeight - 1;
|
|
|
|
const auto tailOffsetRows = totalSkipRows + totalFullRows;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto bytesPerRow = CheckedUint32(mRowLength) * bytesPerPixel;
|
|
|
|
const auto rowStride = RoundUpToMultipleOf(bytesPerRow, mAlignment);
|
|
|
|
if (!rowStride.isValid()) {
|
|
|
|
MOZ_CRASH("Should be checked earlier.");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-22 09:05:52 +03:00
|
|
|
const auto tailOffsetBytes = tailOffsetRows * rowStride;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
uploadPtr += tailOffsetBytes.value();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
//////
|
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 1); // No stride padding.
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, 0); // No padding in general.
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES, 0); // Don't skip images,
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS,
|
2016-07-22 09:05:52 +03:00
|
|
|
0); // or rows.
|
|
|
|
// Keep skipping pixels though!
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 06:03:16 +03:00
|
|
|
*out_error = DoTexOrSubImage(true, gl, target, level, dui, xOffset,
|
|
|
|
yOffset + mHeight - 1, zOffset + mDepth - 1,
|
|
|
|
mWidth, 1, 1, uploadPtr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 09:05:52 +03:00
|
|
|
// Reset all our modified state.
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackAlignment);
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackImageHeight);
|
2016-07-22 09:05:52 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackRowLength);
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackSkipImages);
|
2016-07-22 06:03:16 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackSkipRows);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-22 20:28:11 +03:00
|
|
|
return true;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TexUnpackImage
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
TexUnpackImage::TexUnpackImage(const WebGLContext* webgl, TexImageTarget target,
|
2020-01-09 01:19:16 +03:00
|
|
|
uint32_t width, uint32_t height, uint32_t depth,
|
2017-04-12 12:55:23 +03:00
|
|
|
layers::Image* image, gfxAlphaType srcAlphaType)
|
2020-01-09 01:19:16 +03:00
|
|
|
: TexUnpackBlob(webgl, target, image->GetSize().width, width, height, depth,
|
|
|
|
srcAlphaType),
|
2015-11-25 07:15:29 +03:00
|
|
|
mImage(image) {}
|
|
|
|
|
2020-02-19 00:08:00 +03:00
|
|
|
TexUnpackImage::~TexUnpackImage() = default;
|
2016-07-22 09:25:41 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackImage::Validate(WebGLContext* webgl,
|
|
|
|
const webgl::PackingInfo& pi) {
|
|
|
|
if (!ValidatePIForDOM(webgl, pi)) return false;
|
2020-01-09 01:19:16 +03:00
|
|
|
|
|
|
|
const auto fullRows = mImage->GetSize().height;
|
|
|
|
return ValidateUnpackPixels(webgl, fullRows, 0, this);
|
2016-10-21 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackImage::TexOrSubImage(bool isSubImage, bool needsRespec,
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLTexture* tex, TexImageTarget target,
|
|
|
|
GLint level,
|
|
|
|
const webgl::DriverUnpackInfo* dui,
|
2017-06-15 22:29:00 +03:00
|
|
|
GLint xOffset, GLint yOffset, GLint zOffset,
|
|
|
|
const webgl::PackingInfo& pi,
|
|
|
|
GLenum* const out_error) const {
|
2015-11-25 07:15:29 +03:00
|
|
|
MOZ_ASSERT_IF(needsRespec, !isSubImage);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLContext* webgl = tex->mContext;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
gl::GLContext* gl = webgl->GL();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (needsRespec) {
|
2016-06-22 20:28:11 +03:00
|
|
|
*out_error =
|
|
|
|
DoTexOrSubImage(isSubImage, gl, target.get(), level, dui, xOffset,
|
|
|
|
yOffset, zOffset, mWidth, mHeight, mDepth, nullptr);
|
2016-07-06 00:01:57 +03:00
|
|
|
if (*out_error) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-24 01:28:55 +03:00
|
|
|
const char* fallbackReason;
|
2018-11-30 13:46:48 +03:00
|
|
|
do {
|
2017-03-24 01:28:55 +03:00
|
|
|
if (mDepth != 1) {
|
|
|
|
fallbackReason = "depth is not 1";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-08-31 23:38:23 +03:00
|
|
|
if (xOffset != 0 || yOffset != 0 || zOffset != 0) {
|
2016-11-16 04:52:41 +03:00
|
|
|
fallbackReason = "x/y/zOffset is not 0";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (webgl->mPixelStore.mUnpackSkipPixels ||
|
|
|
|
webgl->mPixelStore.mUnpackSkipRows ||
|
|
|
|
webgl->mPixelStore.mUnpackSkipImages) {
|
2017-03-24 01:28:55 +03:00
|
|
|
fallbackReason = "non-zero UNPACK_SKIP_* not yet supported";
|
2015-11-25 07:15:29 +03:00
|
|
|
break;
|
2017-04-11 12:15:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto fnHasPremultMismatch = [&]() {
|
|
|
|
if (mSrcAlphaType == gfxAlphaType::Opaque) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-11 12:15:25 +03:00
|
|
|
const bool srcIsPremult = (mSrcAlphaType == gfxAlphaType::Premult);
|
2020-01-09 01:19:14 +03:00
|
|
|
const auto& dstIsPremult = webgl->mPixelStore.mPremultiplyAlpha;
|
2017-04-12 12:55:23 +03:00
|
|
|
if (srcIsPremult == dstIsPremult) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-24 01:28:55 +03:00
|
|
|
if (dstIsPremult) {
|
2017-04-11 12:15:25 +03:00
|
|
|
fallbackReason = "UNPACK_PREMULTIPLY_ALPHA_WEBGL is not true";
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2017-03-24 01:28:55 +03:00
|
|
|
fallbackReason = "UNPACK_PREMULTIPLY_ALPHA_WEBGL is not false";
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-04-12 12:55:23 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2017-04-11 12:15:25 +03:00
|
|
|
if (fnHasPremultMismatch()) break;
|
2016-06-01 05:18:31 +03:00
|
|
|
|
2016-07-22 09:25:41 +03:00
|
|
|
if (dui->unpackFormat != LOCAL_GL_RGB &&
|
2017-03-24 01:28:55 +03:00
|
|
|
dui->unpackFormat != LOCAL_GL_RGBA) {
|
2016-07-22 09:25:41 +03:00
|
|
|
fallbackReason = "`format` is not RGB or RGBA";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
|
|
|
|
if (dui->unpackType != LOCAL_GL_UNSIGNED_BYTE) {
|
|
|
|
fallbackReason = "`type` is not UNSIGNED_BYTE";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2016-07-12 08:51:19 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
gl::ScopedFramebuffer scopedFB(gl);
|
|
|
|
gl::ScopedBindFramebuffer bindFB(gl, scopedFB.FB());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
{
|
2018-07-27 07:46:33 +03:00
|
|
|
gl::GLContext::LocalErrorScope errorScope(*gl);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
gl->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER,
|
|
|
|
LOCAL_GL_COLOR_ATTACHMENT0, target.get(),
|
2015-11-25 07:15:29 +03:00
|
|
|
tex->mGLName, level);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-24 01:28:55 +03:00
|
|
|
if (errorScope.GetError()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
fallbackReason = "bug: failed to attach to FB for blit";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const GLenum status = gl->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
if (status != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
fallbackReason = "bug: failed to confirm FB for blit";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-29 05:07:46 +03:00
|
|
|
const gfx::IntSize dstSize(mWidth, mHeight);
|
2016-07-12 08:51:19 +03:00
|
|
|
const auto dstOrigin =
|
2020-01-09 01:19:14 +03:00
|
|
|
(webgl->mPixelStore.mFlipY ? gl::OriginPos::TopLeft
|
|
|
|
: gl::OriginPos::BottomLeft);
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!gl->BlitHelper()->BlitImageToFramebuffer(mImage, dstSize, dstOrigin)) {
|
2016-11-24 03:55:00 +03:00
|
|
|
fallbackReason = "likely bug: failed to blit";
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2016-05-12 09:08:24 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
// Blitting was successful, so we're done!
|
2016-06-22 20:28:11 +03:00
|
|
|
*out_error = 0;
|
2016-07-12 08:51:19 +03:00
|
|
|
return true;
|
2015-11-25 07:15:29 +03:00
|
|
|
} while (false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
const nsPrintfCString perfMsg(
|
|
|
|
"Failed to hit GPU-copy fast-path: %s (src type %u)", fallbackReason,
|
2017-04-12 12:55:23 +03:00
|
|
|
uint32_t(mImage->GetFormat()));
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (webgl->mPixelStore.mRequireFastPath) {
|
2017-04-11 12:15:25 +03:00
|
|
|
webgl->ErrorInvalidOperation("%s", perfMsg.BeginReading());
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 12:15:25 +03:00
|
|
|
webgl->GeneratePerfWarning("%s Falling back to CPU upload.",
|
|
|
|
perfMsg.BeginReading());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 09:25:41 +03:00
|
|
|
const RefPtr<gfx::SourceSurface> surf = mImage->GetAsSourceSurface();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-22 09:25:41 +03:00
|
|
|
RefPtr<gfx::DataSourceSurface> dataSurf;
|
2016-07-12 08:51:19 +03:00
|
|
|
if (surf) {
|
|
|
|
// WARNING: OSX can lose our MakeCurrent here.
|
|
|
|
dataSurf = surf->GetDataSurface();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
if (!dataSurf) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorOutOfMemory(
|
2016-07-12 08:51:19 +03:00
|
|
|
"GetAsSourceSurface or GetDataSurface failed after"
|
2017-03-24 01:28:55 +03:00
|
|
|
" blit failed for TexUnpackImage.");
|
2017-04-11 12:15:25 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
const TexUnpackSurface surfBlob(webgl, target, mWidth, mHeight, mDepth,
|
|
|
|
dataSurf, mSrcAlphaType);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
return surfBlob.TexOrSubImage(isSubImage, needsRespec, tex, target, level,
|
2017-06-15 22:29:00 +03:00
|
|
|
dui, xOffset, yOffset, zOffset, pi, out_error);
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TexUnpackSurface
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
TexUnpackSurface::TexUnpackSurface(const WebGLContext* webgl,
|
2016-07-12 08:51:19 +03:00
|
|
|
TexImageTarget target, uint32_t width,
|
2016-06-22 20:28:11 +03:00
|
|
|
uint32_t height, uint32_t depth,
|
2017-04-12 12:55:23 +03:00
|
|
|
gfx::DataSourceSurface* surf,
|
|
|
|
gfxAlphaType srcAlphaType)
|
2020-01-09 01:19:16 +03:00
|
|
|
: TexUnpackBlob(webgl, target, surf->GetSize().width, width, height, depth,
|
|
|
|
srcAlphaType),
|
|
|
|
mSurf(surf) {}
|
2016-06-22 20:28:11 +03:00
|
|
|
|
|
|
|
//////////
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
static bool GetFormatForSurf(gfx::SourceSurface* surf,
|
2016-07-12 08:51:19 +03:00
|
|
|
WebGLTexelFormat* const out_texelFormat,
|
|
|
|
uint8_t* const out_bpp) {
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto surfFormat = surf->GetFormat();
|
2015-11-25 07:15:29 +03:00
|
|
|
switch (surfFormat) {
|
|
|
|
case gfx::SurfaceFormat::B8G8R8A8:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::BGRA8;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 4;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::B8G8R8X8:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::BGRX8;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 4;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::R8G8B8A8:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::RGBA8;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 4;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::R8G8B8X8:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::RGBX8;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 4;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::R5G6B5_UINT16:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::RGB565;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 2;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::A8:
|
|
|
|
*out_texelFormat = WebGLTexelFormat::A8;
|
2016-07-12 08:51:19 +03:00
|
|
|
*out_bpp = 1;
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case gfx::SurfaceFormat::YUV:
|
|
|
|
// Ugh...
|
|
|
|
NS_ERROR("We don't handle uploads from YUV sources yet.");
|
|
|
|
// When we want to, check out gfx/ycbcr/YCbCrUtils.h. (specifically
|
|
|
|
// GetYCbCrToRGBDestFormatAndSize and ConvertYCbCrToRGB)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
//////////
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackSurface::Validate(WebGLContext* webgl,
|
|
|
|
const webgl::PackingInfo& pi) {
|
|
|
|
if (!ValidatePIForDOM(webgl, pi)) return false;
|
2016-12-21 02:03:21 +03:00
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto fullRows = mSurf->GetSize().height;
|
2018-07-27 07:46:33 +03:00
|
|
|
return ValidateUnpackPixels(webgl, fullRows, 0, this);
|
2016-10-21 04:03:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool TexUnpackSurface::TexOrSubImage(
|
2015-11-25 07:15:29 +03:00
|
|
|
bool isSubImage, bool needsRespec, WebGLTexture* tex, TexImageTarget target,
|
2017-06-15 22:29:00 +03:00
|
|
|
GLint level, const webgl::DriverUnpackInfo* dui, GLint xOffset,
|
|
|
|
GLint yOffset, GLint zOffset, const webgl::PackingInfo& dstPI,
|
2016-06-22 20:28:11 +03:00
|
|
|
GLenum* const out_error) const {
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto& webgl = tex->mContext;
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto rowLength = mSurf->GetSize().width;
|
|
|
|
const auto rowCount = mSurf->GetSize().height;
|
2016-12-14 01:51:06 +03:00
|
|
|
|
|
|
|
const auto& dstBPP = webgl::BytesPerPixel(dstPI);
|
|
|
|
const auto dstFormat = FormatForPackingInfo(dstPI);
|
|
|
|
|
|
|
|
////
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-07-12 08:51:19 +03:00
|
|
|
WebGLTexelFormat srcFormat;
|
|
|
|
uint8_t srcBPP;
|
2020-01-09 01:19:16 +03:00
|
|
|
if (!GetFormatForSurf(mSurf, &srcFormat, &srcBPP)) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorImplementationBug(
|
|
|
|
"GetFormatForSurf failed for"
|
2016-07-12 08:51:19 +03:00
|
|
|
" WebGLTexelFormat::%u.",
|
2020-01-09 01:19:16 +03:00
|
|
|
uint32_t(mSurf->GetFormat()));
|
2016-06-22 20:28:11 +03:00
|
|
|
return false;
|
2016-04-11 12:45:09 +03:00
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
gfx::DataSourceSurface::ScopedMap map(mSurf,
|
|
|
|
gfx::DataSourceSurface::MapType::READ);
|
|
|
|
if (!map.IsMapped()) {
|
|
|
|
webgl->ErrorOutOfMemory("Failed to map source surface for upload.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& srcBegin = map.GetData();
|
|
|
|
const auto& srcStride = map.GetStride();
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
////
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto srcRowLengthBytes = rowLength * srcBPP;
|
|
|
|
|
|
|
|
const uint8_t maxGLAlignment = 8;
|
|
|
|
uint8_t srcAlignment = 1;
|
|
|
|
for (; srcAlignment <= maxGLAlignment; srcAlignment *= 2) {
|
|
|
|
const auto strideGuess =
|
|
|
|
RoundUpToMultipleOf(srcRowLengthBytes, srcAlignment);
|
|
|
|
if (strideGuess == srcStride) break;
|
|
|
|
}
|
|
|
|
const uint32_t dstAlignment =
|
|
|
|
(srcAlignment > maxGLAlignment) ? 1 : srcAlignment;
|
2016-07-12 08:51:19 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto dstRowLengthBytes = rowLength * dstBPP;
|
|
|
|
const auto dstStride = RoundUpToMultipleOf(dstRowLengthBytes, dstAlignment);
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const uint8_t* dstBegin = srcBegin;
|
2016-07-12 08:51:19 +03:00
|
|
|
UniqueBuffer tempBuffer;
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ConvertIfNeeded(webgl, rowLength, rowCount, srcFormat, srcBegin,
|
2016-12-14 01:51:06 +03:00
|
|
|
srcStride, dstFormat, dstStride, &dstBegin,
|
|
|
|
&tempBuffer)) {
|
2016-06-22 20:28:11 +03:00
|
|
|
return false;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
////
|
2016-07-12 08:51:19 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
const auto& gl = webgl->gl;
|
2017-07-24 13:24:25 +03:00
|
|
|
if (!gl->MakeCurrent()) {
|
|
|
|
*out_error = LOCAL_GL_CONTEXT_LOST;
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, dstAlignment);
|
|
|
|
if (webgl->IsWebGL2()) {
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, rowLength);
|
2016-07-12 22:43:39 +03:00
|
|
|
}
|
2016-07-12 08:51:19 +03:00
|
|
|
|
2017-06-15 22:29:00 +03:00
|
|
|
*out_error =
|
|
|
|
DoTexOrSubImage(isSubImage, gl, target.get(), level, dui, xOffset,
|
2016-12-14 01:51:06 +03:00
|
|
|
yOffset, zOffset, mWidth, mHeight, mDepth, dstBegin);
|
2016-06-01 05:17:52 +03:00
|
|
|
|
2016-12-14 01:51:06 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackAlignment);
|
2016-12-14 01:51:06 +03:00
|
|
|
if (webgl->IsWebGL2()) {
|
2016-07-12 22:43:39 +03:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH,
|
2020-01-09 01:19:14 +03:00
|
|
|
webgl->mPixelStore.mUnpackRowLength);
|
2016-07-12 22:43:39 +03:00
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-06-22 20:28:11 +03:00
|
|
|
return true;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace webgl
|
|
|
|
} // namespace mozilla
|