raster method

Image? raster(
  1. int id,
  2. Size logicalSize,
  3. double devicePixelRatio,
  4. void record(
    1. Canvas canvas
    ), {
  5. Offset origin = ui.Offset.zero,
})

返回物理像素位图;picture 模式、零尺寸或边长/共享预算超限时返回 null。

origin 是逻辑坐标中的左上角;例如负坐标球体可传其包围盒原点。

Implementation

ui.Image? raster(int id, ui.Size logicalSize, double devicePixelRatio,
    void Function(ui.Canvas canvas) record,
    {ui.Offset origin = ui.Offset.zero}) {
  luckyTraceStart('luckdraw.raster.build');
  try {
    final width = (logicalSize.width * devicePixelRatio).ceil();
    final height = (logicalSize.height * devicePixelRatio).ceil();
    if (mode == LuckyCacheMode.picture ||
        width <= 0 ||
        height <= 0 ||
        width > maxRasterSide ||
        height > maxRasterSide) {
      return null;
    }
    final old = _rasters[id];
    if (maxRasterBytes > 0 &&
        old != null &&
        old.$2 == width &&
        old.$3 == height &&
        old.$4 == devicePixelRatio &&
        old.$5 == origin) {
      return old.$1;
    }
    final bytes = width * height * kLuckyRgbaBytesPerPixel;
    if (!LuckyRasterBudget.instance.tryReserve(bytes, maxRasterBytes)) {
      return null;
    }
    var retained = false;
    try {
      final source = picture(id, record);
      final recorder = ui.PictureRecorder();
      final canvas = ui.Canvas(recorder)
        ..scale(devicePixelRatio)
        ..translate(-origin.dx, -origin.dy);
      canvas.drawPicture(source);
      final scaled = recorder.endRecording();
      try {
        final result = scaled.toImageSync(width, height);
        _disposeRaster(old);
        _rasters[id] = (result, width, height, devicePixelRatio, origin);
        retained = true;
        return result;
      } finally {
        scaled.dispose();
      }
    } finally {
      if (!retained) LuckyRasterBudget.instance.release(bytes);
    }
  } finally {
    luckyTraceEnd();
  }
}