Flutter Impeller
linear_gradient_contents.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 
6 
10 #include "impeller/entity/entity.h"
12 
13 namespace impeller {
14 
16 
18 
19 void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
20  start_point_ = start_point;
21  end_point_ = end_point;
22 }
23 
24 void LinearGradientContents::SetColors(std::vector<Color> colors) {
25  colors_ = std::move(colors);
26 }
27 
28 void LinearGradientContents::SetStops(std::vector<Scalar> stops) {
29  stops_ = std::move(stops);
30 }
31 
32 const std::vector<Color>& LinearGradientContents::GetColors() const {
33  return colors_;
34 }
35 
36 const std::vector<Scalar>& LinearGradientContents::GetStops() const {
37  return stops_;
38 }
39 
41  tile_mode_ = tile_mode;
42 }
43 
45  if (GetOpacityFactor() < 1 || tile_mode_ == Entity::TileMode::kDecal) {
46  return false;
47  }
48  for (auto color : colors_) {
49  if (!color.IsOpaque()) {
50  return false;
51  }
52  }
53  return true;
54 }
55 
57  const Entity& entity,
58  RenderPass& pass) const {
59  if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
60  return RenderSSBO(renderer, entity, pass);
61  }
62  return RenderTexture(renderer, entity, pass);
63 }
64 
65 bool LinearGradientContents::RenderTexture(const ContentContext& renderer,
66  const Entity& entity,
67  RenderPass& pass) const {
70 
71  VS::FrameInfo frame_info;
72  frame_info.matrix = GetInverseEffectTransform();
73 
74  PipelineBuilderCallback pipeline_callback =
75  [&renderer](ContentContextOptions options) {
76  return renderer.GetLinearGradientFillPipeline(options);
77  };
78  return ColorSourceContents::DrawGeometry<VS>(
79  renderer, entity, pass, pipeline_callback, frame_info,
80  [this, &renderer](RenderPass& pass) {
81  auto gradient_data = CreateGradientBuffer(colors_, stops_);
82  auto gradient_texture =
83  CreateGradientTexture(gradient_data, renderer.GetContext());
84  if (gradient_texture == nullptr) {
85  return false;
86  }
87 
88  FS::FragInfo frag_info;
89  frag_info.start_point = start_point_;
90  frag_info.end_point = end_point_;
91  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
92  frag_info.decal_border_color = decal_border_color_;
93  frag_info.texture_sampler_y_coord_scale =
94  gradient_texture->GetYCoordScale();
95  frag_info.alpha = GetOpacityFactor();
96  frag_info.half_texel =
97  Vector2(0.5 / gradient_texture->GetSize().width,
98  0.5 / gradient_texture->GetSize().height);
99 
100  pass.SetCommandLabel("LinearGradientFill");
101 
102  SamplerDescriptor sampler_desc;
103  sampler_desc.min_filter = MinMagFilter::kLinear;
104  sampler_desc.mag_filter = MinMagFilter::kLinear;
105 
106  FS::BindTextureSampler(
107  pass, std::move(gradient_texture),
108  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
109  sampler_desc));
110  FS::BindFragInfo(
111  pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
112  return true;
113  });
114 }
115 
116 bool LinearGradientContents::RenderSSBO(const ContentContext& renderer,
117  const Entity& entity,
118  RenderPass& pass) const {
121 
122  VS::FrameInfo frame_info;
123  frame_info.matrix = GetInverseEffectTransform();
124 
125  PipelineBuilderCallback pipeline_callback =
126  [&renderer](ContentContextOptions options) {
127  return renderer.GetLinearGradientSSBOFillPipeline(options);
128  };
129  return ColorSourceContents::DrawGeometry<VS>(
130  renderer, entity, pass, pipeline_callback, frame_info,
131  [this, &renderer](RenderPass& pass) {
132  FS::FragInfo frag_info;
133  frag_info.start_point = start_point_;
134  frag_info.end_point = end_point_;
135  frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
136  frag_info.decal_border_color = decal_border_color_;
137  frag_info.alpha = GetOpacityFactor();
138 
139  auto& host_buffer = renderer.GetTransientsBuffer();
140  auto colors = CreateGradientColors(colors_, stops_);
141 
142  frag_info.colors_length = colors.size();
143  auto color_buffer =
144  host_buffer.Emplace(colors.data(), colors.size() * sizeof(StopData),
146 
147  pass.SetCommandLabel("LinearGradientSSBOFill");
148 
149  FS::BindFragInfo(
150  pass, renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
151  FS::BindColorData(pass, color_buffer);
152 
153  return true;
154  });
155 }
156 
158  const ColorFilterProc& color_filter_proc) {
159  for (Color& color : colors_) {
160  color = color_filter_proc(color);
161  }
162  decal_border_color_ = color_filter_proc(decal_border_color_);
163  return true;
164 }
165 
166 } // namespace impeller
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:113
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::ContentContext::GetLinearGradientFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetLinearGradientFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:426
gradient_generator.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
entity.h
impeller::Color
Definition: color.h:124
impeller::LinearGradientContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: linear_gradient_contents.cc:157
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::LinearGradientContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: linear_gradient_contents.cc:56
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:320
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::LinearGradientContents::SetEndPoints
void SetEndPoints(Point start_point, Point end_point)
Definition: linear_gradient_contents.cc:19
impeller::LinearGradientContents::SetColors
void SetColors(std::vector< Color > colors)
Definition: linear_gradient_contents.cc:24
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::LinearGradientContents::SetTileMode
void SetTileMode(Entity::TileMode tile_mode)
Definition: linear_gradient_contents.cc:40
impeller::Entity
Definition: entity.h:21
render_pass.h
impeller::LinearGradientContents::GetColors
const std::vector< Color > & GetColors() const
Definition: linear_gradient_contents.cc:32
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::MinMagFilter::kLinear
@ kLinear
impeller::LinearGradientContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: linear_gradient_contents.cc:44
impeller::CreateGradientBuffer
GradientData CreateGradientBuffer(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the interpolated color bytes for the linear gradient described by colors and s...
Definition: gradient.cc:20
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
content_context.h
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:568
impeller::LinearGradientContents::LinearGradientContents
LinearGradientContents()
impeller::TPoint< Scalar >
impeller::LinearGradientContents::SetStops
void SetStops(std::vector< Scalar > stops)
Definition: linear_gradient_contents.cc:28
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:50
impeller::LinearGradientContents::GetStops
const std::vector< Scalar > & GetStops() const
Definition: linear_gradient_contents.cc:36
impeller::CreateGradientColors
std::vector< StopData > CreateGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops)
Populate a vector with the color and stop data for a gradient.
Definition: gradient_generator.cc:45
impeller::CreateGradientTexture
std::shared_ptr< Texture > CreateGradientTexture(const GradientData &gradient_data, const std::shared_ptr< impeller::Context > &context)
Create a host visible texture that contains the gradient defined by the provided gradient data.
Definition: gradient_generator.cc:17
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:93
impeller::ContentContextOptions
Definition: content_context.h:288
impeller::Capabilities::SupportsSSBO
virtual bool SupportsSSBO() const =0
Whether the context backend supports binding Shader Storage Buffer Objects (SSBOs) to pipelines.
impeller
Definition: aiks_blur_unittests.cc:20
impeller::LinearGradientContents::~LinearGradientContents
~LinearGradientContents() override
impeller::ContentContext
Definition: content_context.h:392
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:36
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
linear_gradient_contents.h