From c250714a4fbaf0849f35284f85f21bcd2c6d072c Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 18 May 2022 09:43:35 -0700 Subject: [PATCH] [impeller] Drop support for R8 pixel format. (flutter/engine#33455) R8 cannot be supported on ES 2 without extensions and we can do with A8 instead. Avoid buffer copy in GLES backend. Texture::SetContents should not involve any intermediate copies now. --- .../impeller/entity/shaders/glyph_atlas.frag | 2 +- .../renderer/backend/gles/texture_gles.cc | 50 +++++++------------ .../renderer/backend/metal/formats_mtl.h | 6 +-- .../src/flutter/impeller/renderer/formats.h | 4 +- .../backends/skia/text_render_context_skia.cc | 2 +- 5 files changed, 23 insertions(+), 41 deletions(-) diff --git a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag index f1dcc45686..f41d9a181d 100644 --- a/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag +++ b/engine/src/flutter/impeller/entity/shaders/glyph_atlas.frag @@ -19,5 +19,5 @@ void main() { frag_color = texture( glyph_atlas_sampler, v_unit_vertex * scale_perspective + offset - ).rrrr * v_text_color; + ).aaaa * v_text_color; } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc index 4a140dd6f4..a14f1b0da8 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc @@ -77,20 +77,24 @@ void TextureGLES::SetLabel(const std::string_view& label) { struct TexImage2DData { GLint internal_format = 0; - GLenum format = GL_NONE; + GLenum external_format = GL_NONE; GLenum type = GL_NONE; std::shared_ptr data; TexImage2DData(PixelFormat pixel_format) { switch (pixel_format) { - case PixelFormat::kR8UNormInt: + case PixelFormat::kA8UNormInt: + internal_format = GL_ALPHA; + external_format = GL_ALPHA; + type = GL_UNSIGNED_BYTE; + break; case PixelFormat::kR8G8B8A8UNormInt: case PixelFormat::kB8G8R8A8UNormInt: case PixelFormat::kR8G8B8A8UNormIntSRGB: case PixelFormat::kB8G8R8A8UNormIntSRGB: internal_format = GL_RGBA; - format = GL_RGBA; - type = GL_UNSIGNED_SHORT_4_4_4_4; + external_format = GL_RGBA; + type = GL_UNSIGNED_BYTE; break; case PixelFormat::kUnknown: case PixelFormat::kS8UInt: @@ -104,36 +108,16 @@ struct TexImage2DData { switch (pixel_format) { case PixelFormat::kUnknown: return; - case PixelFormat::kR8UNormInt: { - TRACE_EVENT0("impeller", "R8UNormIntCopy"); - internal_format = GL_RGBA; - format = GL_RGBA; - type = GL_UNSIGNED_SHORT_4_4_4_4; - { - const auto length = mapping->GetSize(); - const uint8_t* contents = mapping->GetMapping(); - auto allocation = std::make_shared(); - if (!allocation->Truncate(length * 2u, false)) { - VALIDATION_LOG << "Could not allocate buffer for texture data."; - return; - } - auto buffer = reinterpret_cast(allocation->GetBuffer()); - for (size_t i = 0; i < length; i++) { - uint8_t value = contents[i]; - buffer[i] = (value | value << 4 | value << 8 | value << 12); - } - data = CreateMappingFromAllocation(std::move(allocation)); - if (!data) { - VALIDATION_LOG << "Could not create mapping from allocation."; - return; - } - } + case PixelFormat::kA8UNormInt: { + internal_format = GL_ALPHA; + external_format = GL_ALPHA; + type = GL_UNSIGNED_BYTE; + data = std::move(mapping); break; } - case PixelFormat::kR8G8B8A8UNormInt: { internal_format = GL_RGBA; - format = GL_RGBA; + external_format = GL_RGBA; type = GL_UNSIGNED_BYTE; data = std::move(mapping); break; @@ -255,7 +239,7 @@ bool TextureGLES::OnSetContents(std::shared_ptr mapping, size.width, // width size.height, // height 0u, // border - data->format, // format + data->external_format, // external format data->type, // type tex_data // data ); @@ -282,7 +266,7 @@ static std::optional ToRenderBufferFormat(PixelFormat format) { case PixelFormat::kS8UInt: return GL_STENCIL_INDEX8; case PixelFormat::kUnknown: - case PixelFormat::kR8UNormInt: + case PixelFormat::kA8UNormInt: case PixelFormat::kR8G8B8A8UNormIntSRGB: case PixelFormat::kB8G8R8A8UNormIntSRGB: return std::nullopt; @@ -332,7 +316,7 @@ void TextureGLES::InitializeContentsIfNecessary() const { size.width, // width size.height, // height 0u, // border - tex_data.format, // format + tex_data.external_format, // format tex_data.type, // type nullptr // data ); diff --git a/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h b/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h index 835eb309d4..4099007385 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h +++ b/engine/src/flutter/impeller/renderer/backend/metal/formats_mtl.h @@ -21,8 +21,6 @@ constexpr PixelFormat FromMTLPixelFormat(MTLPixelFormat format) { switch (format) { case MTLPixelFormatInvalid: return PixelFormat::kUnknown; - case MTLPixelFormatR8Unorm: - return PixelFormat::kR8UNormInt; case MTLPixelFormatBGRA8Unorm: return PixelFormat::kB8G8R8A8UNormInt; case MTLPixelFormatBGRA8Unorm_sRGB: @@ -43,8 +41,8 @@ constexpr MTLPixelFormat ToMTLPixelFormat(PixelFormat format) { switch (format) { case PixelFormat::kUnknown: return MTLPixelFormatInvalid; - case PixelFormat::kR8UNormInt: - return MTLPixelFormatR8Unorm; + case PixelFormat::kA8UNormInt: + return MTLPixelFormatA8Unorm; case PixelFormat::kB8G8R8A8UNormInt: return MTLPixelFormatBGRA8Unorm; case PixelFormat::kB8G8R8A8UNormIntSRGB: diff --git a/engine/src/flutter/impeller/renderer/formats.h b/engine/src/flutter/impeller/renderer/formats.h index 760c19360c..3a953a6769 100644 --- a/engine/src/flutter/impeller/renderer/formats.h +++ b/engine/src/flutter/impeller/renderer/formats.h @@ -49,7 +49,7 @@ class Texture; /// enum class PixelFormat { kUnknown, - kR8UNormInt, + kA8UNormInt, kR8G8B8A8UNormInt, kR8G8B8A8UNormIntSRGB, kB8G8R8A8UNormInt, @@ -216,7 +216,7 @@ constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format) { switch (format) { case PixelFormat::kUnknown: return 0u; - case PixelFormat::kR8UNormInt: + case PixelFormat::kA8UNormInt: case PixelFormat::kS8UInt: return 1u; case PixelFormat::kR8G8B8A8UNormInt: diff --git a/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc index d5b82a6124..65340809b6 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/text_render_context_skia.cc @@ -162,7 +162,7 @@ static std::shared_ptr UploadGlyphTextureAtlas( const auto& pixmap = bitmap.pixmap(); TextureDescriptor texture_descriptor; - texture_descriptor.format = PixelFormat::kR8UNormInt; + texture_descriptor.format = PixelFormat::kA8UNormInt; texture_descriptor.size = ISize::MakeWH(atlas_size, atlas_size); if (pixmap.rowBytes() * pixmap.height() !=