Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
6 changes: 4 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2407,8 +2407,10 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStan
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec_Internal.h
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/flutter_standard_codec_unittest.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.mm
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h
Expand Down
1 change: 1 addition & 0 deletions common/graphics/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ source_set("graphics") {
# additions here could result in added app sizes across embeddings.
deps = [
"//flutter/assets",
"//flutter/display_list",
"//flutter/fml",
"//flutter/shell/version:version",
"//third_party/boringssl",
Expand Down
16 changes: 12 additions & 4 deletions common/graphics/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <map>

#include "flutter/display_list/display_list_builder.h"
#include "flutter/display_list/display_list_paint.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/synchronization/waitable_event.h"
#include "third_party/skia/include/core/SkCanvas.h"
Expand All @@ -33,16 +35,22 @@ class ContextListener {

class Texture : public ContextListener {
public:
struct PaintContext {
SkCanvas* canvas = nullptr;
DisplayListBuilder* builder = nullptr;
GrDirectContext* gr_context = nullptr;
const SkPaint* sk_paint = nullptr;
const DlPaint* dl_paint = nullptr;
};

explicit Texture(int64_t id); // Called from UI or raster thread.
virtual ~Texture(); // Called from raster thread.

// Called from raster thread.
virtual void Paint(SkCanvas& canvas,
virtual void Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling,
const SkPaint* paint = nullptr) = 0;
const SkSamplingOptions& sampling) = 0;

// Called on raster thread.
virtual void MarkNewFrameAvailable() = 0;
Expand Down
10 changes: 8 additions & 2 deletions flow/layers/texture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ void TextureLayer::Paint(PaintContext& context) const {
return;
}
AutoCachePaint cache_paint(context);
texture->Paint(*context.leaf_nodes_canvas, paint_bounds(), freeze_,
context.gr_context, ToSk(sampling_), cache_paint.sk_paint());
Texture::PaintContext ctx{
.canvas = context.leaf_nodes_canvas,
.builder = context.leaf_nodes_builder,
.gr_context = context.gr_context,
.sk_paint = cache_paint.sk_paint(),
.dl_paint = cache_paint.dl_paint(),
};
texture->Paint(ctx, paint_bounds(), freeze_, ToSk(sampling_));
}

} // namespace flutter
12 changes: 6 additions & 6 deletions flow/testing/mock_texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
// found in the LICENSE file.

#include "flutter/flow/testing/mock_texture.h"
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/testing/skia_gpu_object_layer_test.h"

namespace flutter {
namespace testing {

MockTexture::MockTexture(int64_t textureId) : Texture(textureId) {}

void MockTexture::Paint(SkCanvas& canvas,
void MockTexture::Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling,
const SkPaint* paint) {
paint_calls_.emplace_back(
PaintCall{canvas, bounds, freeze, context, sampling, paint});
const SkSamplingOptions& sampling) {
paint_calls_.emplace_back(PaintCall{*(context.canvas), bounds, freeze,
context.gr_context, sampling,
context.sk_paint});
}

bool operator==(const MockTexture::PaintCall& a,
Expand Down
6 changes: 2 additions & 4 deletions flow/testing/mock_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ class MockTexture : public Texture {
explicit MockTexture(int64_t textureId);

// Called from raster thread.
void Paint(SkCanvas& canvas,
void Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling,
const SkPaint* paint = nullptr) override;
const SkSamplingOptions& sampling) override;

void OnGrContextCreated() override { gr_context_created_ = true; }
void OnGrContextDestroyed() override { gr_context_destroyed_ = true; }
Expand Down
16 changes: 10 additions & 6 deletions flow/testing/mock_texture_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ TEST(MockTextureTest, PaintCalls) {
MockTexture::PaintCall{canvas, paint_bounds1, false, nullptr, sampling},
MockTexture::PaintCall{canvas, paint_bounds2, true, nullptr, sampling}};
auto texture = std::make_shared<MockTexture>(0);

texture->Paint(canvas, paint_bounds1, false, nullptr, sampling);
texture->Paint(canvas, paint_bounds2, true, nullptr, sampling);
Texture::PaintContext context{
.canvas = &canvas,
};
texture->Paint(context, paint_bounds1, false, sampling);
texture->Paint(context, paint_bounds2, true, sampling);
EXPECT_EQ(texture->paint_calls(), expected_paint_calls);
}

Expand All @@ -49,9 +51,11 @@ TEST(MockTextureTest, PaintCallsWithLinearSampling) {
MockTexture::PaintCall{canvas, paint_bounds1, false, nullptr, sampling},
MockTexture::PaintCall{canvas, paint_bounds2, true, nullptr, sampling}};
auto texture = std::make_shared<MockTexture>(0);

texture->Paint(canvas, paint_bounds1, false, nullptr, sampling);
texture->Paint(canvas, paint_bounds2, true, nullptr, sampling);
Texture::PaintContext context{
.canvas = &canvas,
};
texture->Paint(context, paint_bounds1, false, sampling);
texture->Paint(context, paint_bounds2, true, sampling);
EXPECT_EQ(texture->paint_calls(), expected_paint_calls);
}

Expand Down
2 changes: 1 addition & 1 deletion impeller/display_list/display_list_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ void DisplayListDispatcher::drawImageRect(
std::make_shared<Image>(image->impeller_texture()), // image
ToRect(src), // source rect
ToRect(dst), // destination rect
paint_, // paint
render_with_attributes ? paint_ : Paint(), // paint
ToSamplerDescriptor(sampling) // sampling
);
}
Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/backend/metal/context_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace impeller {
class ContextMTL final : public Context,
public BackendCast<ContextMTL, Context> {
public:
static std::shared_ptr<Context> Create(
static std::shared_ptr<ContextMTL> Create(
const std::vector<std::string>& shader_library_paths);

static std::shared_ptr<Context> Create(
static std::shared_ptr<ContextMTL> Create(
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
const std::string& label);

Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/backend/metal/context_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
return ::MTLCreateSystemDefaultDevice();
}

std::shared_ptr<Context> ContextMTL::Create(
std::shared_ptr<ContextMTL> ContextMTL::Create(
const std::vector<std::string>& shader_library_paths) {
auto device = CreateMetalDevice();
auto context = std::shared_ptr<ContextMTL>(new ContextMTL(
Expand All @@ -164,7 +164,7 @@
return context;
}

std::shared_ptr<Context> ContextMTL::Create(
std::shared_ptr<ContextMTL> ContextMTL::Create(
const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
const std::string& label) {
auto device = CreateMetalDevice();
Expand Down
10 changes: 9 additions & 1 deletion impeller/renderer/backend/metal/texture_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ namespace impeller {
class TextureMTL final : public Texture,
public BackendCast<TextureMTL, Texture> {
public:
TextureMTL(TextureDescriptor desc, id<MTLTexture> texture);
TextureMTL(TextureDescriptor desc,
id<MTLTexture> texture,
bool wrapped = false);

static std::shared_ptr<TextureMTL> Wrapper(TextureDescriptor desc,
id<MTLTexture> texture);

// |Texture|
~TextureMTL() override;

id<MTLTexture> GetMTLTexture() const;

bool IsWrapped() const;

private:
id<MTLTexture> texture_ = nullptr;
bool is_valid_ = false;
bool is_wrapped_ = false;

// |Texture|
void SetLabel(std::string_view label) override;
Expand Down
17 changes: 15 additions & 2 deletions impeller/renderer/backend/metal/texture_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include "impeller/renderer/backend/metal/texture_mtl.h"

#include "impeller/base/validation.h"
#include "impeller/renderer/texture_descriptor.h"

namespace impeller {

TextureMTL::TextureMTL(TextureDescriptor p_desc, id<MTLTexture> texture)
TextureMTL::TextureMTL(TextureDescriptor p_desc,
id<MTLTexture> texture,
bool wrapped)
: Texture(p_desc), texture_(texture) {
const auto& desc = GetTextureDescriptor();

Expand All @@ -21,9 +24,15 @@
return;
}

is_wrapped_ = wrapped;
is_valid_ = true;
}

std::shared_ptr<TextureMTL> TextureMTL::Wrapper(TextureDescriptor desc,
id<MTLTexture> texture) {
return std::make_shared<TextureMTL>(desc, texture, true);
}

TextureMTL::~TextureMTL() = default;

void TextureMTL::SetLabel(std::string_view label) {
Expand All @@ -42,7 +51,7 @@
bool TextureMTL::OnSetContents(const uint8_t* contents,
size_t length,
size_t slice) {
if (!IsValid() || !contents) {
if (!IsValid() || !contents || is_wrapped_) {
return false;
}

Expand Down Expand Up @@ -83,4 +92,8 @@
return is_valid_;
}

bool TextureMTL::IsWrapped() const {
return is_wrapped_;
}

} // namespace impeller
4 changes: 4 additions & 0 deletions impeller/renderer/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ bool Texture::IsSliceValid(size_t slice) const {
FML_UNREACHABLE();
}

void Texture::SetIntent(TextureIntent intent) {
intent_ = intent;
}

TextureIntent Texture::GetIntent() const {
return intent_;
}
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Texture {

const TextureDescriptor& GetTextureDescriptor() const;

void SetIntent(TextureIntent intent);

TextureIntent GetIntent() const;

virtual Scalar GetYCoordScale() const;
Expand Down
8 changes: 4 additions & 4 deletions shell/common/shell_test_platform_view_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/shell/gpu/gpu_surface_metal_skia.h"
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h"
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"

namespace flutter {
namespace testing {
Expand All @@ -30,12 +30,12 @@
class DarwinContextMetal {
public:
DarwinContextMetal()
: context_([[FlutterDarwinContextMetal alloc] initWithDefaultMTLDevice]),
: context_([[FlutterDarwinContextMetalSkia alloc] initWithDefaultMTLDevice]),
offscreen_texture_(CreateOffscreenTexture([context_.get() device])) {}

~DarwinContextMetal() = default;

fml::scoped_nsobject<FlutterDarwinContextMetal> context() const { return context_; }
fml::scoped_nsobject<FlutterDarwinContextMetalSkia> context() const { return context_; }

fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture() const { return offscreen_texture_; }

Expand All @@ -47,7 +47,7 @@ GPUMTLTextureInfo offscreen_texture_info() const {
}

private:
const fml::scoped_nsobject<FlutterDarwinContextMetal> context_;
const fml::scoped_nsobject<FlutterDarwinContextMetalSkia> context_;
const fml::scoped_nsprotocol<id<MTLTexture>> offscreen_texture_;

FML_DISALLOW_COPY_AND_ASSIGN(DarwinContextMetal);
Expand Down
6 changes: 2 additions & 4 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1850,12 +1850,10 @@ class MockTexture : public Texture {
~MockTexture() override = default;

// Called from raster thread.
void Paint(SkCanvas& canvas,
void Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions&,
const SkPaint* paint) override {}
const SkSamplingOptions&) override {}

void OnGrContextCreated() override {}

Expand Down
24 changes: 11 additions & 13 deletions shell/platform/android/android_external_texture_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ void AndroidExternalTextureGL::MarkNewFrameAvailable() {
new_frame_ready_ = true;
}

void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
void AndroidExternalTextureGL::Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling,
const SkPaint* paint) {
const SkSamplingOptions& sampling) {
if (state_ == AttachmentState::detached) {
return;
}
Expand All @@ -60,29 +58,29 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
GL_RGBA8_OES};
GrBackendTexture backendTexture(1, 1, GrMipMapped::kNo, textureInfo);
sk_sp<SkImage> image = SkImage::MakeFromTexture(
context, backendTexture, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
kPremul_SkAlphaType, nullptr);
context.gr_context, backendTexture, kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
if (image) {
SkAutoCanvasRestore autoRestore(&canvas, true);
SkAutoCanvasRestore autoRestore(context.canvas, true);

// The incoming texture is vertically flipped, so we flip it
// back. OpenGL's coordinate system has Positive Y equivalent to up, while
// Skia's coordinate system has Negative Y equvalent to up.
canvas.translate(bounds.x(), bounds.y() + bounds.height());
canvas.scale(bounds.width(), -bounds.height());
context.canvas->translate(bounds.x(), bounds.y() + bounds.height());
context.canvas->scale(bounds.width(), -bounds.height());

if (!transform.isIdentity()) {
sk_sp<SkShader> shader = image->makeShader(
SkTileMode::kRepeat, SkTileMode::kRepeat, sampling, transform);

SkPaint paintWithShader;
if (paint) {
paintWithShader = *paint;
if (context.sk_paint) {
paintWithShader = *context.sk_paint;
}
paintWithShader.setShader(shader);
canvas.drawRect(SkRect::MakeWH(1, 1), paintWithShader);
context.canvas->drawRect(SkRect::MakeWH(1, 1), paintWithShader);
} else {
canvas.drawImage(image, 0, 0, sampling, paint);
context.canvas->drawImage(image, 0, 0, sampling, context.sk_paint);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions shell/platform/android/android_external_texture_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ class AndroidExternalTextureGL : public flutter::Texture {

~AndroidExternalTextureGL() override;

void Paint(SkCanvas& canvas,
void Paint(PaintContext& context,
const SkRect& bounds,
bool freeze,
GrDirectContext* context,
const SkSamplingOptions& sampling,
const SkPaint* paint) override;
const SkSamplingOptions& sampling) override;

void OnGrContextCreated() override;

Expand Down
Loading