Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Android] SurfaceTransaction updates for HC++.
  • Loading branch information
jonahwilliams committed Jan 29, 2025
commit 8c78ea462efaca7f1bb7cd13d5a5429e12298a11
4 changes: 4 additions & 0 deletions engine/src/flutter/impeller/toolkit/android/proc_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

namespace impeller::android {

ASurfaceTransaction* ASurfaceTransaction_fromJava(JNIEnv* env,
jobject transaction);

//------------------------------------------------------------------------------
/// @brief The Android procs along with the device API level on which these
/// will be available. There is no checking of the actual API level
Expand Down Expand Up @@ -61,6 +64,7 @@ namespace impeller::android {
INVOKE(ASurfaceTransaction_setColor, 29) \
INVOKE(ASurfaceTransaction_setOnComplete, 29) \
INVOKE(ASurfaceTransactionStats_getPreviousReleaseFenceFd, 29) \
INVOKE(ASurfaceTransaction_fromJava, 34) \
INVOKE(ATrace_isEnabled, 23) \
INVOKE(eglGetNativeClientBufferANDROID, 0)

Expand Down
20 changes: 14 additions & 6 deletions engine/src/flutter/impeller/toolkit/android/surface_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ bool SurfaceTransaction::Apply(OnCompleteCallback callback) {
auto data = std::make_unique<TransactionInFlightData>();
data->callback = callback;
proc_table.ASurfaceTransaction_setOnComplete(
transaction_.get(), //
data.release(), //
transaction_.get().tx, //
data.release(), //
[](void* context, ASurfaceTransactionStats* stats) -> void {
auto data = reinterpret_cast<TransactionInFlightData*>(context);
data->callback(stats);
delete data;
});
proc_table.ASurfaceTransaction_apply(transaction_.get());
// If the transaction was created in Java, then it must be applied in
// the java PlatformViewController and not as a part of the engine render
Copy link
Contributor

Choose a reason for hiding this comment

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

s/java/Java

// loop.
if (!transaction_.get().owned) {
std::ignore = transaction_.reset();
return true;
}

proc_table.ASurfaceTransaction_apply(transaction_.get().tx);

// Transactions may not be applied over and over.
transaction_.reset();
Expand All @@ -59,7 +67,7 @@ bool SurfaceTransaction::SetContents(const SurfaceControl* control,
return false;
}
GetProcTable().ASurfaceTransaction_setBuffer(
transaction_.get(), //
transaction_.get().tx, //
control->GetHandle(), //
buffer->GetHandle(), //
acquire_fence.is_valid() ? acquire_fence.release() : -1 //
Expand All @@ -72,7 +80,7 @@ bool SurfaceTransaction::SetBackgroundColor(const SurfaceControl& control,
if (!IsValid() || !control.IsValid()) {
return false;
}
GetProcTable().ASurfaceTransaction_setColor(transaction_.get(), //
GetProcTable().ASurfaceTransaction_setColor(transaction_.get().tx, //
control.GetHandle(), //
color.red, //
color.green, //
Expand All @@ -92,7 +100,7 @@ bool SurfaceTransaction::SetParent(const SurfaceControl& control,
return false;
}
GetProcTable().ASurfaceTransaction_reparent(
transaction_.get(), //
transaction_.get().tx, //
control.GetHandle(), //
new_parent == nullptr ? nullptr : new_parent->GetHandle() //
);
Expand Down
32 changes: 26 additions & 6 deletions engine/src/flutter/impeller/toolkit/android/surface_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ namespace impeller::android {
class SurfaceControl;
class HardwareBuffer;

/// @brief A wrapper class that indicates whether a SurfaceTransaction was
/// created by the flutter engine or was borrowed from Java for platform
/// interop.
struct WrappedSurfaceTransaction {
ASurfaceTransaction* tx;
Copy link
Contributor

Choose a reason for hiding this comment

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

= nullptr;

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

bool owned;
Copy link
Contributor

Choose a reason for hiding this comment

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

= 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.

true is the safer default here but roger.


constexpr bool operator==(const WrappedSurfaceTransaction& other) const {
return other.tx == tx;
}

constexpr bool operator!=(const WrappedSurfaceTransaction& other) const {
return !(*this == other);
}
};

//------------------------------------------------------------------------------
/// @brief A wrapper for ASurfaceTransaction.
/// https://developer.android.com/ndk/reference/group/native-activity#asurfacetransaction
Expand Down Expand Up @@ -48,6 +64,8 @@ class SurfaceTransaction {

SurfaceTransaction& operator=(const SurfaceTransaction&) = delete;

explicit SurfaceTransaction(ASurfaceTransaction* transaction);

bool IsValid() const;

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -119,18 +137,20 @@ class SurfaceTransaction {

private:
struct UniqueASurfaceTransactionTraits {
static ASurfaceTransaction* InvalidValue() { return nullptr; }
static WrappedSurfaceTransaction InvalidValue() { return {}; }

static bool IsValid(ASurfaceTransaction* value) {
return value != InvalidValue();
static bool IsValid(const WrappedSurfaceTransaction& value) {
return value.tx != nullptr;
}

static void Free(ASurfaceTransaction* value) {
GetProcTable().ASurfaceTransaction_delete(value);
static void Free(const WrappedSurfaceTransaction& value) {
if (value.owned && value.tx) {
GetProcTable().ASurfaceTransaction_delete(value.tx);
}
}
};

fml::UniqueObject<ASurfaceTransaction*, UniqueASurfaceTransactionTraits>
fml::UniqueObject<WrappedSurfaceTransaction, UniqueASurfaceTransactionTraits>
transaction_;
};

Expand Down