Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] agressive atlas recycling. #52564

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
../../../flutter/impeller/display_list/skia_conversions_unittests.cc
../../../flutter/impeller/docs
../../../flutter/impeller/entity/contents/clip_contents_unittests.cc
../../../flutter/impeller/entity/contents/content_context_unittests.cc
../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
../../../flutter/impeller/entity/contents/host_buffer_unittests.cc
Expand Down
44 changes: 38 additions & 6 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "flutter/impeller/aiks/aiks_unittests.h"

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
Expand All @@ -21,8 +20,8 @@
#include "impeller/aiks/image_filter.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/aiks/testing/context_spy.h"
#include "impeller/core/device_buffer.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/render_target_cache.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/constants.h"
#include "impeller/geometry/geometry_asserts.h"
Expand All @@ -35,7 +34,6 @@
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/snapshot.h"
#include "impeller/typographer/backends/skia/text_frame_skia.h"
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
#include "impeller/typographer/backends/stb/text_frame_stb.h"
#include "impeller/typographer/backends/stb/typeface_stb.h"
#include "impeller/typographer/backends/stb/typographer_context_stb.h"
Expand Down Expand Up @@ -3061,12 +3059,13 @@ TEST_P(AiksTest, MipmapGenerationWorksCorrectly) {
auto texture =
GetContext()->GetResourceAllocator()->CreateTexture(texture_descriptor);

ASSERT_TRUE(!!texture);
ASSERT_TRUE(texture->SetContents(mapping));

auto device_buffer =
GetContext()->GetResourceAllocator()->CreateBufferWithCopy(*mapping);
auto command_buffer = GetContext()->CreateCommandBuffer();
auto blit_pass = command_buffer->CreateBlitPass();

blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)),
texture);
blit_pass->GenerateMipmap(texture);
EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
EXPECT_TRUE(GetContext()->GetCommandQueue()->Submit({command_buffer}).ok());
Expand Down Expand Up @@ -3153,6 +3152,39 @@ TEST_P(AiksTest, CanRenderTextWithLargePerspectiveTransform) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_P(AiksTest, SetContentsWithRegion) {
auto bridge = CreateTextureForFixture("bay_bridge.jpg");

// Replace part of the texture with a red rectangle.
std::vector<uint8_t> bytes(100 * 100 * 4);
for (auto i = 0u; i < bytes.size(); i += 4) {
bytes[i] = 255;
bytes[i + 1] = 0;
bytes[i + 2] = 0;
bytes[i + 3] = 255;
}
auto mapping =
std::make_shared<fml::NonOwnedMapping>(bytes.data(), bytes.size());
auto device_buffer =
GetContext()->GetResourceAllocator()->CreateBufferWithCopy(*mapping);
auto cmd_buffer = GetContext()->CreateCommandBuffer();
auto blit_pass = cmd_buffer->CreateBlitPass();
blit_pass->AddCopy(DeviceBuffer::AsBufferView(device_buffer), bridge,
IRect::MakeLTRB(50, 50, 150, 150));

if (!blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) ||
!GetContext()->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok()) {
GTEST_FAIL();
}

auto image = std::make_shared<Image>(bridge);

Canvas canvas;
canvas.DrawImage(image, {0, 0}, {});

ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

} // namespace testing
} // namespace impeller

Expand Down
15 changes: 0 additions & 15 deletions impeller/core/device_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ BufferView DeviceBuffer::AsBufferView(std::shared_ptr<DeviceBuffer> buffer) {
return view;
}

std::shared_ptr<Texture> DeviceBuffer::AsTexture(
Allocator& allocator,
const TextureDescriptor& descriptor,
uint16_t row_bytes) const {
auto texture = allocator.CreateTexture(descriptor);
if (!texture) {
return nullptr;
}
if (!texture->SetContents(std::make_shared<fml::NonOwnedMapping>(
OnGetContents(), desc_.size))) {
return nullptr;
}
return texture;
}

const DeviceBufferDescriptor& DeviceBuffer::GetDeviceBufferDescriptor() const {
return desc_;
}
Expand Down
6 changes: 0 additions & 6 deletions impeller/core/device_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "impeller/core/buffer_view.h"
#include "impeller/core/device_buffer_descriptor.h"
#include "impeller/core/range.h"
#include "impeller/core/texture.h"

namespace impeller {

Expand All @@ -31,11 +30,6 @@ class DeviceBuffer {
/// @brief Create a buffer view of this entire buffer.
static BufferView AsBufferView(std::shared_ptr<DeviceBuffer> buffer);

virtual std::shared_ptr<Texture> AsTexture(
Allocator& allocator,
const TextureDescriptor& descriptor,
uint16_t row_bytes) const;

const DeviceBufferDescriptor& GetDeviceBufferDescriptor() const;

virtual uint8_t* OnGetContents() const = 0;
Expand Down
6 changes: 6 additions & 0 deletions impeller/core/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class Texture {

virtual void SetLabel(std::string_view label) = 0;

// Deprecated: use BlitPass::AddCopy instead.
[[nodiscard]] bool SetContents(const uint8_t* contents,
size_t length,
size_t slice = 0,
bool is_opaque = false);

// Deprecated: use BlitPass::AddCopy instead.
[[nodiscard]] bool SetContents(std::shared_ptr<const fml::Mapping> mapping,
size_t slice = 0,
bool is_opaque = false);
Expand All @@ -39,6 +41,10 @@ class Texture {

const TextureDescriptor& GetTextureDescriptor() const;

/// Update the coordinate system used by the texture.
///
/// The setting is used to conditionally invert the coordinates to
/// account for the different origin of GLES textures.
void SetCoordinateSystem(TextureCoordinateSystem coordinate_system);

TextureCoordinateSystem GetCoordinateSystem() const;
Expand Down
1 change: 0 additions & 1 deletion impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ impeller_component("entity_unittests") {

sources = [
"contents/clip_contents_unittests.cc",
"contents/content_context_unittests.cc",
"contents/filters/gaussian_blur_filter_contents_unittests.cc",
"contents/filters/inputs/filter_input_unittests.cc",
"contents/host_buffer_unittests.cc",
Expand Down
12 changes: 11 additions & 1 deletion impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,17 @@ ContentContext::ContentContext(
desc.size = ISize{1, 1};
empty_texture_ = GetContext()->GetResourceAllocator()->CreateTexture(desc);
auto data = Color::BlackTransparent().ToR8G8B8A8();
if (!empty_texture_->SetContents(data.data(), 4)) {
auto cmd_buffer = GetContext()->CreateCommandBuffer();
auto blit_pass = cmd_buffer->CreateBlitPass();
auto& host_buffer = GetTransientsBuffer();
auto buffer_view = host_buffer.Emplace(data);
blit_pass->AddCopy(buffer_view, empty_texture_);

if (!blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()) ||
!GetContext()
->GetCommandQueue()
->Submit({std::move(cmd_buffer)})
.ok()) {
VALIDATION_LOG << "Failed to create empty texture.";
}
}
Expand Down