texture.dart 3.31 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'box.dart';
import 'layer.dart';
import 'object.dart';

/// A rectangle upon which a backend texture is mapped.
///
/// Backend textures are images that can be applied (mapped) to an area of the
/// Flutter view. They are created, managed, and updated using a
/// platform-specific texture registry. This is typically done by a plugin
/// that integrates with host platform video player, camera, or OpenGL APIs,
/// or similar image sources.
///
/// A texture box refers to its backend texture using an integer ID. Texture
/// IDs are obtained from the texture registry and are scoped to the Flutter
/// view. Texture IDs may be reused after deregistration, at the discretion
/// of the registry. The use of texture IDs currently unknown to the registry
/// will silently result in a blank rectangle.
///
/// Texture boxes are repainted autonomously as dictated by the backend (e.g. on
/// arrival of a video frame). Such repainting generally does not involve
/// executing Dart code.
///
/// The size of the rectangle is determined by the parent, and the texture is
/// automatically scaled to fit.
///
/// See also:
///
32
///  * <https://api.flutter.dev/javadoc/io/flutter/view/TextureRegistry.html>
33
///    for how to create and manage backend textures on Android.
34
///  * <https://api.flutter.dev/objcdoc/Protocols/FlutterTextureRegistry.html>
35
///    for how to create and manage backend textures on iOS.
36
class TextureBox extends RenderBox {
37 38 39
  /// Creates a box backed by the texture identified by [textureId], and use
  /// [filterQuality] to set texture's [FilterQuality].
  TextureBox({
40
    required int textureId,
41
    bool freeze = false,
42 43 44
    FilterQuality filterQuality = FilterQuality.low,
  }) : assert(textureId != null),
      _textureId = textureId,
45
      _freeze = freeze,
46
      _filterQuality = filterQuality;
47 48 49 50 51 52 53 54 55 56 57 58

  /// The identity of the backend texture.
  int get textureId => _textureId;
  int _textureId;
  set textureId(int value) {
    assert(value != null);
    if (value != _textureId) {
      _textureId = value;
      markNeedsPaint();
    }
  }

59 60 61 62 63 64 65 66 67 68 69
  /// When true the texture will not be updated with new frames.
  bool get freeze => _freeze;
  bool _freeze;
  set freeze(bool value) {
    assert(value != null);
    if (value != _freeze) {
      _freeze = value;
      markNeedsPaint();
    }
  }

70
  /// {@macro flutter.widgets.Texture.filterQuality}
71 72 73 74
  FilterQuality get filterQuality => _filterQuality;
  FilterQuality _filterQuality;
  set filterQuality(FilterQuality value) {
    assert(value != null);
75 76 77 78
    if (value != _filterQuality) {
      _filterQuality = value;
      markNeedsPaint();
    }
79 80
  }

81 82 83 84 85 86 87 88 89 90
  @override
  bool get sizedByParent => true;

  @override
  bool get alwaysNeedsCompositing => true;

  @override
  bool get isRepaintBoundary => true;

  @override
91 92
  Size computeDryLayout(BoxConstraints constraints) {
    return constraints.biggest;
93 94 95
  }

  @override
96
  bool hitTestSelf(Offset position) => true;
97 98 99

  @override
  void paint(PaintingContext context, Offset offset) {
100 101
    context.addLayer(TextureLayer(
      rect: Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height),
102
      textureId: _textureId,
103
      freeze: freeze,
104
      filterQuality: _filterQuality,
105 106 107
    ));
  }
}