import from UXP: Issue #3042 - Add canvas WebGL context powerPreference (546e0075)

This commit is contained in:
2026-04-10 23:19:11 +08:00
parent 39391a8776
commit 2f20f30dba
4 changed files with 46 additions and 32 deletions
+27 -8
View File
@@ -95,19 +95,26 @@ using namespace mozilla::gl;
using namespace mozilla::layers;
WebGLContextOptions::WebGLContextOptions()
: alpha(true)
, depth(true)
, stencil(false)
, premultipliedAlpha(true)
, antialias(true)
, preserveDrawingBuffer(false)
, failIfMajorPerformanceCaveat(false)
{
// Set default alpha state based on preference.
if (gfxPrefs::WebGLDefaultNoAlpha())
alpha = false;
}
bool
WebGLContextOptions::operator==(const WebGLContextOptions& r) const
{
bool eq = true;
eq &= (alpha == r.alpha);
eq &= (depth == r.depth);
eq &= (stencil == r.stencil);
eq &= (premultipliedAlpha == r.premultipliedAlpha);
eq &= (antialias == r.antialias);
eq &= (preserveDrawingBuffer == r.preserveDrawingBuffer);
eq &= (failIfMajorPerformanceCaveat == r.failIfMajorPerformanceCaveat);
eq &= (powerPreference == r.powerPreference);
return eq;
}
/*static*/ const uint32_t WebGLContext::kMinMaxColorAttachments = 4;
/*static*/ const uint32_t WebGLContext::kMinMaxDrawBuffers = 4;
@@ -385,6 +392,7 @@ WebGLContext::SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options,
newOpts.antialias = attributes.mAntialias;
newOpts.preserveDrawingBuffer = attributes.mPreserveDrawingBuffer;
newOpts.failIfMajorPerformanceCaveat = attributes.mFailIfMajorPerformanceCaveat;
newOpts.powerPreference = attributes.mPowerPreference;
if (attributes.mAlpha.WasPassed())
newOpts.alpha = attributes.mAlpha.Value();
@@ -403,7 +411,7 @@ WebGLContext::SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options,
newOpts.preserveDrawingBuffer ? 1 : 0);
#endif
if (mOptionsFrozen && newOpts != mOptions) {
if (mOptionsFrozen && !(newOpts == mOptions)) {
// Error if the options are already frozen, and the ones that were asked for
// aren't the same as what they were originally.
return NS_ERROR_FAILURE;
@@ -726,6 +734,16 @@ WebGLContext::CreateAndInitGL(bool forceEnabled,
flags |= gl::CreateContextFlags::REQUIRE_COMPAT_PROFILE;
}
switch (mOptions.powerPreference) {
case dom::WebGLPowerPreference::Low_power:
break;
case dom::WebGLPowerPreference::High_performance:
default:
// Default to high-performance if queried.
flags |= gl::CreateContextFlags::HIGH_POWER;
break;
}
//////
const bool useEGL = PR_GetEnv("MOZ_WEBGL_FORCE_EGL");
@@ -1443,6 +1461,7 @@ WebGLContext::GetContextAttributes(dom::Nullable<dom::WebGLContextAttributes>& r
result.mPremultipliedAlpha = mOptions.premultipliedAlpha;
result.mPreserveDrawingBuffer = mOptions.preserveDrawingBuffer;
result.mFailIfMajorPerformanceCaveat = mOptions.failIfMajorPerformanceCaveat;
result.mPowerPreference = mOptions.powerPreference;
}
NS_IMETHODIMP
+10 -23
View File
@@ -137,30 +137,17 @@ void AssertUintParamCorrect(gl::GLContext* gl, GLenum pname, GLuint shadow);
struct WebGLContextOptions
{
// these are defaults
bool alpha = true;
bool depth = true;
bool stencil = false;
bool premultipliedAlpha = true;
bool antialias = true;
bool preserveDrawingBuffer = false;
bool failIfMajorPerformanceCaveat = false;
dom::WebGLPowerPreference powerPreference = dom::WebGLPowerPreference::Default;
WebGLContextOptions();
bool operator==(const WebGLContextOptions& other) const {
return
alpha == other.alpha &&
depth == other.depth &&
stencil == other.stencil &&
premultipliedAlpha == other.premultipliedAlpha &&
antialias == other.antialias &&
preserveDrawingBuffer == other.preserveDrawingBuffer;
}
bool operator!=(const WebGLContextOptions& other) const {
return !operator==(other);
}
bool alpha;
bool depth;
bool stencil;
bool premultipliedAlpha;
bool antialias;
bool preserveDrawingBuffer;
bool failIfMajorPerformanceCaveat;
bool operator==(const WebGLContextOptions&) const;
};
// From WebGLContextUtils
+5
View File
@@ -32,6 +32,10 @@ typedef unrestricted float GLfloat;
typedef unrestricted float GLclampf;
typedef unsigned long long GLuint64EXT;
// The power preference settings are documented in the WebGLContextAttributes
// section of the specification.
enum WebGLPowerPreference { "default", "low-power", "high-performance" };
dictionary WebGLContextAttributes {
// boolean alpha = true;
// We deviate from the spec here.
@@ -43,6 +47,7 @@ dictionary WebGLContextAttributes {
GLboolean premultipliedAlpha = true;
GLboolean preserveDrawingBuffer = false;
GLboolean failIfMajorPerformanceCaveat = false;
WebGLPowerPreference powerPreference = "default";
};
[Exposed=(Window,Worker),
+4 -1
View File
@@ -45,7 +45,7 @@ struct GLFormats
GLsizei samples;
};
enum class CreateContextFlags : int8_t {
enum class CreateContextFlags : uint8_t {
NONE = 0,
REQUIRE_COMPAT_PROFILE = 1 << 0,
// Force the use of hardware backed GL, don't allow software implementations.
@@ -56,6 +56,9 @@ enum class CreateContextFlags : int8_t {
PREFER_ES3 = 1 << 3,
NO_VALIDATION = 1 << 4,
//PREFER_ROBUSTNESS = 1 << 5, /* reserved for now */
HIGH_POWER = 1 << 6,
};
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(CreateContextFlags)