diff --git a/engine/src/flutter/impeller/compositor/BUILD.gn b/engine/src/flutter/impeller/compositor/BUILD.gn index 9e5aa68940..b9a5e5dc96 100644 --- a/engine/src/flutter/impeller/compositor/BUILD.gn +++ b/engine/src/flutter/impeller/compositor/BUILD.gn @@ -56,6 +56,8 @@ impeller_component("compositor") { "surface.mm", "texture.h", "texture.mm", + "texture_descriptor.h", + "texture_descriptor.mm", "vertex_buffer.h", "vertex_buffer.mm", "vertex_buffer_builder.h", diff --git a/engine/src/flutter/impeller/compositor/allocator.h b/engine/src/flutter/impeller/compositor/allocator.h index 86c9a08e51..9f306180b8 100644 --- a/engine/src/flutter/impeller/compositor/allocator.h +++ b/engine/src/flutter/impeller/compositor/allocator.h @@ -9,6 +9,7 @@ #include #include "flutter/fml/macros.h" +#include "flutter/fml/mapping.h" namespace impeller { @@ -57,6 +58,9 @@ class Allocator { std::shared_ptr CreateBufferWithCopy(const uint8_t* buffer, size_t length); + std::shared_ptr CreateBufferWithCopy( + const fml::Mapping& mapping); + static bool RequiresExplicitHostSynchronization(StorageMode mode); private: diff --git a/engine/src/flutter/impeller/compositor/allocator.mm b/engine/src/flutter/impeller/compositor/allocator.mm index 5d86910986..a94d171d9f 100644 --- a/engine/src/flutter/impeller/compositor/allocator.mm +++ b/engine/src/flutter/impeller/compositor/allocator.mm @@ -77,6 +77,11 @@ std::shared_ptr Allocator::CreateBufferWithCopy( return new_buffer; } +std::shared_ptr Allocator::CreateBufferWithCopy( + const fml::Mapping& mapping) { + return CreateBufferWithCopy(mapping.GetMapping(), mapping.GetSize()); +} + std::shared_ptr Allocator::CreateBuffer(StorageMode mode, size_t length) { auto buffer = diff --git a/engine/src/flutter/impeller/compositor/device_buffer.h b/engine/src/flutter/impeller/compositor/device_buffer.h index f8c93b09c2..6918d04dbb 100644 --- a/engine/src/flutter/impeller/compositor/device_buffer.h +++ b/engine/src/flutter/impeller/compositor/device_buffer.h @@ -27,6 +27,18 @@ class DeviceBuffer final : public Buffer, Range source_range, size_t offset = 0u); + //---------------------------------------------------------------------------- + /// @brief Create a texture whose contents are the same as that of this + /// buffer. Changes to either the contents of the texture or the + /// buffer will be shared. When using buffer backed textures, + /// implementations may have to disable certain optimizations. + /// + /// @param[in] desc The description of the texture. + /// @param[in] offset The offset of the texture data within buffer. + /// + /// @return The texture whose contents are backed by (a part of) this + /// buffer. + /// std::shared_ptr MakeTexture(TextureDescriptor desc, size_t offset = 0u) const; diff --git a/engine/src/flutter/impeller/compositor/device_buffer.mm b/engine/src/flutter/impeller/compositor/device_buffer.mm index 157814f22b..cbc84aa076 100644 --- a/engine/src/flutter/impeller/compositor/device_buffer.mm +++ b/engine/src/flutter/impeller/compositor/device_buffer.mm @@ -65,7 +65,9 @@ std::shared_ptr DeviceBuffer::MakeTexture(TextureDescriptor desc, return false; } - ::memmove(dest + offset, source + source_range.offset, source_range.length); + if (source) { + ::memmove(dest + offset, source + source_range.offset, source_range.length); + } if (Allocator::RequiresExplicitHostSynchronization(mode_)) { [buffer_ didModifyRange:NSMakeRange(offset, source_range.length)]; diff --git a/engine/src/flutter/impeller/compositor/texture.h b/engine/src/flutter/impeller/compositor/texture.h index e64d2c7dfc..f1767b90fb 100644 --- a/engine/src/flutter/impeller/compositor/texture.h +++ b/engine/src/flutter/impeller/compositor/texture.h @@ -8,37 +8,11 @@ #include "flutter/fml/macros.h" #include "impeller/compositor/formats.h" +#include "impeller/compositor/texture_descriptor.h" #include "impeller/geometry/size.h" namespace impeller { -struct TextureDescriptor { - PixelFormat format = PixelFormat::kUnknown; - ISize size; - size_t mip_count = 1u; // Size::MipCount is usually appropriate. - - constexpr size_t GetSizeOfBaseMipLevel() const { - if (!IsValid()) { - return 0u; - } - return size.Area() * BytesPerPixelForPixelFormat(format); - } - - constexpr size_t GetBytesPerRow() const { - if (!IsValid()) { - return 0u; - } - return size.width * BytesPerPixelForPixelFormat(format); - } - - bool IsValid() const { - return format != PixelFormat::kUnknown && // - size.IsPositive() && // - mip_count >= 1u // - ; - } -}; - class Texture { public: Texture(id texture); diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.h b/engine/src/flutter/impeller/compositor/texture_descriptor.h new file mode 100644 index 0000000000..c3b7f653fb --- /dev/null +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.h @@ -0,0 +1,45 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include + +#include "impeller/compositor/formats.h" +#include "impeller/geometry/size.h" +#include "impeller/image/image.h" + +namespace impeller { + +struct TextureDescriptor { + PixelFormat format = PixelFormat::kUnknown; + ISize size; + size_t mip_count = 1u; // Size::MipCount is usually appropriate. + + constexpr size_t GetSizeOfBaseMipLevel() const { + if (!IsValid()) { + return 0u; + } + return size.Area() * BytesPerPixelForPixelFormat(format); + } + + constexpr size_t GetBytesPerRow() const { + if (!IsValid()) { + return 0u; + } + return size.width * BytesPerPixelForPixelFormat(format); + } + + bool IsValid() const { + return format != PixelFormat::kUnknown && // + size.IsPositive() && // + mip_count >= 1u // + ; + } + + static std::optional MakeFromImageResult( + const ImageResult& result); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/compositor/texture_descriptor.mm b/engine/src/flutter/impeller/compositor/texture_descriptor.mm new file mode 100644 index 0000000000..18021e8e78 --- /dev/null +++ b/engine/src/flutter/impeller/compositor/texture_descriptor.mm @@ -0,0 +1,50 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/compositor/texture_descriptor.h" + +namespace impeller { + +std::optional FormatForImageResultComponents( + ImageResult::Components comp) { + switch (comp) { + case ImageResult::Components::Invalid: + return std::nullopt; + case ImageResult::Components::Grey: + return std::nullopt; + case ImageResult::Components::GreyAlpha: + return std::nullopt; + case ImageResult::Components::RGB: + return std::nullopt; + case ImageResult::Components::RGBA: + return PixelFormat::kPixelFormat_B8G8R8A8_UNormInt; + } + return std::nullopt; +} + +std::optional TextureDescriptor::MakeFromImageResult( + const ImageResult& result) { + if (!result.IsValid()) { + return std::nullopt; + } + + const auto pixel_format = + FormatForImageResultComponents(result.GetComponents()); + if (!pixel_format.has_value()) { + return std::nullopt; + } + + TextureDescriptor desc; + desc.format = pixel_format.value(); + desc.size = result.GetSize(); + desc.mip_count = result.GetSize().MipCount(); + + if (!desc.IsValid()) { + return std::nullopt; + } + + return desc; +} + +} // namespace impeller diff --git a/engine/src/flutter/impeller/image/image.cc b/engine/src/flutter/impeller/image/image.cc index aa0f1f9de1..edcb4b3bf1 100644 --- a/engine/src/flutter/impeller/image/image.cc +++ b/engine/src/flutter/impeller/image/image.cc @@ -22,19 +22,19 @@ ImageResult Image::Decode() const { int comps = 0; stbi_uc* decoded = - stbi_load_from_memory(source_->GetMapping(), // Source Data - source_->GetSize(), // Source Data Size - &width, // Out: Width - &height, // Out: Height - &comps, // Out: Components - STBI_default); + ::stbi_load_from_memory(source_->GetMapping(), // Source Data + source_->GetSize(), // Source Data Size + &width, // Out: Width + &height, // Out: Height + &comps, // Out: Components + STBI_default); if (decoded == nullptr) { FML_LOG(ERROR) << "Could not decode image from host memory."; return {}; } - auto destinationAllocation = std::make_shared( + auto dest_allocation = std::make_shared( decoded, // bytes width * height * comps * sizeof(stbi_uc), // byte size [](const uint8_t* data, size_t size) { @@ -71,9 +71,9 @@ ImageResult Image::Decode() const { } return ImageResult{ - Size{static_cast(width), static_cast(height)}, // size - components, // components - std::move(destinationAllocation) // allocation + ISize{width, height}, // size + components, // components + std::move(dest_allocation) // allocation }; } diff --git a/engine/src/flutter/impeller/image/image.h b/engine/src/flutter/impeller/image/image.h index 4bd0d32d60..612e76763b 100644 --- a/engine/src/flutter/impeller/image/image.h +++ b/engine/src/flutter/impeller/image/image.h @@ -19,7 +19,7 @@ class Image { ~Image(); - ImageResult Decode() const; + [[nodiscard]] ImageResult Decode() const; bool IsValid() const; diff --git a/engine/src/flutter/impeller/image/image_result.cc b/engine/src/flutter/impeller/image/image_result.cc index 937a3b946e..a778de0e38 100644 --- a/engine/src/flutter/impeller/image/image_result.cc +++ b/engine/src/flutter/impeller/image/image_result.cc @@ -8,21 +8,23 @@ namespace impeller { ImageResult::ImageResult() = default; -ImageResult::ImageResult(Size size, +ImageResult::ImageResult(ISize size, Components components, std::shared_ptr allocation) - : success_(true), - size_(size), - components_(components), - allocation_(std::move(allocation)) {} + : size_(size), components_(components), allocation_(std::move(allocation)) { + if (!allocation_ || !size.IsPositive()) { + return; + } + is_valid_ = true; +} ImageResult::~ImageResult() = default; -bool ImageResult::WasSuccessful() const { - return success_; +bool ImageResult::IsValid() const { + return is_valid_; } -const Size& ImageResult::GetSize() const { +const ISize& ImageResult::GetSize() const { return size_; } @@ -30,7 +32,7 @@ ImageResult::Components ImageResult::GetComponents() const { return components_; } -const std::shared_ptr& ImageResult::Allocation() const { +const std::shared_ptr& ImageResult::GetAllocation() const { return allocation_; } diff --git a/engine/src/flutter/impeller/image/image_result.h b/engine/src/flutter/impeller/image/image_result.h index 7f5e4b7e44..7b0c23354a 100644 --- a/engine/src/flutter/impeller/image/image_result.h +++ b/engine/src/flutter/impeller/image/image_result.h @@ -24,25 +24,25 @@ class ImageResult { ImageResult(); - ImageResult(Size size, + ImageResult(ISize size, Components components, std::shared_ptr allocation); ~ImageResult(); - const Size& GetSize() const; + const ISize& GetSize() const; - bool WasSuccessful() const; + bool IsValid() const; Components GetComponents() const; - const std::shared_ptr& Allocation() const; + const std::shared_ptr& GetAllocation() const; private: - bool success_ = false; - Size size_; + ISize size_; Components components_ = Components::Invalid; std::shared_ptr allocation_; + bool is_valid_ = false; FML_DISALLOW_COPY_AND_ASSIGN(ImageResult); }; diff --git a/engine/src/flutter/impeller/playground/playground.mm b/engine/src/flutter/impeller/playground/playground.mm index eb564748c3..2fe879e8b8 100644 --- a/engine/src/flutter/impeller/playground/playground.mm +++ b/engine/src/flutter/impeller/playground/playground.mm @@ -76,8 +76,7 @@ bool Playground::OpenPlaygroundHere(Renderer::RenderCallback render_callback) { ::glfwWindowHint(GLFW_RESIZABLE, false); auto window_title = GetWindowTitle(flutter::testing::GetCurrentTestName()); - auto window = - ::glfwCreateWindow(1920, 1080, window_title.c_str(), NULL, NULL); + auto window = ::glfwCreateWindow(1024, 768, window_title.c_str(), NULL, NULL); if (!window) { return false; } diff --git a/engine/src/flutter/impeller/primitives/BUILD.gn b/engine/src/flutter/impeller/primitives/BUILD.gn index 18acdaddd6..8abe52c1ab 100644 --- a/engine/src/flutter/impeller/primitives/BUILD.gn +++ b/engine/src/flutter/impeller/primitives/BUILD.gn @@ -29,6 +29,7 @@ impeller_component("primitives_unittests") { sources = [ "primitives_unittests.mm" ] deps = [ ":primitives", + "../image", "../playground", ] } diff --git a/engine/src/flutter/impeller/primitives/box.frag b/engine/src/flutter/impeller/primitives/box.frag index 0263c965f2..c1d0500b37 100644 --- a/engine/src/flutter/impeller/primitives/box.frag +++ b/engine/src/flutter/impeller/primitives/box.frag @@ -3,9 +3,12 @@ // found in the LICENSE file. in vec4 color; +in vec2 interporlated_texture_coordinates; out vec4 frag_color; +uniform sampler2D contents_texture; + void main() { - frag_color = color; + frag_color = texture(contents_texture, interporlated_texture_coordinates); } diff --git a/engine/src/flutter/impeller/primitives/box.vert b/engine/src/flutter/impeller/primitives/box.vert index 431c4c04d1..87d0e537e7 100644 --- a/engine/src/flutter/impeller/primitives/box.vert +++ b/engine/src/flutter/impeller/primitives/box.vert @@ -8,10 +8,13 @@ uniform UniformBuffer { in vec3 vertex_position; in vec4 vertex_color; +in vec2 texture_coordinates; out vec4 color; +out vec2 interporlated_texture_coordinates; void main() { gl_Position = uniforms.mvp * vec4(vertex_position, 1.0); color = vertex_color; + interporlated_texture_coordinates = texture_coordinates; } diff --git a/engine/src/flutter/impeller/primitives/primitives_unittests.mm b/engine/src/flutter/impeller/primitives/primitives_unittests.mm index bd65d6f28b..a87b9b19f0 100644 --- a/engine/src/flutter/impeller/primitives/primitives_unittests.mm +++ b/engine/src/flutter/impeller/primitives/primitives_unittests.mm @@ -10,6 +10,7 @@ #include "impeller/compositor/sampler_descriptor.h" #include "impeller/compositor/surface.h" #include "impeller/compositor/vertex_buffer_builder.h" +#include "impeller/image/image.h" #include "impeller/playground/playground.h" #include "impeller/primitives/box.frag.h" #include "impeller/primitives/box.vert.h" @@ -29,20 +30,37 @@ TEST_F(PrimitivesTest, CanCreateBoxPrimitive) { context->GetPipelineLibrary()->GetRenderPipeline(std::move(desc)).get(); ASSERT_TRUE(box_pipeline); + // Vertex buffer. VertexBufferBuilder vertex_builder; vertex_builder.SetLabel("Box"); vertex_builder.AddVertices({ - {{100, 100, 0.0}, {Color::Red()}}, // 1 - {{800, 100, 0.0}, {Color::Green()}}, // 2 - {{800, 800, 0.0}, {Color::Blue()}}, // 3 + {{100, 100, 0.0}, {Color::Red()}, {0.0, 0.0}}, // 1 + {{800, 100, 0.0}, {Color::Green()}, {1.0, 0.0}}, // 2 + {{800, 800, 0.0}, {Color::Blue()}, {1.0, 1.0}}, // 3 - {{100, 100, 0.0}, {Color::Cyan()}}, // 1 - {{800, 800, 0.0}, {Color::White()}}, // 3 - {{100, 800, 0.0}, {Color::Purple()}}, // 4 + {{100, 100, 0.0}, {Color::Cyan()}, {0.0, 0.0}}, // 1 + {{800, 800, 0.0}, {Color::White()}, {1.0, 1.0}}, // 3 + {{100, 800, 0.0}, {Color::Purple()}, {0.0, 1.0}}, // 4 }); auto vertex_buffer = vertex_builder.CreateVertexBuffer(*context->GetPermanentsAllocator()); ASSERT_TRUE(vertex_buffer); + + // Contents texture. + Image image(flutter::testing::OpenFixtureAsMapping("image.png")); + auto result = image.Decode(); + ASSERT_TRUE(result.IsValid()); + auto device_image_allocation = + context->GetPermanentsAllocator()->CreateBufferWithCopy( + *result.GetAllocation()); + device_image_allocation->SetLabel("Bay Bridge"); + ASSERT_TRUE(device_image_allocation); + auto texture_descriptor = TextureDescriptor::MakeFromImageResult(result); + ASSERT_TRUE(texture_descriptor.has_value()); + auto texture = + device_image_allocation->MakeTexture(texture_descriptor.value()); + ASSERT_TRUE(texture); + Renderer::RenderCallback callback = [&](const Surface& surface, RenderPass& pass) { BoxVertexShader::UniformBuffer uniforms;