Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Add setImageSampler (for replacing setSampler) #37821

Merged
merged 11 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions lib/ui/dart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ typedef CanvasPath Path;
V(EngineLayer, dispose, 1) \
V(FragmentProgram, initFromAsset, 2) \
V(ReusableFragmentShader, Dispose, 1) \
V(ReusableFragmentShader, SetImageSampler, 3) \
V(ReusableFragmentShader, SetSampler, 3) \
V(ReusableFragmentShader, ValidateSamplers, 1) \
V(Gradient, initLinear, 6) \
Expand Down
15 changes: 15 additions & 0 deletions lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4314,6 +4314,18 @@ class FragmentShader extends Shader {
_floats[index] = value;
}

/// Sets the sampler uniform at [index] to [image].
///
/// The index provided to setImageSampler is the index of the sampler uniform defined
/// in the fragment program, excluding all non-sampler uniforms.
///
/// All the sampler uniforms that a shader expects must be provided or the
/// results will be undefined.
void setImageSampler(int index, Image image) {
assert(!debugDisposed, 'Tried to access uniforms on a disposed Shader: $this');
_setImageSampler(index, image._image);
}

/// Sets the sampler uniform at [index] to [sampler].
///
/// The index provided to setSampler is the index of the sampler uniform defined
Expand Down Expand Up @@ -4341,6 +4353,9 @@ class FragmentShader extends Shader {
@FfiNative<Handle Function(Handle, Handle, Handle, Handle)>('ReusableFragmentShader::Create')
external Float32List _constructor(FragmentProgram program, int floatUniforms, int samplerUniforms);

@FfiNative<Void Function(Pointer<Void>, Handle, Handle)>('ReusableFragmentShader::SetImageSampler')
external void _setImageSampler(int index, _Image sampler);

@FfiNative<Void Function(Pointer<Void>, Handle, Handle)>('ReusableFragmentShader::SetSampler')
external void _setSampler(int index, ImageShader sampler);

Expand Down
23 changes: 23 additions & 0 deletions lib/ui/painting/fragment_shader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "flutter/lib/ui/painting/fragment_shader.h"

#include "flutter/display_list/display_list_color_source.h"
#include "flutter/display_list/display_list_tile_mode.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "flutter/lib/ui/painting/fragment_program.h"
#include "flutter/lib/ui/ui_dart_state.h"
Expand Down Expand Up @@ -61,6 +63,27 @@ bool ReusableFragmentShader::ValidateSamplers() {
return true;
}

void ReusableFragmentShader::SetImageSampler(Dart_Handle index_handle,
Dart_Handle image_handle) {
uint64_t index = tonic::DartConverter<uint64_t>::FromDart(index_handle);
CanvasImage* image =
tonic::DartConverter<CanvasImage*>::FromDart(image_handle);
if (index >= samplers_.size()) {
Dart_ThrowException(tonic::ToDart("Sampler index out of bounds"));
}

// TODO(115794): Once the DlImageSampling enum is replaced, expose the
// sampling options as a new default parameter for users.
samplers_[index] = std::make_shared<DlImageColorSource>(
image->image(), DlTileMode::kClamp, DlTileMode::kClamp,
DlImageSampling::kNearestNeighbor, nullptr);

auto* uniform_floats =
reinterpret_cast<float*>(uniform_data_->writable_data());
uniform_floats[float_count_ + 2 * index] = image->width();
uniform_floats[float_count_ + 2 * index + 1] = image->height();
}

void ReusableFragmentShader::SetSampler(Dart_Handle index_handle,
Dart_Handle sampler_handle) {
uint64_t index = tonic::DartConverter<uint64_t>::FromDart(index_handle);
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/fragment_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ReusableFragmentShader : public Shader {
Dart_Handle float_count,
Dart_Handle sampler_count);

void SetImageSampler(Dart_Handle index, Dart_Handle image);

void SetSampler(Dart_Handle index, Dart_Handle sampler);

bool ValidateSamplers();
Expand Down
2 changes: 2 additions & 0 deletions lib/web_ui/lib/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ abstract class FragmentProgram {
abstract class FragmentShader implements Shader {
void setFloat(int index, double value);

void setImageSampler(int index, Image image);

void setSampler(int index, ImageShader sampler);

@override
Expand Down
10 changes: 10 additions & 0 deletions lib/web_ui/lib/src/engine/canvaskit/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:typed_data';
import 'package:ui/ui.dart' as ui;

import '../color_filter.dart';
import '../vector_math.dart';
import 'canvaskit_api.dart';
import 'color_filter.dart';
import 'image_filter.dart';
Expand Down Expand Up @@ -476,6 +477,15 @@ class CkFragmentShader implements ui.FragmentShader {
floats[index] = value;
}

@override
void setImageSampler(int index, ui.Image image) {
final ui.ImageShader sampler = ui.ImageShader(image, ui.TileMode.clamp,
ui.TileMode.clamp, toMatrix64(Matrix4.identity().storage));
samplers[index] = (sampler as CkShader).skiaObject;
setFloat(lastFloatIndex + 2 * index, (sampler as CkImageShader).imageWidth.toDouble());
setFloat(lastFloatIndex + 2 * index + 1, sampler.imageHeight.toDouble());
}

@override
void setSampler(int index, ui.ImageShader sampler) {
samplers[index] = (sampler as CkShader).skiaObject;
Expand Down
5 changes: 5 additions & 0 deletions lib/web_ui/lib/src/engine/html/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ class HtmlFragmentShader implements ui.FragmentShader {
throw UnsupportedError('FragmentShader is not supported for the HTML renderer.');
}

@override
void setImageSampler(int index, ui.Image image) {
throw UnsupportedError('FragmentShader is not supported for the HTML renderer.');
}

@override
void setSampler(int index, ui.ImageShader sampler) {
throw UnsupportedError('FragmentShader is not supported for the HTML renderer.');
Expand Down