Skip to content

Commit

Permalink
Implement ReadPixels
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed May 13, 2016
1 parent 03465ad commit 2f416ba
Show file tree
Hide file tree
Showing 46 changed files with 903 additions and 40 deletions.
42 changes: 34 additions & 8 deletions components/script/dom/webglrenderingcontext.rs
Expand Up @@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderi
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{WebGLRenderingContextMethods};
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::{self, WebGLContextAttributes};
use dom::bindings::codegen::UnionTypes::ImageDataOrHTMLImageElementOrHTMLCanvasElementOrHTMLVideoElement;
use dom::bindings::conversions::{ToJSValConvertible, array_buffer_view_data_checked};
use dom::bindings::conversions::{ToJSValConvertible, array_buffer_view_data, array_buffer_view_data_checked};
use dom::bindings::conversions::{array_buffer_view_to_vec_checked, array_buffer_view_to_vec};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
Expand All @@ -28,7 +28,7 @@ use dom::webgltexture::{TexParameterValue, WebGLTexture};
use dom::webgluniformlocation::WebGLUniformLocation;
use euclid::size::Size2D;
use ipc_channel::ipc::{self, IpcSender};
use js::jsapi::{JSContext, JSObject, RootedValue};
use js::jsapi::{JSContext, JS_GetArrayBufferViewType, JSObject, RootedValue, Type};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UndefinedValue};
use net_traits::image::base::PixelFormat;
use net_traits::image_cache_thread::ImageResponse;
Expand Down Expand Up @@ -471,8 +471,8 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
WebGLParameter::Int(val) => Int32Value(val),
WebGLParameter::Bool(_) => panic!("Buffer parameter should not be bool"),
WebGLParameter::Float(_) => panic!("Buffer parameter should not be float"),
WebGLParameter::String(_) => panic!("Buffer parameter should not be string"),
WebGLParameter::FloatArray(_) => panic!("Buffer parameter should not be float array"),
WebGLParameter::String(_) => panic!("Buffer parameter should not be string"),
WebGLParameter::Invalid => NullValue(),
}
}
Expand Down Expand Up @@ -1238,6 +1238,36 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
.unwrap()
}

#[allow(unsafe_code)]
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.12
fn ReadPixels(&self, _cx: *mut JSContext, x: i32, y: i32, width: i32, height: i32,
format: u32, pixel_type: u32, pixels: *mut JSObject) {
let mut data = match unsafe { array_buffer_view_data::<u8>(pixels) } {
Some(data) => data,
None => return self.webgl_error(InvalidValue),
};

match unsafe { JS_GetArrayBufferViewType(pixels) } {
Type::Uint8 => (),
_ => return self.webgl_error(InvalidOperation)
}

let (sender, receiver) = ipc::channel().unwrap();
self.ipc_renderer
.send(CanvasMsg::WebGL(WebGLCommand::ReadPixels(x, y, width, height, format, pixel_type, sender)))
.unwrap();

let result = receiver.recv().unwrap();

if result.len() > data.len() {
return self.webgl_error(InvalidOperation)
}

for i in 0..result.len() {
data[i] = result[i]
}
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.4
fn Scissor(&self, x: i32, y: i32, width: i32, height: i32) {
self.ipc_renderer
Expand Down Expand Up @@ -1337,11 +1367,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.9
fn GetShaderSource(&self, shader: Option<&WebGLShader>) -> Option<DOMString> {
if let Some(shader) = shader {
shader.source()
} else {
None
}
shader.and_then(|s| s.source())
}

// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.10
Expand Down
2 changes: 2 additions & 0 deletions components/script/dom/webidls/WebGLRenderingContext.webidl
Expand Up @@ -614,6 +614,8 @@ interface WebGLRenderingContextBase

//void readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
// GLenum format, GLenum type, ArrayBufferView? pixels);
void readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type, object? pixels);

//void renderbufferStorage(GLenum target, GLenum internalformat,
// GLsizei width, GLsizei height);
Expand Down
Expand Up @@ -3,3 +3,9 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #15: at (0, 0) expected: 0,255,0,255 was 255,255,255,255]
expected: FAIL

[WebGL test #16: getError expected: NO_ERROR. Was INVALID_ENUM : should be no errors]
expected: FAIL

Expand Up @@ -3,3 +3,6 @@
[WebGL test #3: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #4: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

Expand Up @@ -6,3 +6,6 @@
[WebGL test #1: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #1: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

Expand Up @@ -6,3 +6,6 @@
[WebGL test #0: getError expected: NO_ERROR. Was INVALID_OPERATION : Should be no errors from setup.]
expected: FAIL

[WebGL test #4: at (0, 0) expected: 32,64,127,255 was 255,128,64,255]
expected: FAIL

This file was deleted.

@@ -1,5 +1,6 @@
[readPixelsBadArgs.html]
type: testharness
expected: CRASH
[WebGL test #0: testReadPixels]
expected: FAIL

Expand Up @@ -3,3 +3,9 @@
[WebGL test #4: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #4: at (0, 0) expected: 0,255,0,255 was 255,255,255,255]
expected: FAIL

[WebGL test #5: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

Expand Up @@ -3,3 +3,9 @@
[WebGL test #6: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #7: at (20, 15) expected: 0,255,0,255 was 0,0,0,255]
expected: FAIL

[WebGL test #9: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

@@ -1,5 +1,6 @@
[read-pixels-pack-alignment.html]
type: testharness
expected: CRASH
[WebGL test #3: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

@@ -1,3 +1,3 @@
[read-pixels-test.html]
type: testharness
expected: TIMEOUT
expected: CRASH
Expand Up @@ -3,3 +3,6 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #1: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

Expand Up @@ -3,3 +3,24 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #0: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #1: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #2: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #4: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #7: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #9: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #10: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

Expand Up @@ -3,3 +3,60 @@
[WebGL test #2: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #50: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #53: at (1, 1) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #56: at (2, 2) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #59: at (3, 3) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #62: at (4, 4) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #65: at (5, 5) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #68: at (6, 6) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #71: at (7, 7) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #74: at (8, 8) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #77: at (9, 9) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #80: at (10, 10) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #83: at (11, 11) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #86: at (12, 12) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #89: at (13, 13) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #92: at (14, 14) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #95: at (15, 15) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #97: getError expected: NO_ERROR. Was INVALID_ENUM : there should be no errors]
expected: FAIL

[WebGL test #98: Unable to fetch WebGL rendering context for Canvas]
expected: FAIL

[WebGL test #99: context does not exist]
expected: FAIL

Expand Up @@ -3,3 +3,48 @@
[WebGL test #1: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #1: at (16, 32) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #6: at (8, 8) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #11: at (4, 16) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #16: at (32, 64) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #41: at (32, 96) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #46: at (24, 72) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #51: at (36, 48) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #56: at (48, 96) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #61: at (0, 32) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #66: at (8, 0) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #71: at (0, 16) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #76: at (32, 0) expected: 0,0,255,255 was 0,0,0,0]
expected: FAIL

[WebGL test #81: getError expected: NO_ERROR. Was INVALID_ENUM : there should be no errors]
expected: FAIL

[WebGL test #82: Unable to fetch WebGL rendering context for Canvas]
expected: FAIL

[WebGL test #83: context does not exist]
expected: FAIL

Expand Up @@ -3,3 +3,6 @@
[WebGL test #1: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #1: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

Expand Up @@ -3,3 +3,30 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #0: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #1: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #2: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #3: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #4: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #5: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #6: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #7: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #8: at (0, 0) expected: 0,255,0,255 was 0,0,0,0]
expected: FAIL

Expand Up @@ -3,3 +3,6 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #2: getError expected: NO_ERROR. Was INVALID_ENUM : there should be no errors]
expected: FAIL

@@ -1,6 +1,5 @@
[state-uneffected-after-compositing.html]
type: testharness
expected: TIMEOUT
[WebGL test #0: Unable to fetch WebGL rendering context for Canvas]
expected: FAIL

Expand Down
Expand Up @@ -3,3 +3,9 @@
[WebGL test #0: successfullyParsed should be true (of type boolean). Was undefined (of type undefined).]
expected: FAIL

[WebGL test #0: at (0, 0) expected: 0,0,0,255 was 0,0,0,0]
expected: FAIL

[WebGL test #0: at (0, 0) expected: 0,0,0,255 was 64,255,191,128]
expected: FAIL

Expand Up @@ -9,3 +9,12 @@
[WebGL test #1: successfullyParsed should be true. Was false.]
expected: FAIL

[WebGL test #0: at (0, 0) expected: 0,255,0,255 was 255,0,0,255]
expected: FAIL

[WebGL test #1: at (0, 8) expected: 255,0,0,255 was 0,255,0,255]
expected: FAIL

[WebGL test #2: successfullyParsed should be true. Was false.]
expected: FAIL

@@ -1,3 +1,5 @@
[tex-image-and-sub-image-2d-with-canvas-rgb565.html]
type: testharness
expected: TIMEOUT
[WebGL test #0: at (0, 16) expected: 255,0,0 was 255,227,0]
expected: FAIL

@@ -1,3 +1,5 @@
[tex-image-and-sub-image-2d-with-canvas-rgba4444.html]
type: testharness
expected: TIMEOUT
[WebGL test #0: at (0, 16) expected: 255,0,0 was 255,255,0]
expected: FAIL

0 comments on commit 2f416ba

Please sign in to comment.