Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix flipped image data on webgl1
  • Loading branch information
ferhatb committed Oct 15, 2020
commit 054aea64519e53dbaa6ca884e8014e79d5c2d52a
18 changes: 15 additions & 3 deletions lib/web_ui/lib/src/engine/html/render_vertices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class _GlContext {
dynamic _kUnsignedByte;
dynamic _kUnsignedShort;
dynamic _kRGBA;
dynamic _kUnpackFlipYWebGl;
Object? _canvas;
int? _widthInPixels;
int? _heightInPixels;
Expand Down Expand Up @@ -659,6 +660,9 @@ class _GlContext {
dynamic get kColorBufferBit =>
_kColorBufferBit ??= js_util.getProperty(glContext, 'COLOR_BUFFER_BIT');

dynamic get kUnpackFlipYWebGl =>
_kUnpackFlipYWebGl ??= js_util.getProperty(glContext, 'UNPACK_FLIP_Y_WEBGL');

/// Returns reference to uniform in program.
Object getUniformLocation(Object program, String uniformName) {
Object? res = js_util
Expand Down Expand Up @@ -711,14 +715,23 @@ class _GlContext {
int? get drawingBufferHeight =>
js_util.getProperty(glContext, 'drawingBufferWidth');

/// Flips the y axis when uploading a texture (for webgl1).
void pixelStoreFlip(bool flip) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

js_util.callMethod(glContext, 'pixelStorei',
<dynamic>[kUnpackFlipYWebGl, flip]);
}

/// Reads gl contents as image data.
///
/// Warning: data is read bottom up (flipped).
html.ImageData readImageData() {
if (browserEngine == BrowserEngine.webkit ||
browserEngine == BrowserEngine.firefox) {
const int kBytesPerPixel = 4;
final int bufferWidth = _widthInPixels!;
final int bufferHeight = _heightInPixels!;
final Uint8List pixels =
Uint8List(bufferWidth * bufferHeight * kBytesPerPixel);
Uint8List(bufferWidth * bufferHeight * kBytesPerPixel);
js_util.callMethod(glContext, 'readPixels',
<dynamic>[0, 0, bufferWidth, bufferHeight, kRGBA, kUnsignedByte, pixels]);
return html.ImageData(
Expand Down Expand Up @@ -748,10 +761,9 @@ class _GlContext {
<dynamic>[]);
return imageBitmap;
} else {
html.ImageData imageData = readImageData();
html.CanvasElement canvas = html.CanvasElement(width: _widthInPixels, height: _heightInPixels);
final html.CanvasRenderingContext2D ctx = canvas.context2D;
ctx.putImageData(imageData, 0 , 0);
drawImage(ctx, 0, 0);
return canvas;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/html/shaders/shader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GradientSweep extends EngineGradient {
_GlContext gl = _OffScreenCanvas.supported
? _GlContext.fromOffscreenCanvas(offScreenCanvas._canvas!)
: _GlContext.fromCanvas(offScreenCanvas._glCanvas!,
(browserEngine == BrowserEngine.webkit));
webGLVersion == 1);
gl.setViewportSize(widthInPixels, heightInPixels);

NormalizedGradient normalizedGradient = NormalizedGradient(
Expand Down