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
9#include "fml/make_copyable.h"
10#include "fml/mapping.h"
14#if IMPELLER_SUPPORTS_RENDERING
16#endif
18
19namespace flutter {
20namespace gpu {
21
23
24Texture::Texture(std::shared_ptr<impeller::Texture> texture)
25 : texture_(std::move(texture)) {}
26
27Texture::~Texture() = default;
28
29std::shared_ptr<impeller::Texture> Texture::GetTexture() {
30 return texture_;
31}
32
34 impeller::TextureCoordinateSystem coordinate_system) {
35 texture_->SetCoordinateSystem(coordinate_system);
36}
37
38bool Texture::Overwrite(Context& gpu_context,
39 const tonic::DartByteData& source_bytes) {
40 const uint8_t* data = static_cast<const uint8_t*>(source_bytes.data());
41 auto copy = std::vector<uint8_t>(data, data + source_bytes.length_in_bytes());
42 // Texture::SetContents is a bit funky right now. It takes a shared_ptr of a
43 // mapping and we're forced to copy here.
44 auto mapping = std::make_shared<fml::DataMapping>(copy);
45
46 // For the GLES backend, command queue submission just flushes the reactor,
47 // which needs to happen on the raster thread.
48 if (gpu_context.GetContext().GetBackendType() ==
50 auto dart_state = flutter::UIDartState::Current();
51 auto& task_runners = dart_state->GetTaskRunners();
52
53 task_runners.GetRasterTaskRunner()->PostTask(
54 fml::MakeCopyable([texture = texture_, mapping = mapping]() mutable {
55 if (!texture->SetContents(mapping)) {
56 FML_LOG(ERROR) << "Failed to set texture contents.";
57 }
58 }));
59 }
60
61 if (!texture_->SetContents(mapping)) {
62 return false;
63 }
65 return true;
66}
67
70 texture_->GetTextureDescriptor().format);
71}
72
73Dart_Handle Texture::AsImage() const {
74 // DlImageImpeller isn't compiled in builds with Impeller disabled. If
75 // Impeller is disabled, it's impossible to get here anyhow, so just ifdef it
76 // out.
77#if IMPELLER_SUPPORTS_RENDERING
79 auto dl_image = impeller::DlImageImpeller::Make(texture_);
80 image->set_image(dl_image);
81 auto wrapped = image->CreateOuterWrapping();
82 return wrapped;
83#else
84 return Dart_Null();
85#endif
86}
87
88} // namespace gpu
89} // namespace flutter
90
91//----------------------------------------------------------------------------
92/// Exports
93///
94
96 flutter::gpu::Context* gpu_context,
97 int storage_mode,
98 int format,
99 int width,
100 int height,
101 int sample_count,
102 int coordinate_system,
103 int texture_type,
104 bool enable_render_target_usage,
105 bool enable_shader_read_usage,
106 bool enable_shader_write_usage) {
109 desc.size = {width, height};
111 desc.usage = {};
112 if (enable_render_target_usage) {
114 }
115 if (enable_shader_read_usage) {
117 }
118 if (enable_shader_write_usage) {
120 }
121 switch (sample_count) {
122 case 1:
124 break;
125 case 4:
127 break;
128 default:
129 return false;
130 }
131 desc.type = static_cast<impeller::TextureType>(texture_type);
134 return false;
135 }
136
137 auto texture =
138 gpu_context->GetContext().GetResourceAllocator()->CreateTexture(desc,
139 true);
140 if (!texture) {
141 FML_LOG(ERROR) << "Failed to create texture.";
142 return false;
143 }
144
145 texture->SetCoordinateSystem(
147
148 auto res = fml::MakeRefCounted<flutter::gpu::Texture>(std::move(texture));
149 res->AssociateWithDartWrapper(wrapper);
150
151 return true;
152}
153
155 flutter::gpu::Texture* wrapper,
156 int coordinate_system) {
157 return wrapper->SetCoordinateSystem(
159}
160
162 flutter::gpu::Context* gpu_context,
163 Dart_Handle source_byte_data) {
164 return texture->Overwrite(*gpu_context,
165 tonic::DartByteData(source_byte_data));
166}
167
169 flutter::gpu::Texture* wrapper) {
170 return wrapper->GetBytesPerTexel();
171}
172
174 return wrapper->AsImage();
175}
static fml::RefPtr< CanvasImage > Create()
Definition image.h:28
static UIDartState * Current()
impeller::Context & GetContext()
Definition context.cc:77
Texture(std::shared_ptr< impeller::Texture > texture)
Definition texture.cc:24
size_t GetBytesPerTexel()
Definition texture.cc:68
std::shared_ptr< impeller::Texture > GetTexture()
Definition texture.cc:29
Dart_Handle AsImage() const
Definition texture.cc:73
void SetCoordinateSystem(impeller::TextureCoordinateSystem coordinate_system)
Definition texture.cc:33
bool Overwrite(Context &gpu_context, const tonic::DartByteData &source_bytes)
Definition texture.cc:38
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:95
void InternalFlutterGpu_Texture_SetCoordinateSystem(flutter::gpu::Texture *wrapper, int coordinate_system)
Definition texture.cc:154
Dart_Handle InternalFlutterGpu_Texture_AsImage(flutter::gpu::Texture *wrapper)
Definition texture.cc:173
bool InternalFlutterGpu_Texture_Overwrite(flutter::gpu::Texture *texture, flutter::gpu::Context *gpu_context, Dart_Handle source_byte_data)
Definition texture.cc:161
int InternalFlutterGpu_Texture_BytesPerTexel(flutter::gpu::Texture *wrapper)
Definition texture.cc:168
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...