| 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
14 namespace impeller {
15
16 std::shared_ptr<Texture> CreateGradientTexture(
17 const GradientData& gradient_data,
18 const std::shared_ptr<impeller::Context>& context) {
19 if (gradient_data.texture_size == 0) {
20 FML_DLOG(ERROR) << "Invalid gradient data.";
21 return nullptr;
22 }
23
24 impeller::TextureDescriptor texture_descriptor;
25 texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
26 texture_descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
27 texture_descriptor.size = {gradient_data.texture_size, 1};
28 auto texture =
29 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
30 if (!texture) {
31 FML_DLOG(ERROR) << "Could not create Impeller texture.";
32 return nullptr;
33 }
34
35 auto data_mapping =
36 std::make_shared<fml::DataMapping>(gradient_data.color_bytes);
37 auto buffer =
38 context->GetResourceAllocator()->CreateBufferWithCopy(*data_mapping);
39
40 auto cmd_buffer = context->CreateCommandBuffer();
41 auto blit_pass = cmd_buffer->CreateBlitPass();
42 blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(buffer)), texture);
43
44 if (!blit_pass->EncodeCommands() ||
45 !context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
46 return nullptr;
47 }
48
49 texture->SetLabel(impeller::SPrintF("Gradient(%p)", texture.get()).c_str());
50 return texture;
51 }
52
53 std::vector<StopData> CreateGradientColors(const std::vector<Color>& colors,
54 const std::vector<Scalar>& stops) {
55 FML_DCHECK(stops.size() == colors.size());
56
57 std::vector<StopData> result;
58 result.reserve(stops.size());
59 Scalar last_stop = 0;
60 for (auto i = 0u; i < stops.size(); i++) {
61 Scalar delta = stops[i] - last_stop;
62 Scalar inverse_delta = delta == 0.0f ? 0.0 : 1.0 / delta;
63 result.emplace_back(StopData{
64 .color = colors[i], .stop = stops[i], .inverse_delta = inverse_delta});
65 last_stop = stops[i];
66 }
67 return result;
68 }
69
70 int PopulateUniformGradientColors(
71 const std::vector<Color>& colors,
72 const std::vector<Scalar>& stops,
73 Vector4 frag_info_colors[kMaxUniformGradientStops],
74 Vector4 frag_info_stop_pairs[kMaxUniformGradientStops / 2]) {
75 FML_DCHECK(stops.size() == colors.size());
76
77 Scalar last_stop = 0;
78 int index = 0;
79 for (auto i = 0u; i < stops.size() && i < kMaxUniformGradientStops; i++) {
80 Scalar cur_stop = stops[i];
81 Scalar delta = cur_stop - last_stop;
82 Scalar inverse_delta = delta == 0.0f ? 0.0 : 1.0 / delta;
83 frag_info_colors[index] = colors[i];
84 if ((i & 1) == 0) {
85 frag_info_stop_pairs[index / 2].x = cur_stop;
86 frag_info_stop_pairs[index / 2].y = inverse_delta;
87 } else {
88 frag_info_stop_pairs[index / 2].z = cur_stop;
89 frag_info_stop_pairs[index / 2].w = inverse_delta;
90 }
91 last_stop = cur_stop;
92 index++;
93 }
94 return index;
95 }
96
97 } // namespace impeller
impeller::DeviceBuffer::AsBufferView
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
Definition: device_buffer.cc:18
formats.h
texture.h
device_buffer.h
gradient_generator.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
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:70
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:53
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:16
impeller::kMaxUniformGradientStops
static constexpr uint32_t kMaxUniformGradientStops
Definition: gradient_generator.h:47
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