| 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
5 #include "impeller/entity/contents/gradient_generator.h"
6
7 #include "flutter/fml/logging.h"
8 #include "impeller/base/strings.h"
9 #include "impeller/core/device_buffer.h"
10 #include "impeller/core/formats.h"
11 #include "impeller/core/texture.h"
12 #include "impeller/renderer/context.h"
13 #include "impeller/renderer/texture_util.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;
26 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
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
51 int PopulateUniformGradientColors(
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
formats.h
texture.h
device_buffer.h
gradient_generator.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::PopulateUniformGradientColors
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.
Definition: gradient_generator.cc:51
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:34
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::kMaxUniformGradientStops
static constexpr uint32_t kMaxUniformGradientStops
Definition: gradient_generator.h:47
impeller::CreateTexture
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
context.h
strings.h
impeller::GradientData
Definition: gradient.h:16
impeller::GradientData::texture_size
uint32_t texture_size
Definition: gradient.h:18
impeller::GradientData::color_bytes
std::vector< uint8_t > color_bytes
Definition: gradient.h:17
impeller::StopData
Definition: gradient_generator.h:28
impeller::StopData::color
Color color
Definition: gradient_generator.h:29
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::Vector4
Definition: vector.h:232
texture_util.h