[impeller] Give textures content mappings so they may elide a copy. (flutter/engine#33441)

This commit is contained in:
Chinmay Garde
2022-05-17 21:50:19 -07:00
committed by GitHub
parent c71b76d93b
commit b163bb2ad7
7 changed files with 130 additions and 62 deletions

View File

@@ -342,8 +342,7 @@ std::shared_ptr<Texture> Playground::CreateTextureForFixture(
}
texture->SetLabel(fixture_name);
auto uploaded = texture->SetContents(image->GetAllocation()->GetMapping(),
image->GetAllocation()->GetSize());
auto uploaded = texture->SetContents(image->GetAllocation());
if (!uploaded) {
VALIDATION_LOG << "Could not upload texture to device memory for fixture "
<< fixture_name;

View File

@@ -7,6 +7,7 @@
#include <optional>
#include "flutter/fml/mapping.h"
#include "flutter/fml/trace_event.h"
#include "impeller/base/allocation.h"
#include "impeller/base/config.h"
#include "impeller/base/validation.h"
@@ -78,7 +79,7 @@ struct TexImage2DData {
GLint internal_format = 0;
GLenum format = GL_NONE;
GLenum type = GL_NONE;
std::shared_ptr<fml::Mapping> data;
std::shared_ptr<const fml::Mapping> data;
TexImage2DData(PixelFormat pixel_format) {
switch (pixel_format) {
@@ -99,16 +100,18 @@ struct TexImage2DData {
}
TexImage2DData(PixelFormat pixel_format,
const uint8_t* contents,
size_t length) {
std::shared_ptr<const fml::Mapping> mapping) {
switch (pixel_format) {
case PixelFormat::kUnknown:
return;
case PixelFormat::kR8UNormInt:
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<Allocation>();
if (!allocation->Truncate(length * 2u, false)) {
VALIDATION_LOG << "Could not allocate buffer for texture data.";
@@ -121,20 +124,20 @@ struct TexImage2DData {
}
data = CreateMappingFromAllocation(std::move(allocation));
if (!data) {
VALIDATION_LOG << "Could not create mapping from allocation.";
return;
}
}
break;
case PixelFormat::kR8G8B8A8UNormInt:
}
case PixelFormat::kR8G8B8A8UNormInt: {
internal_format = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
data = CreateMappingWithCopy(contents, length);
if (!data) {
VALIDATION_LOG << "Could not copy texture data.";
return;
}
data = std::move(mapping);
break;
}
case PixelFormat::kR8G8B8A8UNormIntSRGB:
return;
case PixelFormat::kB8G8R8A8UNormInt:
@@ -157,10 +160,24 @@ struct TexImage2DData {
bool TextureGLES::OnSetContents(const uint8_t* contents,
size_t length,
size_t slice) {
if (length == 0u) {
return OnSetContents(CreateMappingWithCopy(contents, length), slice);
}
// |Texture|
bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice) {
if (!mapping) {
return false;
}
if (mapping->GetSize() == 0u) {
return true;
}
if (mapping->GetMapping() == nullptr) {
return false;
}
if (GetType() != Type::kTexture) {
VALIDATION_LOG << "Incorrect texture usage flags for setting contents on "
"this texture object.";
@@ -182,7 +199,8 @@ bool TextureGLES::OnSetContents(const uint8_t* contents,
return false;
}
if (length < tex_descriptor.GetByteSizeOfBaseMipLevel()) {
if (mapping->GetSize() < tex_descriptor.GetByteSizeOfBaseMipLevel()) {
return false;
}
GLenum texture_type;
@@ -202,8 +220,8 @@ bool TextureGLES::OnSetContents(const uint8_t* contents,
break;
}
auto data =
std::make_shared<TexImage2DData>(tex_descriptor.format, contents, length);
auto data = std::make_shared<TexImage2DData>(tex_descriptor.format,
std::move(mapping));
if (!data || !data->IsValid()) {
VALIDATION_LOG << "Invalid texture format.";
return false;
@@ -228,16 +246,20 @@ bool TextureGLES::OnSetContents(const uint8_t* contents,
tex_data = data->data->GetMapping();
}
gl.TexImage2D(texture_target, // target
0u, // LOD level
data->internal_format, // internal format
size.width, // width
size.height, // height
0u, // border
data->format, // format
data->type, // type
tex_data // data
);
{
TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes",
std::to_string(data->data->GetSize()).c_str());
gl.TexImage2D(texture_target, // target
0u, // LOD level
data->internal_format, // internal format
size.width, // width
size.height, // height
0u, // border
data->format, // format
data->type, // type
tex_data // data
);
}
};
contents_initialized_ = reactor_->AddOperation(texture_upload);
@@ -302,16 +324,20 @@ void TextureGLES::InitializeContentsIfNecessary() const {
return;
}
gl.BindTexture(GL_TEXTURE_2D, handle.value());
gl.TexImage2D(GL_TEXTURE_2D, // target
0u, // LOD level (base mip level size checked)
tex_data.internal_format, // internal format
size.width, // width
size.height, // height
0u, // border
tex_data.format, // format
tex_data.type, // type
nullptr // data
);
{
TRACE_EVENT0("impeller", "TexImage2DInitialization");
gl.TexImage2D(GL_TEXTURE_2D, // target
0u, // LOD level (base mip level size checked)
tex_data.internal_format, // internal format
size.width, // width
size.height, // height
0u, // border
tex_data.format, // format
tex_data.type, // type
nullptr // data
);
}
} break;
case Type::kRenderBuffer:
auto render_buffer_format =
@@ -321,11 +347,15 @@ void TextureGLES::InitializeContentsIfNecessary() const {
return;
}
gl.BindRenderbuffer(GL_RENDERBUFFER, handle.value());
gl.RenderbufferStorage(GL_RENDERBUFFER, // target
render_buffer_format.value(), // internal format
size.width, // width
size.height // height
);
{
TRACE_EVENT0("impeller", "RenderBufferStorageInitialization");
gl.RenderbufferStorage(GL_RENDERBUFFER, // target
render_buffer_format.value(), // internal format
size.width, // width
size.height // height
);
}
break;
}
reactor_->SetDebugLabel(handle_, label_);

View File

@@ -70,6 +70,10 @@ class TextureGLES final : public Texture,
size_t length,
size_t slice) override;
// |Texture|
bool OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice) override;
// |Texture|
bool IsValid() const override;

View File

@@ -34,6 +34,10 @@ class TextureMTL final : public Texture,
size_t length,
size_t slice) override;
// |Texture|
bool OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice) override;
// |Texture|
bool IsValid() const override;

View File

@@ -30,6 +30,15 @@ void TextureMTL::SetLabel(const std::string_view& label) {
[texture_ setLabel:@(label.data())];
}
// |Texture|
bool TextureMTL::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice) {
// Metal has no threading restrictions. So we can pass this data along to the
// client rendering API immediately.
return OnSetContents(mapping->GetMapping(), mapping->GetSize(), slice);
}
// |Texture|
bool TextureMTL::OnSetContents(const uint8_t* contents,
size_t length,
size_t slice) {

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "impeller/renderer/texture.h"
#include "impeller/base/validation.h"
namespace impeller {
@@ -14,29 +15,40 @@ Texture::~Texture() = default;
bool Texture::SetContents(const uint8_t* contents,
size_t length,
size_t slice) {
switch (desc_.type) {
case TextureType::kTexture2D:
case TextureType::kTexture2DMultisample:
if (slice != 0) {
VALIDATION_LOG
<< "Slice must be 0 when setting the contents of a Texture2D.";
return false;
}
break;
case TextureType::kTextureCube:
if (slice > 5) {
VALIDATION_LOG << "Slice must be <= 5 when setting the contents of a "
"cube texture.";
return false;
}
break;
if (!IsSliceValid(slice)) {
VALIDATION_LOG << "Invalid slice for texture.";
return false;
}
return OnSetContents(contents, length, slice);
}
bool Texture::SetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice) {
if (!IsSliceValid(slice)) {
VALIDATION_LOG << "Invalid slice for texture.";
return false;
}
return OnSetContents(contents, length, slice);
if (!mapping) {
return false;
}
return OnSetContents(std::move(mapping), slice);
}
const TextureDescriptor& Texture::GetTextureDescriptor() const {
return desc_;
}
bool Texture::IsSliceValid(size_t slice) const {
switch (desc_.type) {
case TextureType::kTexture2D:
case TextureType::kTexture2DMultisample:
return slice == 0;
case TextureType::kTextureCube:
return slice <= 5;
}
FML_UNREACHABLE();
}
} // namespace impeller

View File

@@ -7,6 +7,7 @@
#include <string_view>
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "impeller/geometry/size.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/texture_descriptor.h"
@@ -19,14 +20,13 @@ class Texture {
virtual void SetLabel(const std::string_view& label) = 0;
[[nodiscard]] virtual bool OnSetContents(const uint8_t* contents,
size_t length,
size_t slice) = 0;
[[nodiscard]] bool SetContents(const uint8_t* contents,
size_t length,
size_t slice = 0);
[[nodiscard]] bool SetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice = 0);
virtual bool IsValid() const = 0;
virtual ISize GetSize() const = 0;
@@ -36,9 +36,19 @@ class Texture {
protected:
Texture(TextureDescriptor desc);
[[nodiscard]] virtual bool OnSetContents(const uint8_t* contents,
size_t length,
size_t slice) = 0;
[[nodiscard]] virtual bool OnSetContents(
std::shared_ptr<const fml::Mapping> mapping,
size_t slice) = 0;
private:
const TextureDescriptor desc_;
bool IsSliceValid(size_t slice) const;
FML_DISALLOW_COPY_AND_ASSIGN(Texture);
};