Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
texture.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
10#include "fml/make_copyable.h"
11#include "fml/mapping.h"
15
16#if IMPELLER_SUPPORTS_RENDERING
18#endif
20
21namespace flutter {
22namespace gpu {
23
25
26Texture::Texture(std::shared_ptr<impeller::Texture> texture)
27 : texture_(std::move(texture)) {}
28
29Texture::~Texture() = default;
30
31std::shared_ptr<impeller::Texture> Texture::GetTexture() {
32 return texture_;
33}
34
36 impeller::TextureCoordinateSystem coordinate_system) {
37 texture_->SetCoordinateSystem(coordinate_system);
38}
39
40bool Texture::Overwrite(Context& gpu_context,
41 const tonic::DartByteData& source_bytes) {
42 const uint8_t* data = static_cast<const uint8_t*>(source_bytes.data());
43 auto copy = std::vector<uint8_t>(data, data + source_bytes.length_in_bytes());
44 // Texture::SetContents is a bit funky right now. It takes a shared_ptr of a
45 // mapping and we're forced to copy here.
46 auto mapping = std::make_shared<fml::DataMapping>(copy);
47
48 // For the GLES backend, command queue submission just flushes the reactor,
49 // which needs to happen on the raster thread.
50 if (gpu_context.GetContext().GetBackendType() ==
52 auto dart_state = flutter::UIDartState::Current();
53 auto& task_runners = dart_state->GetTaskRunners();
54
55 task_runners.GetRasterTaskRunner()->PostTask(
56 fml::MakeCopyable([texture = texture_, mapping = mapping]() mutable {
57 if (!texture->SetContents(mapping)) {
58 FML_LOG(ERROR) << "Failed to set texture contents.";
59 }
60 }));
61 }
62
63 if (!texture_->SetContents(mapping)) {
64 return false;
65 }
67 return true;
68}
69
72 texture_->GetTextureDescriptor().format);
73}
74
75Dart_Handle Texture::AsImage() const {
76 // DlImageImpeller isn't compiled in builds with Impeller disabled. If
77 // Impeller is disabled, it's impossible to get here anyhow, so just ifdef it
78 // out.
79#if IMPELLER_SUPPORTS_RENDERING
81 auto dl_image = impeller::DlImageImpeller::Make(texture_);
82 image->set_image(dl_image);
83 auto wrapped = image->CreateOuterWrapping();
84 return wrapped;
85#else
86 return Dart_Null();
87#endif
88}
89
90} // namespace gpu
91} // namespace flutter
92
93//----------------------------------------------------------------------------
94/// Exports
95///
96
98 flutter::gpu::Context* gpu_context,
99 int storage_mode,
100 int format,
101 int width,
102 int height,
103 int sample_count,
104 int coordinate_system,
105 int texture_type,
106 bool enable_render_target_usage,
107 bool enable_shader_read_usage,
108 bool enable_shader_write_usage) {
111 desc.size = {width, height};
113 desc.usage = {};
114 if (enable_render_target_usage) {
116 }
117 if (enable_shader_read_usage) {
119 }
120 if (enable_shader_write_usage) {
122 }
123 switch (sample_count) {
124 case 1:
126 break;
127 case 4:
129 break;
130 default:
131 return false;
132 }
133 desc.type = static_cast<impeller::TextureType>(texture_type);
136 return false;
137 }
138
139 auto texture =
140 gpu_context->GetContext().GetResourceAllocator()->CreateTexture(desc,
141 true);
142 if (!texture) {
143 FML_LOG(ERROR) << "Failed to create texture.";
144 return false;
145 }
146
147 texture->SetCoordinateSystem(
149
150 auto res = fml::MakeRefCounted<flutter::gpu::Texture>(std::move(texture));
151 res->AssociateWithDartWrapper(wrapper);
152
153 return true;
154}
155
157 flutter::gpu::Texture* wrapper,
158 int coordinate_system) {
159 return wrapper->SetCoordinateSystem(
161}
162
164 flutter::gpu::Context* gpu_context,
165 Dart_Handle source_byte_data) {
166 return texture->Overwrite(*gpu_context,
167 tonic::DartByteData(source_byte_data));
168}
169
171 flutter::gpu::Texture* wrapper) {
172 return wrapper->GetBytesPerTexel();
173}
174
176 return wrapper->AsImage();
177}
static fml::RefPtr< CanvasImage > Create()
Definition image.h:36
static UIDartState * Current()
impeller::Context & GetContext()
Definition context.cc:77
Texture(std::shared_ptr< impeller::Texture > texture)
Definition texture.cc:26
size_t GetBytesPerTexel()
Definition texture.cc:70
std::shared_ptr< impeller::Texture > GetTexture()
Definition texture.cc:31
Dart_Handle AsImage() const
Definition texture.cc:75
void SetCoordinateSystem(impeller::TextureCoordinateSystem coordinate_system)
Definition texture.cc:35
bool Overwrite(Context &gpu_context, const tonic::DartByteData &source_bytes)
Definition texture.cc:40
virtual BackendType GetBackendType() const =0
Get the graphics backend of an Impeller context.
virtual void DisposeThreadLocalCachedResources()
Definition context.h:223
virtual std::shared_ptr< Allocator > GetResourceAllocator() const =0
Returns the allocator used to create textures and buffers on the device.
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
const void * data() const
size_t length_in_bytes() const
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
FlutterVulkanImage * image
uint32_t uint32_t * format
#define FML_LOG(severity)
Definition logging.h:101
bool InternalFlutterGpu_Texture_Initialize(Dart_Handle wrapper, flutter::gpu::Context *gpu_context, int storage_mode, int format, int width, int height, int sample_count, int coordinate_system, int texture_type, bool enable_render_target_usage, bool enable_shader_read_usage, bool enable_shader_write_usage)
Definition texture.cc:97
void InternalFlutterGpu_Texture_SetCoordinateSystem(flutter::gpu::Texture *wrapper, int coordinate_system)
Definition texture.cc:156
Dart_Handle InternalFlutterGpu_Texture_AsImage(flutter::gpu::Texture *wrapper)
Definition texture.cc:175
bool InternalFlutterGpu_Texture_Overwrite(flutter::gpu::Texture *texture, flutter::gpu::Context *gpu_context, Dart_Handle source_byte_data)
Definition texture.cc:163
int InternalFlutterGpu_Texture_BytesPerTexel(flutter::gpu::Texture *wrapper)
Definition texture.cc:170
FlTexture * texture
constexpr impeller::PixelFormat ToImpellerPixelFormat(FlutterGPUPixelFormat value)
Definition formats.h:60
constexpr impeller::TextureCoordinateSystem ToImpellerTextureCoordinateSystem(FlutterGPUTextureCoordinateSystem value)
Definition formats.h:148
constexpr impeller::StorageMode ToImpellerStorageMode(FlutterGPUStorageMode value)
Definition formats.h:25
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
internal::CopyableLambda< T > MakeCopyable(T lambda)
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition formats.h:469
TextureCoordinateSystem
Definition formats.h:330
constexpr bool IsMultisampleCapable(TextureType type)
Definition formats.h:286
Definition ref_ptr.h:261
int32_t height
int32_t width
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...