10 #include "flutter/fml/logging.h"
11 #include "flutter/fml/mapping.h"
12 #include "flutter/fml/trace_event.h"
23 static bool IsDepthStencilFormat(
PixelFormat format) {
48 const TextureDescriptor& desc) {
52 if (usage == render_target && IsDepthStencilFormat(desc.format)) {
60 struct TexImage2DData {
64 std::shared_ptr<const fml::Mapping>
data;
67 switch (pixel_format) {
71 type = GL_UNSIGNED_BYTE;
76 type = GL_UNSIGNED_BYTE;
84 type = GL_UNSIGNED_BYTE;
105 type = GL_UNSIGNED_INT_24_8;
119 std::shared_ptr<const fml::Mapping> mapping)
120 : TexImage2DData(pixel_format) {
121 data = std::move(mapping);
124 bool IsValid()
const {
return is_valid_; }
127 bool is_valid_ =
false;
144 std::shared_ptr<ReactorGLES> reactor,
147 auto texture = std::shared_ptr<TextureGLES>(
148 new TextureGLES(std::move(reactor), desc, fbo, std::nullopt));
149 if (!texture->IsValid()) {
156 std::shared_ptr<ReactorGLES> reactor,
159 if (external_handle.
IsDead()) {
167 auto texture = std::shared_ptr<TextureGLES>(
168 new TextureGLES(std::move(reactor), desc, std::nullopt, external_handle));
169 if (!texture->IsValid()) {
176 std::shared_ptr<ReactorGLES> reactor,
191 std::optional<GLuint> fbo,
192 std::optional<HandleGLES> external_handle)
194 reactor_(
std::move(reactor)),
195 type_(GetTextureTypeFromDescriptor(GetTextureDescriptor())),
196 handle_(external_handle.has_value()
197 ? external_handle.
value()
198 : reactor_->CreateUntrackedHandle(
ToHandleType(type_))),
199 is_wrapped_(fbo.has_value() || external_handle.has_value()),
208 const auto max_size =
209 reactor_->GetProcTable().GetCapabilities()->max_texture_size;
210 if (tex_size.Max(max_size) != max_size) {
212 <<
" would exceed max supported size of " << max_size <<
".";
221 reactor_->CollectHandle(handle_);
222 if (cached_fbo_ != GL_NONE) {
223 reactor_->GetProcTable().DeleteFramebuffers(1, &cached_fbo_);
233 void TextureGLES::SetLabel(std::string_view label) {
234 #ifdef IMPELLER_DEBUG
235 reactor_->SetDebugLabel(handle_, label);
240 void TextureGLES::SetLabel(std::string_view label, std::string_view trailing) {
241 #ifdef IMPELLER_DEBUG
242 if (reactor_->CanSetDebugLabels()) {
243 reactor_->SetDebugLabel(handle_,
244 SPrintF(
"%s %s", label.data(), trailing.data()));
250 bool TextureGLES::OnSetContents(
const uint8_t* contents,
257 bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
263 if (mapping->GetSize() == 0u) {
267 if (mapping->GetMapping() ==
nullptr) {
272 VALIDATION_LOG <<
"Incorrect texture usage flags for setting contents on "
273 "this texture object.";
278 VALIDATION_LOG <<
"Cannot set the contents of a wrapped texture.";
284 if (tex_descriptor.size.IsEmpty()) {
288 if (!tex_descriptor.IsValid() ||
289 mapping->GetSize() < tex_descriptor.GetByteSizeOfBaseMipLevel()) {
294 GLenum texture_target;
295 switch (tex_descriptor.type) {
297 texture_type = GL_TEXTURE_2D;
298 texture_target = GL_TEXTURE_2D;
301 VALIDATION_LOG <<
"Multisample texture uploading is not supported for "
302 "the OpenGLES backend.";
305 texture_type = GL_TEXTURE_CUBE_MAP;
306 texture_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
309 texture_type = GL_TEXTURE_EXTERNAL_OES;
310 texture_target = GL_TEXTURE_EXTERNAL_OES;
314 auto data = std::make_shared<TexImage2DData>(tex_descriptor.format,
323 size = tex_descriptor.size,
326 ](
const auto& reactor) {
327 auto gl_handle = reactor.GetGLHandle(handle);
328 if (!gl_handle.has_value()) {
330 <<
"Texture was collected before it could be uploaded to the GPU.";
333 const auto& gl = reactor.GetProcTable();
334 gl.BindTexture(texture_type, gl_handle.value());
335 const GLvoid* tex_data =
nullptr;
337 tex_data =
data->data->GetMapping();
341 TRACE_EVENT1(
"impeller",
"TexImage2DUpload",
"Bytes",
342 std::to_string(
data->data->GetSize()).c_str());
343 gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
344 gl.TexImage2D(texture_target,
346 data->internal_format,
350 data->external_format,
357 slices_initialized_ = reactor_->AddOperation(texture_upload);
358 return slices_initialized_[0];
362 ISize TextureGLES::GetSize()
const {
376 return GL_STENCIL_INDEX8;
378 return GL_DEPTH24_STENCIL8;
380 return GL_DEPTH32F_STENCIL8;
404 void TextureGLES::InitializeContentsIfNecessary()
const {
405 if (!
IsValid() || slices_initialized_[0]) {
408 slices_initialized_[0] =
true;
414 auto size = GetSize();
416 if (size.IsEmpty()) {
420 const auto& gl = reactor_->GetProcTable();
421 std::optional<GLuint> handle = reactor_->GetGLHandle(handle_);
422 if (!handle.has_value()) {
423 VALIDATION_LOG <<
"Could not initialize the contents of texture.";
431 if (!tex_data.IsValid()) {
435 gl.BindTexture(GL_TEXTURE_2D, handle.value());
437 TRACE_EVENT0(
"impeller",
"TexImage2DInitialization");
438 gl.TexImage2D(GL_TEXTURE_2D,
440 tex_data.internal_format,
444 tex_data.external_format,
452 auto render_buffer_format =
454 if (!render_buffer_format.has_value()) {
458 gl.BindRenderbuffer(GL_RENDERBUFFER, handle.value());
460 TRACE_EVENT0(
"impeller",
"RenderBufferStorageInitialization");
462 gl.RenderbufferStorageMultisampleEXT(
465 render_buffer_format.value(),
470 gl.RenderbufferStorage(
472 render_buffer_format.value(),
486 return reactor_->GetGLHandle(handle_);
491 if (!handle.has_value()) {
494 const auto& gl = reactor_->GetProcTable();
496 if (fence_.has_value()) {
497 std::optional<GLsync> fence = reactor_->GetGLFence(fence_.value());
498 if (fence.has_value()) {
499 gl.WaitSync(fence.value(), 0, GL_TIMEOUT_IGNORED);
501 reactor_->CollectHandle(fence_.value());
502 fence_ = std::nullopt;
509 if (!target.has_value()) {
513 gl.BindTexture(target.value(), handle.value());
517 gl.BindRenderbuffer(GL_RENDERBUFFER, handle.value());
520 InitializeContentsIfNecessary();
525 for (
size_t i = 0; i < slices_initialized_.size(); i++) {
526 slices_initialized_[i] =
true;
531 slices_initialized_[slice] =
true;
535 return slices_initialized_[slice];
548 VALIDATION_LOG <<
"Generating mipmaps for multisample textures is not "
549 "supported in the GLES backend.";
562 if (!handle.has_value()) {
566 const auto& gl = reactor_->GetProcTable();
579 return GL_COLOR_ATTACHMENT0;
581 return GL_DEPTH_ATTACHMENT;
583 return GL_STENCIL_ATTACHMENT;
593 InitializeContentsIfNecessary();
595 if (!handle.has_value()) {
598 const auto& gl = reactor_->GetProcTable();
602 gl.FramebufferTexture2D(target,
610 gl.FramebufferTexture2DMultisampleEXT(
621 gl.FramebufferRenderbuffer(
634 Scalar TextureGLES::GetYCoordScale()
const {
653 FML_DCHECK(!fence_.has_value());
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
constexpr bool IsDead() const
Determines if the handle is dead.
HandleType GetType() const
std::function< void(const ReactorGLES &reactor)> Operation
static std::shared_ptr< TextureGLES > WrapFBO(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, GLuint fbo)
Create a texture by wrapping an external framebuffer object whose lifecycle is owned by the caller.
void MarkContentsInitialized()
Indicates that all texture storage has already been allocated and contents initialized.
std::optional< HandleGLES > GetSyncFence() const
bool IsSliceInitialized(size_t slice) const
@ kRenderBufferMultisampled
bool IsValid() const override
void SetCachedFBO(GLuint fbo)
void SetFence(HandleGLES fence)
Attach a sync fence to this texture that will be waited on before encoding a rendering operation that...
GLuint GetCachedFBO() const
Retrieve the cached FBO object, or GL_NONE if there is no object.
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type) const
static std::shared_ptr< TextureGLES > CreatePlaceholder(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc)
Create a "texture" that is never expected to be bound/unbound explicitly or initialized in any way....
TextureGLES(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc)
std::optional< GLuint > GetFBO() const
Type ComputeTypeForBinding(GLenum target) const
void MarkSliceInitialized(size_t slice) const
Indicates that a specific texture slice has been initialized.
std::optional< GLuint > GetGLHandle() const
static std::shared_ptr< TextureGLES > WrapTexture(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, HandleGLES external_handle)
Create a texture by wrapping an external OpenGL texture handle. Ownership of the texture handle is as...
const TextureDescriptor & GetTextureDescriptor() const
TextureCoordinateSystem GetCoordinateSystem() const
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, Bytes length)
Creates a mapping with copy of the bytes.
AllocationSize< 1u > Bytes
std::string SPrintF(const char *format,...)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
constexpr GLenum ToTextureType(TextureType type)
static std::optional< GLenum > ToRenderBufferFormat(PixelFormat format)
constexpr std::optional< GLenum > ToTextureTarget(TextureType type)
static GLenum ToAttachmentType(TextureGLES::AttachmentType point)
Mask< TextureUsage > TextureUsageMask
HandleType ToHandleType(TextureGLES::Type type)
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
std::shared_ptr< const fml::Mapping > data