Remove some boolean flags in tex_image_2d and tex_sub_image_2d

This commit is contained in:
Anthony Ramine 2018-11-15 15:22:05 +01:00
parent 3f8a3b2887
commit 947e5afa0c
2 changed files with 89 additions and 30 deletions

View file

@ -757,6 +757,24 @@ impl TexDataType {
} }
} }
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq)]
pub enum AlphaTreatment {
Premultiply,
Unmultiply,
}
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq)]
pub enum YAxisTreatment {
AsIs,
Flipped,
}
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq)]
pub enum TexSource {
FromHtmlElement,
FromArray,
}
/// Translates an image in rgba8 (red in the first byte) format to /// Translates an image in rgba8 (red in the first byte) format to
/// the format that was requested of TexImage. /// the format that was requested of TexImage.
pub fn rgba8_image_to_tex_image_data( pub fn rgba8_image_to_tex_image_data(

View file

@ -6,10 +6,10 @@
use backtrace::Backtrace; use backtrace::Backtrace;
use canvas_traits::webgl::WebGLError::*; use canvas_traits::webgl::WebGLError::*;
use canvas_traits::webgl::{ use canvas_traits::webgl::{
self, webgl_channel, DOMToTextureCommand, Parameter, TexDataType, TexFormat, TexParameter, self, webgl_channel, AlphaTreatment, DOMToTextureCommand, Parameter, TexDataType, TexFormat,
WebGLCommand, WebGLCommandBacktrace, WebGLContextShareMode, WebGLError, TexParameter, TexSource, WebGLCommand, WebGLCommandBacktrace, WebGLContextShareMode,
WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLProgramId, WebGLResult, WebGLError, WebGLFramebufferBindingRequest, WebGLMsg, WebGLMsgSender, WebGLProgramId,
WebGLSLVersion, WebGLSender, WebGLVersion, WebVRCommand, WebGLResult, WebGLSLVersion, WebGLSender, WebGLVersion, WebVRCommand, YAxisTreatment,
}; };
use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::ANGLEInstancedArraysConstants; use crate::dom::bindings::codegen::Bindings::ANGLEInstancedArraysBinding::ANGLEInstancedArraysConstants;
use crate::dom::bindings::codegen::Bindings::EXTBlendMinmaxBinding::EXTBlendMinmaxConstants; use crate::dom::bindings::codegen::Bindings::EXTBlendMinmaxBinding::EXTBlendMinmaxConstants;
@ -505,7 +505,7 @@ impl WebGLRenderingContext {
0, 0,
1, 1,
true, true,
true, TexSource::FromHtmlElement,
pixels, pixels,
); );
@ -645,28 +645,35 @@ impl WebGLRenderingContext {
width: u32, width: u32,
height: u32, height: u32,
unpacking_alignment: u32, unpacking_alignment: u32,
source_premultiplied: bool, alpha_treatment: Option<AlphaTreatment>,
source_from_image_or_canvas: bool, y_axis_treatment: YAxisTreatment,
tex_source: TexSource,
mut pixels: Vec<u8>, mut pixels: Vec<u8>,
) -> Vec<u8> { ) -> Vec<u8> {
let settings = self.texture_unpacking_settings.get(); match alpha_treatment {
let dest_premultiply = settings.contains(TextureUnpacking::PREMULTIPLY_ALPHA); Some(AlphaTreatment::Premultiply) => {
if !source_premultiplied && dest_premultiply { if tex_source == TexSource::FromHtmlElement {
if source_from_image_or_canvas { webgl::premultiply_inplace(
// When the pixels come from image or canvas or imagedata, use RGBA8 format TexFormat::RGBA,
webgl::premultiply_inplace(TexFormat::RGBA, TexDataType::UnsignedByte, &mut pixels); TexDataType::UnsignedByte,
&mut pixels,
);
} else { } else {
webgl::premultiply_inplace(internal_format, data_type, &mut pixels); webgl::premultiply_inplace(internal_format, data_type, &mut pixels);
} }
} else if source_premultiplied && !dest_premultiply { },
Some(AlphaTreatment::Unmultiply) => {
assert_eq!(tex_source, TexSource::FromHtmlElement);
webgl::unmultiply_inplace(&mut pixels); webgl::unmultiply_inplace(&mut pixels);
},
None => {},
} }
if source_from_image_or_canvas { if tex_source == TexSource::FromHtmlElement {
pixels = webgl::rgba8_image_to_tex_image_data(internal_format, data_type, pixels); pixels = webgl::rgba8_image_to_tex_image_data(internal_format, data_type, pixels);
} }
if settings.contains(TextureUnpacking::FLIP_Y_AXIS) { if y_axis_treatment == YAxisTreatment::Flipped {
// FINISHME: Consider doing premultiply and flip in a single mutable Vec. // FINISHME: Consider doing premultiply and flip in a single mutable Vec.
pixels = webgl::flip_pixels_y( pixels = webgl::flip_pixels_y(
internal_format, internal_format,
@ -693,17 +700,33 @@ impl WebGLRenderingContext {
_border: u32, _border: u32,
unpacking_alignment: u32, unpacking_alignment: u32,
source_premultiplied: bool, source_premultiplied: bool,
source_from_image_or_canvas: bool, tex_source: TexSource,
pixels: Vec<u8>, pixels: Vec<u8>,
) { ) {
let settings = self.texture_unpacking_settings.get();
let dest_premultiplied = settings.contains(TextureUnpacking::PREMULTIPLY_ALPHA);
let alpha_treatment = match (source_premultiplied, dest_premultiplied) {
(true, false) => Some(AlphaTreatment::Unmultiply),
(false, true) => Some(AlphaTreatment::Premultiply),
_ => None,
};
let y_axis_treatment = if settings.contains(TextureUnpacking::FLIP_Y_AXIS) {
YAxisTreatment::Flipped
} else {
YAxisTreatment::AsIs
};
let pixels = self.prepare_pixels( let pixels = self.prepare_pixels(
internal_format, internal_format,
data_type, data_type,
width, width,
height, height,
unpacking_alignment, unpacking_alignment,
source_premultiplied, alpha_treatment,
source_from_image_or_canvas, y_axis_treatment,
tex_source,
pixels, pixels,
); );
@ -760,17 +783,35 @@ impl WebGLRenderingContext {
data_type: TexDataType, data_type: TexDataType,
unpacking_alignment: u32, unpacking_alignment: u32,
source_premultiplied: bool, source_premultiplied: bool,
source_from_image_or_canvas: bool, tex_source: TexSource,
pixels: Vec<u8>, pixels: Vec<u8>,
) { ) {
let settings = self.texture_unpacking_settings.get();
let alpha_treatment = match (
source_premultiplied,
settings.contains(TextureUnpacking::PREMULTIPLY_ALPHA),
) {
(true, false) => Some(AlphaTreatment::Unmultiply),
(false, true) => Some(AlphaTreatment::Premultiply),
_ => None,
};
let y_axis_treatment = if settings.contains(TextureUnpacking::FLIP_Y_AXIS) {
YAxisTreatment::Flipped
} else {
YAxisTreatment::AsIs
};
let pixels = self.prepare_pixels( let pixels = self.prepare_pixels(
format, format,
data_type, data_type,
width, width,
height, height,
unpacking_alignment, unpacking_alignment,
source_premultiplied, alpha_treatment,
source_from_image_or_canvas, y_axis_treatment,
tex_source,
pixels, pixels,
); );
@ -3677,7 +3718,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
border, border,
unpacking_alignment, unpacking_alignment,
false, false,
false, TexSource::FromArray,
buff, buff,
); );
@ -3752,7 +3793,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
border, border,
1, 1,
pixels.premultiplied, pixels.premultiplied,
true, TexSource::FromHtmlElement,
pixels.data, pixels.data,
); );
Ok(()) Ok(())
@ -3888,7 +3929,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
data_type, data_type,
unpacking_alignment, unpacking_alignment,
false, false,
false, TexSource::FromArray,
buff, buff,
); );
Ok(()) Ok(())
@ -3945,7 +3986,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
data_type, data_type,
1, 1,
pixels.premultiplied, pixels.premultiplied,
true, TexSource::FromHtmlElement,
pixels.data, pixels.data,
); );
Ok(()) Ok(())