Skip to content

Commit

Permalink
Move parts to libraries (flutter#26054)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatb authored May 10, 2021
1 parent 5d3dde5 commit 790457a
Show file tree
Hide file tree
Showing 6 changed files with 605 additions and 529 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/clip.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/color_filter.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/debug_canvas_reuse_overlay.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/image_filter.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/offscreen_canvas.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/offset.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/opacity.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/painting.dart
Expand All @@ -518,6 +519,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/normalized_gradien
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/shader.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/shader_builder.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/vertex_shaders.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/shaders/webgl_context.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/surface.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/surface_stats.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/transform.dart
Expand Down
6 changes: 6 additions & 0 deletions lib/web_ui/lib/src/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export 'engine/browser_detection.dart';
import 'engine/html_image_codec.dart';
export 'engine/html_image_codec.dart';

import 'engine/html/offscreen_canvas.dart';
export 'engine/html/offscreen_canvas.dart';

import 'engine/html/painting.dart';
export 'engine/html/painting.dart';

Expand Down Expand Up @@ -83,6 +86,9 @@ export 'engine/html/shaders/shader_builder.dart';
import 'engine/html/shaders/vertex_shaders.dart';
export 'engine/html/shaders/vertex_shaders.dart';

import 'engine/html/shaders/webgl_context.dart';
export 'engine/html/shaders/webgl_context.dart';

import 'engine/mouse_cursor.dart';
export 'engine/mouse_cursor.dart';

Expand Down
98 changes: 98 additions & 0 deletions lib/web_ui/lib/src/engine/html/offscreen_canvas.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:html' as html;
import 'dart:js_util' as js_util;
import 'package:ui/src/engine.dart';

/// Polyfill for html.OffscreenCanvas that is not supported on some browsers.
class OffScreenCanvas {
html.OffscreenCanvas? offScreenCanvas;
html.CanvasElement? canvasElement;
int width;
int height;
static bool? _supported;

OffScreenCanvas(this.width, this.height) {
if (OffScreenCanvas.supported) {
offScreenCanvas = html.OffscreenCanvas(width, height);
} else {
canvasElement = html.CanvasElement(
width: width,
height: height,
);
canvasElement!.className = 'gl-canvas';
final double cssWidth = width / EnginePlatformDispatcher.browserDevicePixelRatio;
final double cssHeight = height / EnginePlatformDispatcher.browserDevicePixelRatio;
canvasElement!.style
..position = 'absolute'
..width = '${cssWidth}px'
..height = '${cssHeight}px';
}
}

void dispose() {
offScreenCanvas = null;
canvasElement = null;
}

/// Returns CanvasRenderContext2D or OffscreenCanvasRenderingContext2D to
/// paint into.
Object? getContext2d() {
return (offScreenCanvas != null
? offScreenCanvas!.getContext('2d')
: canvasElement!.getContext('2d'));
}

/// Feature detection for transferToImageBitmap on OffscreenCanvas.
bool get transferToImageBitmapSupported =>
js_util.hasProperty(offScreenCanvas!, 'transferToImageBitmap');

/// Creates an ImageBitmap object from the most recently rendered image
/// of the OffscreenCanvas.
///
/// !Warning API still in experimental status, feature detect before using.
Object? transferToImageBitmap() {
return js_util.callMethod(offScreenCanvas!, 'transferToImageBitmap',
<dynamic>[]);
}

/// Draws canvas contents to a rendering context.
void transferImage(Object targetContext) {
// Actual size of canvas may be larger than viewport size. Use
// source/destination to draw part of the image data.
js_util.callMethod(targetContext, 'drawImage',
<dynamic>[offScreenCanvas ?? canvasElement!, 0, 0, width, height,
0, 0, width, height]);
}

/// Converts canvas contents to an image and returns as data url.
Future<String> toDataUrl() {
final Completer<String> completer = Completer<String>();
if (offScreenCanvas != null) {
offScreenCanvas!.convertToBlob().then((html.Blob value) {
final fileReader = html.FileReader();
fileReader.onLoad.listen((event) {
completer.complete(js_util.getProperty(
js_util.getProperty(event, 'target')!, 'result')!);
});
fileReader.readAsDataUrl(value);
});
return completer.future;
} else {
return Future.value(canvasElement!.toDataUrl());
}
}

/// Draws an image to canvas for both offscreen canvas canvas context2d.
void drawImage(Object image, int x, int y, int width, int height) {
js_util.callMethod(
getContext2d()!, 'drawImage', <dynamic>[image, x, y, width, height]);
}

/// Feature detects OffscreenCanvas.
static bool get supported => _supported ??=
js_util.hasProperty(html.window, 'OffscreenCanvas');
}
Loading

0 comments on commit 790457a

Please sign in to comment.