| 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 <algorithm>
6
7 #include "impeller/entity/contents/gradient_generator.h"
8
9 #include "flutter/fml/logging.h"
10 #include "impeller/core/texture.h"
11 #include "impeller/entity/contents/content_context.h"
12 #include "impeller/renderer/context.h"
13 #include "impeller/renderer/render_pass.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 auto texture =
30 context->GetResourceAllocator()->CreateTexture(texture_descriptor);
31 if (!texture) {
32 FML_DLOG(ERROR) << "Could not create Impeller texture.";
33 return nullptr;
34 }
35
36 auto mapping = std::make_shared<fml::DataMapping>(gradient_data.color_bytes);
37 if (!texture->SetContents(mapping)) {
38 FML_DLOG(ERROR) << "Could not copy contents into Impeller texture.";
39 return nullptr;
40 }
41 texture->SetLabel(impeller::SPrintF("Gradient(%p)", texture.get()).c_str());
42 return texture;
43 }
44
45 std::vector<StopData> CreateGradientColors(const std::vector<Color>& colors,
46 const std::vector<Scalar>& stops) {
47 FML_DCHECK(stops.size() == colors.size());
48
49 std::vector<StopData> result(stops.size());
50 for (auto i = 0u; i < stops.size(); i++) {
51 result[i] = {.color = colors[i], .stop = stops[i]};
52 }
53 return result;
54 }
55
56 } // namespace impeller
gradient_generator.h
impeller::GradientData::texture_size
uint32_t texture_size
Definition: gradient.h:21
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:40
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::StorageMode::kHostVisible
@ kHostVisible
render_pass.h
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::GradientData::color_bytes
std::vector< uint8_t > color_bytes
Definition: gradient.h:20
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:41
content_context.h
impeller::GradientData
Definition: gradient.h:19
texture.h
context.h
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:38
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::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:37
impeller
Definition: aiks_context.cc:10