Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions impeller/renderer/backend/gles/blit_pass_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "flutter/fml/macros.h"
#include "flutter/impeller/base/config.h"
#include "flutter/impeller/renderer/backend/gles/reactor_gles.h"
#include "flutter/impeller/renderer/blit_pass.h"
#include "impeller/renderer/backend/gles/blit_command_gles.h"
Expand Down Expand Up @@ -50,6 +51,14 @@ class BlitPassGLES final : public BlitPass {
size_t destination_offset,
std::string label) override;

// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) override {
IMPELLER_UNIMPLEMENTED;
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally fine. But IMPELLER_UNIMPLEMENTED in //impeller/base/config.h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
// |BlitPass|
bool OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
std::string label) override;
Expand Down
1 change: 1 addition & 0 deletions impeller/renderer/backend/metal/allocator_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ static MTLStorageMode ToMTLStorageMode(StorageMode mode,

mtl_texture_desc.storageMode = ToMTLStorageMode(
desc.storage_mode, supports_memoryless_targets_, supports_uma_);

if (@available(macOS 12.5, ios 15.0, *)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: make configurable, figure out if it has reasonable behavior for image textures

if (desc.compression_type == CompressionType::kLossy &&
SupportsLossyTextureCompression(device_)) {
Expand Down
10 changes: 10 additions & 0 deletions impeller/renderer/backend/metal/blit_command_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ struct BlitGenerateMipmapCommandMTL : public BlitGenerateMipmapCommand,
[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};

struct BlitCopyBufferToTextureCommandMTL
: public BlitCopyBufferToTextureCommand,
public BlitEncodeMTL {
~BlitCopyBufferToTextureCommandMTL() override;

std::string GetLabel() const override;

[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};

} // namespace impeller
45 changes: 45 additions & 0 deletions impeller/renderer/backend/metal/blit_command_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,51 @@
return true;
};

BlitCopyBufferToTextureCommandMTL::~BlitCopyBufferToTextureCommandMTL() =
default;

std::string BlitCopyBufferToTextureCommandMTL::GetLabel() const {
return label;
}

bool BlitCopyBufferToTextureCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer();
if (!source_mtl) {
return false;
}

auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
if (!destination_mtl) {
return false;
}

auto destination_origin_mtl =
MTLOriginMake(destination_origin.x, destination_origin.y, 0);

auto image_size = destination->GetTextureDescriptor().size;
auto source_size_mtl = MTLSizeMake(image_size.width, image_size.height, 1);

auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto destination_bytes_per_row =
source_size_mtl.width * destination_bytes_per_pixel;
auto destination_bytes_per_image =
source_size_mtl.height * destination_bytes_per_row;

[encoder copyFromBuffer:source_mtl
sourceOffset:source.range.offset
sourceBytesPerRow:destination_bytes_per_row
sourceBytesPerImage:destination_bytes_per_image
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:0
destinationLevel:0
destinationOrigin:destination_origin_mtl];

return true;
};

BlitGenerateMipmapCommandMTL::~BlitGenerateMipmapCommandMTL() = default;

std::string BlitGenerateMipmapCommandMTL::GetLabel() const {
Expand Down
5 changes: 5 additions & 0 deletions impeller/renderer/backend/metal/blit_pass_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class BlitPassMTL final : public BlitPass {
IRect source_region,
size_t destination_offset,
std::string label) override;
// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) override;

// |BlitPass|
bool OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
Expand Down
15 changes: 15 additions & 0 deletions impeller/renderer/backend/metal/blit_pass_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@
return true;
}

bool BlitPassMTL::OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) {
auto command = std::make_unique<BlitCopyBufferToTextureCommandMTL>();
command->label = label;
command->source = std::move(source);
command->destination = std::move(destination);
command->destination_origin = destination_origin;

commands_.emplace_back(std::move(command));
return true;
}

// |BlitPass|
bool BlitPassMTL::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
std::string label) {
Expand Down
5 changes: 2 additions & 3 deletions impeller/renderer/backend/metal/device_buffer_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

namespace impeller {

class DeviceBufferMTL final
: public DeviceBuffer,
public BackendCast<DeviceBufferMTL, DeviceBuffer> {
class DeviceBufferMTL final : public DeviceBuffer,
public BackendCast<DeviceBufferMTL, Buffer> {
public:
DeviceBufferMTL();

Expand Down
10 changes: 10 additions & 0 deletions impeller/renderer/backend/vulkan/blit_pass_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "flutter/fml/macros.h"
#include "flutter/impeller/base/config.h"
#include "impeller/renderer/backend/vulkan/blit_command_vk.h"
#include "impeller/renderer/blit_pass.h"

Expand Down Expand Up @@ -50,6 +51,15 @@ class BlitPassVK final : public BlitPass {
size_t destination_offset,
std::string label) override;

// |BlitPass|
bool OnCopyBufferToTextureCommand(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) override {
IMPELLER_UNIMPLEMENTED;
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I'll handle it. But again, FML_UNIMPLEMENTED so its easier to grep for more than anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// |BlitPass|
bool OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
std::string label) override;
Expand Down
6 changes: 6 additions & 0 deletions impeller/renderer/blit_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ struct BlitCopyTextureToBufferCommand : public BlitCommand {
size_t destination_offset;
};

struct BlitCopyBufferToTextureCommand : public BlitCommand {
BufferView source;
std::shared_ptr<Texture> destination;
IPoint destination_origin;
};

struct BlitGenerateMipmapCommand : public BlitCommand {
std::shared_ptr<Texture> texture;
};
Expand Down
26 changes: 25 additions & 1 deletion impeller/renderer/blit_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
if (destination_offset + bytes_per_image >
destination->GetDeviceBufferDescriptor().size) {
VALIDATION_LOG
<< "Attempted to add a texture blit with out fo bounds access.";
<< "Attempted to add a texture blit with out of bounds access.";
return false;
}

Expand All @@ -116,6 +116,30 @@ bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
std::move(label));
}

bool BlitPass::AddCopy(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) {
if (!destination) {
VALIDATION_LOG << "Attempted to add a texture blit with no destination.";
return false;
}

auto bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto bytes_per_image =
destination->GetTextureDescriptor().size.Area() * bytes_per_pixel;

if (source.range.length != bytes_per_image) {
VALIDATION_LOG
<< "Attempted to add a texture blit with out of bounds access.";
return false;
}

return OnCopyBufferToTextureCommand(std::move(source), std::move(destination),
destination_origin, std::move(label));
}

bool BlitPass::GenerateMipmap(std::shared_ptr<Texture> texture,
std::string label) {
if (!texture) {
Expand Down
34 changes: 30 additions & 4 deletions impeller/renderer/blit_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class BlitPass {
std::string label = "");

//----------------------------------------------------------------------------
/// @brief Record a command to copy the contents of the texture to
/// the buffer.
/// @brief Record a command to copy the contents of the buffer to
/// the texture.
/// No work is encoded into the command buffer at this time.
///
/// @param[in] source The texture to read for copying.
Expand All @@ -71,8 +71,8 @@ class BlitPass {
/// @param[in] source_region The optional region of the source texture
/// to use for copying. If not specified, the
/// full size of the source texture is used.
/// @param[in] destination_offset The offset to start writing to in the
/// destination buffer.
/// @param[in] destination_origin The origin to start writing to in the
/// destination texture.
/// @param[in] label The optional debug label to give the
/// command.
///
Expand All @@ -84,6 +84,26 @@ class BlitPass {
size_t destination_offset = 0,
std::string label = "");

//----------------------------------------------------------------------------
/// @brief Record a command to copy the contents of the buffer to
/// the texture.
/// No work is encoded into the command buffer at this time.
///
/// @param[in] source The buffer view to read for copying.
/// @param[in] destination The texture to overwrite using the source
/// contents.
/// @param[in] destination_offset The offset to start writing to in the
/// destination buffer.
/// @param[in] label The optional debug label to give the
/// command.
///
/// @return If the command was valid for subsequent commitment.
///
bool AddCopy(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin = {},
std::string label = "");

//----------------------------------------------------------------------------
/// @brief Record a command to generate all mip levels for a texture.
/// No work is encoded into the command buffer at this time.
Expand Down Expand Up @@ -127,6 +147,12 @@ class BlitPass {
size_t destination_offset,
std::string label) = 0;

virtual bool OnCopyBufferToTextureCommand(
BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label) = 0;

virtual bool OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
std::string label) = 0;

Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/device_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class DeviceBuffer : public Buffer,

const DeviceBufferDescriptor& GetDeviceBufferDescriptor() const;

virtual uint8_t* OnGetContents() const = 0;

protected:
const DeviceBufferDescriptor desc_;

explicit DeviceBuffer(DeviceBufferDescriptor desc);

virtual uint8_t* OnGetContents() const = 0;

virtual bool OnCopyHostBuffer(const uint8_t* source,
Range source_range,
size_t offset) = 0;
Expand Down
6 changes: 5 additions & 1 deletion impeller/renderer/testing/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class MockBlitPass : public BlitPass {
IRect source_region,
size_t destination_offset,
std::string label));

MOCK_METHOD4(OnCopyBufferToTextureCommand,
bool(BufferView source,
std::shared_ptr<Texture> destination,
IPoint destination_origin,
std::string label));
MOCK_METHOD2(OnGenerateMipmapCommand,
bool(std::shared_ptr<Texture> texture, std::string label));
};
Expand Down
1 change: 1 addition & 0 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ if (enable_unittests) {
":ui",
":ui_unittests_fixtures",
"//flutter/common",
"//flutter/impeller",
"//flutter/lib/snapshot",
"//flutter/shell/common:shell_test_fixture_sources",
"//flutter/testing",
Expand Down
Loading