Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
8
9#include "flutter/fml/logging.h"
14
15namespace impeller {
16
17std::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 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
45std::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
GAsyncResult * result
#define FML_DLOG(severity)
Definition logging.h:102
#define FML_DCHECK(condition)
Definition logging.h:103
FlTexture * texture
std::string SPrintF(const char *format,...)
Definition strings.cc:12
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.
std::vector< uint8_t > color_bytes
Definition gradient.h:20
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define ERROR(message)