Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fragment_shader.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6#include <utility>
7
8#include "flutter/lib/ui/painting/fragment_shader.h"
9
10#include "flutter/display_list/dl_tile_mode.h"
11#include "flutter/display_list/effects/dl_color_source.h"
12#include "flutter/lib/ui/dart_wrapper.h"
13#include "flutter/lib/ui/painting/fragment_program.h"
14#include "flutter/lib/ui/ui_dart_state.h"
21
22namespace flutter {
23
25
26ReusableFragmentShader::ReusableFragmentShader(
28 uint64_t float_count,
29 uint64_t sampler_count)
30 : program_(std::move(program)),
31 uniform_data_(SkData::MakeUninitialized(
32 (float_count + 2 * sampler_count) * sizeof(float))),
33 samplers_(sampler_count),
34 float_count_(float_count) {}
35
36Dart_Handle ReusableFragmentShader::Create(Dart_Handle wrapper,
37 Dart_Handle program,
38 Dart_Handle float_count_handle,
39 Dart_Handle sampler_count_handle) {
40 auto* fragment_program =
42 uint64_t float_count =
44 uint64_t sampler_count =
45 tonic::DartConverter<uint64_t>::FromDart(sampler_count_handle);
46
47 auto res = fml::MakeRefCounted<ReusableFragmentShader>(
48 fml::Ref(fragment_program), float_count, sampler_count);
49 res->AssociateWithDartWrapper(wrapper);
50
51 void* raw_uniform_data =
52 reinterpret_cast<void*>(res->uniform_data_->writable_data());
54 float_count);
55}
56
57bool ReusableFragmentShader::ValidateSamplers() {
58 for (auto i = 0u; i < samplers_.size(); i += 1) {
59 if (samplers_[i] == nullptr) {
60 return false;
61 }
62 // The samplers should have been checked as they were added, this
63 // is a double-sanity-check.
64 FML_DCHECK(samplers_[i]->isUIThreadSafe());
65 }
66 return true;
67}
68
69void ReusableFragmentShader::SetImageSampler(Dart_Handle index_handle,
70 Dart_Handle image_handle) {
71 uint64_t index = tonic::DartConverter<uint64_t>::FromDart(index_handle);
74 if (index >= samplers_.size()) {
75 Dart_ThrowException(tonic::ToDart("Sampler index out of bounds"));
76 }
77 if (!image->image()->isUIThreadSafe()) {
78 Dart_ThrowException(tonic::ToDart("Image is not thread-safe"));
79 }
80
81 // TODO(115794): Once the DlImageSampling enum is replaced, expose the
82 // sampling options as a new default parameter for users.
83 samplers_[index] = std::make_shared<DlImageColorSource>(
84 image->image(), DlTileMode::kClamp, DlTileMode::kClamp,
85 DlImageSampling::kNearestNeighbor, nullptr);
86 // This should be true since we already checked the image above, but
87 // we check again for sanity.
88 FML_DCHECK(samplers_[index]->isUIThreadSafe());
89
90 auto* uniform_floats =
91 reinterpret_cast<float*>(uniform_data_->writable_data());
92 uniform_floats[float_count_ + 2 * index] = image->width();
93 uniform_floats[float_count_ + 2 * index + 1] = image->height();
94}
95
96std::shared_ptr<DlColorSource> ReusableFragmentShader::shader(
97 DlImageSampling sampling) {
98 FML_CHECK(program_);
99
100 // The lifetime of this object is longer than a frame, and the uniforms can be
101 // continually changed on the UI thread. So we take a copy of the uniforms
102 // before handing it to the DisplayList for consumption on the render thread.
103 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
104 uniform_data->resize(uniform_data_->size());
105 memcpy(uniform_data->data(), uniform_data_->bytes(), uniform_data->size());
106
107 auto source = program_->MakeDlColorSource(std::move(uniform_data), samplers_);
108 // The samplers should have been checked as they were added, this
109 // is a double-sanity-check.
110 FML_DCHECK(source->isUIThreadSafe());
111 return source;
112}
113
114void ReusableFragmentShader::Dispose() {
115 uniform_data_.reset();
116 program_ = nullptr;
117 samplers_.clear();
118 ClearDartWrapper();
119}
120
121ReusableFragmentShader::~ReusableFragmentShader() = default;
122
123} // namespace flutter
void reset()
Definition SkBitmap.cpp:92
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
DART_EXPORT Dart_Handle Dart_NewExternalTypedData(Dart_TypedData_Type type, void *data, intptr_t length)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
@ Dart_TypedData_kFloat32
Definition dart_api.h:2614
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
sk_sp< SkImage > image
Definition examples.cpp:29
SkBitmap source
Definition examples.cpp:28
#define FML_CHECK(condition)
Definition logging.h:85
#define FML_DCHECK(condition)
Definition logging.h:103
RefPtr< T > Ref(T *ptr)
Definition ref_ptr.h:237
Definition ref_ptr.h:256
Dart_Handle ToDart(const T &object)