Flutter Impeller
gradient_generator.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 
7 #include "flutter/fml/logging.h"
10 #include "impeller/core/formats.h"
11 #include "impeller/core/texture.h"
14 
15 namespace impeller {
16 
17 std::shared_ptr<Texture> CreateGradientTexture(
18  const GradientData& gradient_data,
19  const std::shared_ptr<impeller::Context>& context) {
20  if (gradient_data.texture_size == 0) {
21  FML_DLOG(ERROR) << "Invalid gradient data.";
22  return nullptr;
23  }
24 
25  impeller::TextureDescriptor texture_descriptor;
27  texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
28  texture_descriptor.size = {gradient_data.texture_size, 1};
29 
30  return CreateTexture(texture_descriptor, gradient_data.color_bytes, context,
31  "Gradient");
32 }
33 
34 std::vector<StopData> CreateGradientColors(const std::vector<Color>& colors,
35  const std::vector<Scalar>& stops) {
36  FML_DCHECK(stops.size() == colors.size());
37 
38  std::vector<StopData> result;
39  result.reserve(stops.size());
40  Scalar last_stop = 0;
41  for (auto i = 0u; i < stops.size(); i++) {
42  Scalar delta = stops[i] - last_stop;
43  Scalar inverse_delta = delta == 0.0f ? 0.0 : 1.0 / delta;
44  result.emplace_back(StopData{
45  .color = colors[i], .stop = stops[i], .inverse_delta = inverse_delta});
46  last_stop = stops[i];
47  }
48  return result;
49 }
50 
52  const std::vector<Color>& colors,
53  const std::vector<Scalar>& stops,
54  Vector4 frag_info_colors[kMaxUniformGradientStops],
55  Vector4 frag_info_stop_pairs[kMaxUniformGradientStops / 2]) {
56  FML_DCHECK(stops.size() == colors.size());
57 
58  Scalar last_stop = 0;
59  int index = 0;
60  for (auto i = 0u; i < stops.size() && i < kMaxUniformGradientStops; i++) {
61  Scalar cur_stop = stops[i];
62  Scalar delta = cur_stop - last_stop;
63  Scalar inverse_delta = delta == 0.0f ? 0.0 : 1.0 / delta;
64  frag_info_colors[index] = colors[i];
65  if ((i & 1) == 0) {
66  frag_info_stop_pairs[index / 2].x = cur_stop;
67  frag_info_stop_pairs[index / 2].y = inverse_delta;
68  } else {
69  frag_info_stop_pairs[index / 2].z = cur_stop;
70  frag_info_stop_pairs[index / 2].w = inverse_delta;
71  }
72  last_stop = cur_stop;
73  index++;
74  }
75  return index;
76 }
77 
78 } // namespace impeller
float Scalar
Definition: scalar.h:19
int PopulateUniformGradientColors(const std::vector< Color > &colors, const std::vector< Scalar > &stops, Vector4 frag_info_colors[kMaxUniformGradientStops], Vector4 frag_info_stop_pairs[kMaxUniformGradientStops/2])
Populate 2 arrays with the colors and stop data for a gradient.
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.
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.
static constexpr uint32_t kMaxUniformGradientStops
std::shared_ptr< Texture > CreateTexture(const TextureDescriptor &texture_descriptor, const std::vector< uint8_t > &data, const std::shared_ptr< impeller::Context > &context, std::string_view debug_label)
Definition: texture_util.cc:11
uint32_t texture_size
Definition: gradient.h:18
std::vector< uint8_t > color_bytes
Definition: gradient.h:17
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...