RenderModel constructor

RenderModel(
  1. Uint8List bytes
)

Wraps bytes; a view whose offset is not word-aligned is copied first, because the words are read through a Uint32List view. Both transports hand in fresh, aligned buffers, so the copy only happens for slices a host carved out of a larger buffer.

Implementation

factory RenderModel(Uint8List bytes) {
  if (bytes.offsetInBytes % 4 != 0) bytes = Uint8List.fromList(bytes);

  if (Endian.host != Endian.little) {
    throw UnsupportedError('flark: big-endian hosts are not supported');
  }
  if (bytes.lengthInBytes < RenderModelSchema.headerWords * 4) {
    throw const FormatException('render model shorter than its header');
  }
  final words = Uint32List.sublistView(
    bytes,
    0,
    bytes.lengthInBytes ~/ 4 * 4,
  );
  if (words[HeaderField.magic] != RenderModelSchema.magic) {
    throw const FormatException('render model magic mismatch');
  }
  if (words[HeaderField.version] != RenderModelSchema.version) {
    throw FormatException(
      'render model version ${words[HeaderField.version]}, expected ${RenderModelSchema.version}',
    );
  }
  final blocks =
      RenderModelSchema.headerWords +
      words[HeaderField.lineCount] * RenderModelSchema.lineWords;
  final content =
      blocks + words[HeaderField.blockCount] * RenderModelSchema.blockWords;
  final runs =
      content +
      words[HeaderField.contentCount] * RenderModelSchema.contentWords;
  final definitions =
      runs + words[HeaderField.runCount] * RenderModelSchema.runWords;
  final runExtras =
      definitions +
      words[HeaderField.definitionCount] * RenderModelSchema.definitionWords;
  final extras =
      runExtras +
      words[HeaderField.runExtraCount] * RenderModelSchema.runExtraWords;
  final strings = (extras + words[HeaderField.extraWords]) * 4;
  if (strings + words[HeaderField.stringBytes] > bytes.lengthInBytes) {
    throw const FormatException('render model truncated');
  }
  return RenderModel._(
    bytes.asUnmodifiableView(),
    words,
    blocks,
    content,
    runs,
    definitions,
    runExtras,
    extras,
    strings,
  );
}