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
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ FILE: ../../../flutter/fml/platform/android/scoped_java_ref.cc
FILE: ../../../flutter/fml/platform/android/scoped_java_ref.h
FILE: ../../../flutter/fml/platform/darwin/cf_utils.cc
FILE: ../../../flutter/fml/platform/darwin/cf_utils.h
FILE: ../../../flutter/fml/platform/darwin/cf_utils_unittests.mm
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.h
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.mm
FILE: ../../../flutter/fml/platform/darwin/paths_darwin.mm
Expand Down Expand Up @@ -890,6 +891,8 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_software.h
FILE: ../../../flutter/shell/platform/darwin/ios/ios_context_software.mm
FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_gl.h
FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm
FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_metal.h
FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_metal.mm
FILE: ../../../flutter/shell/platform/darwin/ios/ios_render_target_gl.h
FILE: ../../../flutter/shell/platform/darwin/ios/ios_render_target_gl.mm
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.h
Expand Down
11 changes: 9 additions & 2 deletions flow/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ Texture::~Texture() = default;
TextureRegistry::TextureRegistry() = default;

void TextureRegistry::RegisterTexture(std::shared_ptr<Texture> texture) {
if (!texture) {
return;
}
mapping_[texture->Id()] = texture;
}

void TextureRegistry::UnregisterTexture(int64_t id) {
mapping_[id]->OnTextureUnregistered();
mapping_.erase(id);
auto found = mapping_.find(id);
if (found == mapping_.end()) {
return;
}
found->second->OnTextureUnregistered();
mapping_.erase(found);
}

void TextureRegistry::OnGrContextCreated() {
Expand Down
8 changes: 7 additions & 1 deletion fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ executable("fml_unittests") {
"message_loop_unittests.cc",
"message_unittests.cc",
"paths_unittests.cc",
"platform/darwin/string_range_sanitization_unittests.mm",
"synchronization/count_down_latch_unittests.cc",
"synchronization/semaphore_unittest.cc",
"synchronization/sync_switch_unittest.cc",
Expand All @@ -264,6 +263,13 @@ executable("fml_unittests") {
"time/time_unittest.cc",
]

if (is_mac) {
sources += [
"platform/darwin/cf_utils_unittests.mm",
"platform/darwin/string_range_sanitization_unittests.mm",
]
}

deps = [
":fml_fixtures",
"//flutter/fml",
Expand Down
25 changes: 23 additions & 2 deletions fml/platform/darwin/cf_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ class CFRef {

CFRef(T instance) : instance_(instance) {}

CFRef(const CFRef& other) : instance_(other.instance_) {
if (instance_) {
CFRetain(instance_);
}
}

CFRef(CFRef&& other) : instance_(other.instance_) {
other.instance_ = nullptr;
}

CFRef& operator=(CFRef&& other) {
Reset(other.Release());
return *this;
}

~CFRef() {
if (instance_ != nullptr) {
CFRelease(instance_);
}
instance_ = nullptr;
}

void Reset(T instance) {
void Reset(T instance = nullptr) {
if (instance_ == instance) {
return;
}
Expand All @@ -36,14 +51,20 @@ class CFRef {
instance_ = instance;
}

[[nodiscard]] T Release() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we on C++ 17 everywhere we have to compile? Particularly google3?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes.

auto instance = instance_;
instance_ = nullptr;
return instance;
}

operator T() const { return instance_; }

operator bool() const { return instance_ != nullptr; }

private:
T instance_;

FML_DISALLOW_COPY_AND_ASSIGN(CFRef);
CFRef& operator=(const CFRef&) = delete;
};

} // namespace fml
Expand Down
63 changes: 63 additions & 0 deletions fml/platform/darwin/cf_utils_unittests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 "flutter/fml/platform/darwin/cf_utils.h"
#include "flutter/testing/testing.h"

namespace fml {
namespace testing {

TEST(CFTest, CanCreateRefs) {
CFRef<CFMutableStringRef> string(CFStringCreateMutable(kCFAllocatorDefault, 100u));
// Cast
ASSERT_TRUE(static_cast<bool>(string));
ASSERT_TRUE(string);

const auto ref_count = CFGetRetainCount(string);

// Copy & Reset
{
CFRef<CFMutableStringRef> string2 = string;
ASSERT_TRUE(string2);
ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
ASSERT_EQ(CFGetRetainCount(string2), CFGetRetainCount(string));

string2.Reset();
ASSERT_FALSE(string2);
ASSERT_EQ(ref_count, CFGetRetainCount(string));
}

// Release
{
auto string3 = string;
ASSERT_TRUE(string3);
ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
auto raw_string3 = string3.Release();
ASSERT_FALSE(string3);
ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
CFRelease(raw_string3);
ASSERT_EQ(ref_count, CFGetRetainCount(string));
}

// Move
{
auto string_source = string;
ASSERT_TRUE(string_source);
auto string_move = std::move(string_source);
ASSERT_FALSE(string_source);
ASSERT_EQ(ref_count + 1u, CFGetRetainCount(string));
string_move.Reset();
ASSERT_EQ(ref_count, CFGetRetainCount(string));
}

// Move assign.
{
auto string_move_assign = std::move(string);
ASSERT_FALSE(string);
ASSERT_EQ(ref_count, CFGetRetainCount(string_move_assign));
}
}

} // namespace testing
} // namespace fml
2 changes: 2 additions & 0 deletions shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ shared_library("create_flutter_framework_dylib") {
sources += [
"ios_context_metal.h",
"ios_context_metal.mm",
"ios_external_texture_metal.h",
"ios_external_texture_metal.mm",
"ios_surface_metal.h",
"ios_surface_metal.mm",
]
Expand Down
16 changes: 16 additions & 0 deletions shell/platform/darwin/ios/ios_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <memory>

#include "flutter/flow/texture.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
#include "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
#include "third_party/skia/include/gpu/GrContext.h"

Expand Down Expand Up @@ -119,6 +121,20 @@ class IOSContext {
///
virtual bool ClearCurrent() = 0;

//----------------------------------------------------------------------------
/// @brief Creates an external texture proxy of the appropriate client
/// rendering API.
///
/// @param[in] texture_id The texture identifier
/// @param[in] texture The texture
///
/// @return The texture proxy if the rendering backend supports embedder
/// provided external textures.
///
virtual std::unique_ptr<Texture> CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) = 0;

protected:
IOSContext();

Expand Down
5 changes: 5 additions & 0 deletions shell/platform/darwin/ios/ios_context_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class IOSContextGL final : public IOSContext {
// |IOSContext|
bool ResourceMakeCurrent() override;

// |IOSContext|
std::unique_ptr<Texture> CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) override;

FML_DISALLOW_COPY_AND_ASSIGN(IOSContextGL);
};

Expand Down
8 changes: 8 additions & 0 deletions shell/platform/darwin/ios/ios_context_gl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "flutter/shell/common/shell_io_manager.h"
#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
#include "flutter/shell/platform/darwin/ios/ios_external_texture_gl.h"

namespace flutter {

Expand Down Expand Up @@ -58,4 +59,11 @@
return [EAGLContext setCurrentContext:nil];
}

// |IOSContext|
std::unique_ptr<Texture> IOSContextGL::CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
return std::make_unique<IOSExternalTextureGL>(texture_id, std::move(texture));
}

} // namespace flutter
7 changes: 7 additions & 0 deletions shell/platform/darwin/ios/ios_context_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Metal/Metal.h>

#include "flutter/fml/macros.h"
#include "flutter/fml/platform/darwin/cf_utils.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/shell/platform/darwin/ios/ios_context.h"
#include "third_party/skia/include/gpu/GrContext.h"
Expand Down Expand Up @@ -35,6 +36,7 @@ class IOSContextMetal final : public IOSContext {
fml::scoped_nsprotocol<id<MTLCommandQueue>> main_queue_;
sk_sp<GrContext> main_context_;
sk_sp<GrContext> resource_context_;
fml::CFRef<CVMetalTextureCacheRef> texture_cache_;
bool is_valid_ = false;

// |IOSContext|
Expand All @@ -49,6 +51,11 @@ class IOSContextMetal final : public IOSContext {
// |IOSContext|
bool ClearCurrent() override;

// |IOSContext|
std::unique_ptr<Texture> CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) override;

FML_DISALLOW_COPY_AND_ASSIGN(IOSContextMetal);
};

Expand Down
27 changes: 24 additions & 3 deletions shell/platform/darwin/ios/ios_context_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
#include "flutter/shell/platform/darwin/ios/ios_context_metal.h"

#include "flutter/fml/logging.h"
#include "flutter/shell/platform/darwin/ios/ios_external_texture_metal.h"

namespace flutter {

IOSContextMetal::IOSContextMetal() {
device_.reset([MTLCreateSystemDefaultDevice() retain]);
if (!device_) {
FML_LOG(ERROR) << "Could not acquire Metal device.";
FML_DLOG(ERROR) << "Could not acquire Metal device.";
return;
}

main_queue_.reset([device_ newCommandQueue]);

if (!main_queue_) {
FML_LOG(ERROR) << "Could not create Metal command queue.";
FML_DLOG(ERROR) << "Could not create Metal command queue.";
return;
}

Expand All @@ -30,10 +31,23 @@
resource_context_ = GrContext::MakeMetal([device_ retain], [main_queue_ retain]);

if (!main_context_ || !resource_context_) {
FML_LOG(ERROR) << "Could not create Skia Metal contexts.";
FML_DLOG(ERROR) << "Could not create Skia Metal contexts.";
return;
}

CVMetalTextureCacheRef texture_cache_raw = NULL;
auto cv_return = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
NULL, // cache attributes (NULL default)
Copy link
Contributor

Choose a reason for hiding this comment

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

clang format doesn't line these up?

Copy link
Member Author

Choose a reason for hiding this comment

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

Apparently not. I don't format code so its got to be clang-format. I think its because the comments are pushed up against the column limit.

device_.get(), // metal device
NULL, // texture attributes (NULL default)
&texture_cache_raw // [out] cache
);
if (cv_return != kCVReturnSuccess) {
FML_DLOG(ERROR) << "Could not create Metal texture cache.";
return;
}
texture_cache_.Reset(texture_cache_raw);

is_valid_ = false;
}

Expand Down Expand Up @@ -83,4 +97,11 @@
return true;
}

// |IOSContext|
std::unique_ptr<Texture> IOSContextMetal::CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
return std::make_unique<IOSExternalTextureMetal>(texture_id, texture_cache_, std::move(texture));
}

} // namespace flutter
5 changes: 5 additions & 0 deletions shell/platform/darwin/ios/ios_context_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class IOSContextSoftware final : public IOSContext {
// |IOSContext|
bool ClearCurrent() override;

// |IOSContext|
std::unique_ptr<Texture> CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) override;

private:
FML_DISALLOW_COPY_AND_ASSIGN(IOSContextSoftware);
};
Expand Down
14 changes: 14 additions & 0 deletions shell/platform/darwin/ios/ios_context_software.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,18 @@
return false;
}

// |IOSContext|
std::unique_ptr<Texture> IOSContextSoftware::CreateExternalTexture(
int64_t texture_id,
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
// Don't use FML for logging as it will contain engine specific details. This is a user facing
// message.
NSLog(@"Flutter: Attempted to composite external texture sources using the software backend. "
@"This backend is only used on simulators. This feature is only available on actual "
@"devices where OpenGL or Metal is used for rendering.");

// Not supported in this backend.
return nullptr;
}

} // namespace flutter
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/ios_external_texture_gl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

bool IOSExternalTextureGL::NeedUpdateTexture(bool freeze) {
// Update texture if `texture_ref_` is reset to `nullptr` when GrContext
// is destroied or new frame is ready.
// is destroyed or new frame is ready.
return (!freeze && new_frame_ready_) || !texture_ref_;
}

Expand Down
Loading