Flutter Engine
The Flutter Engine
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"
13
14namespace impeller {
15
16std::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;
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(context->GetResourceAllocator()) ||
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
53std::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} // namespace impeller
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
GAsyncResult * result
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_DCHECK(condition)
Definition: logging.h:103
FlTexture * texture
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
float Scalar
Definition: scalar.h:18
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.
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...
#define ERROR(message)
Definition: elf_loader.cc:260