| Flutter Impeller |

surface_transaction.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/impeller/toolkit/android/surface_transaction.h"
6
7 #include "flutter/impeller/toolkit/android/hardware_buffer.h"
8 #include "flutter/impeller/toolkit/android/surface_control.h"
9 #include "impeller/base/validation.h"
10
11 namespace impeller::android {
12
13 SurfaceTransaction::SurfaceTransaction()
14 : transaction_(
15 WrappedSurfaceTransaction{GetProcTable().ASurfaceTransaction_create(),
16 /*owned=*/true}) {}
17
18 SurfaceTransaction::SurfaceTransaction(ASurfaceTransaction* transaction)
19 : transaction_(WrappedSurfaceTransaction{transaction, /*owned=*/false}) {}
20
21 SurfaceTransaction::~SurfaceTransaction() = default;
22
23 bool SurfaceTransaction::IsValid() const {
24 return transaction_.is_valid();
25 }
26
27 struct TransactionInFlightData {
28 SurfaceTransaction::OnCompleteCallback callback;
29 };
30
31 bool SurfaceTransaction::Apply(OnCompleteCallback callback) {
32 if (!IsValid()) {
33 return false;
34 }
35
36 if (!callback) {
37 callback = [](auto) {};
38 }
39
40 const auto& proc_table = GetProcTable();
41
42 auto data = std::make_unique<TransactionInFlightData>();
43 data->callback = callback;
44 proc_table.ASurfaceTransaction_setOnComplete(
45 transaction_.get().tx, //
46 data.release(), //
47 [](void* context, ASurfaceTransactionStats* stats) -> void {
48 auto data = reinterpret_cast<TransactionInFlightData*>(context);
49 data->callback(stats);
50 delete data;
51 });
52 // If the transaction was created in Java, then it must be applied in
53 // the Java PlatformViewController and not as a part of the engine render
54 // loop.
55 if (!transaction_.get().owned) {
56 transaction_.reset();
57 return true;
58 }
59
60 proc_table.ASurfaceTransaction_apply(transaction_.get().tx);
61
62 // Transactions may not be applied over and over.
63 transaction_.reset();
64 return true;
65 }
66
67 bool SurfaceTransaction::SetContents(const SurfaceControl* control,
68 const HardwareBuffer* buffer,
69 fml::UniqueFD acquire_fence) {
70 if (control == nullptr || buffer == nullptr) {
71 VALIDATION_LOG << "Invalid control or buffer.";
72 return false;
73 }
74 GetProcTable().ASurfaceTransaction_setBuffer(
75 transaction_.get().tx, //
76 control->GetHandle(), //
77 buffer->GetHandle(), //
78 acquire_fence.is_valid() ? acquire_fence.release() : -1 //
79 );
80 return true;
81 }
82
83 bool SurfaceTransaction::SetBackgroundColor(const SurfaceControl& control,
84 const Color& color) {
85 if (!IsValid() || !control.IsValid()) {
86 return false;
87 }
88 GetProcTable().ASurfaceTransaction_setColor(transaction_.get().tx, //
89 control.GetHandle(), //
90 color.red, //
91 color.green, //
92 color.blue, //
93 color.alpha, //
94 ADATASPACE_SRGB_LINEAR //
95 );
96 return true;
97 }
98
99 bool SurfaceTransaction::SetParent(const SurfaceControl& control,
100 const SurfaceControl* new_parent) {
101 if (!IsValid() || !control.IsValid()) {
102 return false;
103 }
104 if (new_parent && !new_parent->IsValid()) {
105 return false;
106 }
107 GetProcTable().ASurfaceTransaction_reparent(
108 transaction_.get().tx, //
109 control.GetHandle(), //
110 new_parent == nullptr ? nullptr : new_parent->GetHandle() //
111 );
112 return true;
113 }
114
115 bool SurfaceTransaction::IsAvailableOnPlatform() {
116 return GetProcTable().IsValid() &&
117 GetProcTable().ASurfaceTransaction_create.IsAvailable();
118 }
119
120 } // namespace impeller::android
impeller::android::HardwareBuffer
A wrapper for AHardwareBuffer https://developer.android.com/ndk/reference/group/a-hardware-buffer.
Definition: hardware_buffer.h:89
impeller::android::HardwareBuffer::GetHandle
AHardwareBuffer * GetHandle() const
Definition: hardware_buffer.cc:83
impeller::android::SurfaceControl
A wrapper for ASurfaceControl. https://developer.android.com/ndk/reference/group/native-activity#asur...
Definition: surface_control.h:22
impeller::android::SurfaceControl::IsValid
bool IsValid() const
Definition: surface_control.cc:31
impeller::android::SurfaceControl::GetHandle
ASurfaceControl * GetHandle() const
Definition: surface_control.cc:35
impeller::android::SurfaceTransaction::SetContents
bool SetContents(const SurfaceControl *control, const HardwareBuffer *buffer, fml::UniqueFD acquire_fence={})
Encodes that the updated contents of a surface control are specified by the given hardware buffer....
Definition: surface_transaction.cc:67
impeller::android::SurfaceTransaction::OnCompleteCallback
std::function< void(ASurfaceTransactionStats *)> OnCompleteCallback
Definition: surface_transaction.h:104
impeller::android::SurfaceTransaction::SurfaceTransaction
SurfaceTransaction()
Definition: surface_transaction.cc:13
impeller::android::SurfaceTransaction::IsValid
bool IsValid() const
Definition: surface_transaction.cc:23
impeller::android::SurfaceTransaction::Apply
bool Apply(OnCompleteCallback callback=nullptr)
Applies the updated encoded in the transaction and invokes the callback when the updated are complete...
Definition: surface_transaction.cc:31
impeller::android::SurfaceTransaction::~SurfaceTransaction
~SurfaceTransaction()
impeller::android::SurfaceTransaction::SetBackgroundColor
bool SetBackgroundColor(const SurfaceControl &control, const Color &color)
Encodes the updated background color of the surface control. The update will not be committed till th...
Definition: surface_transaction.cc:83
impeller::android::SurfaceTransaction::SetParent
bool SetParent(const SurfaceControl &control, const SurfaceControl *new_parent=nullptr)
Set the new parent control of the given control. If the new parent is null, it is removed from the co...
Definition: surface_transaction.cc:99
impeller::android::SurfaceTransaction::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: surface_transaction.cc:115
hardware_buffer.h
impeller::android
Definition: ahb_texture_source_vk_unittests.cc:16
impeller::android::GetProcTable
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
impeller::Color
Definition: color.h:124
impeller::Color::blue
Scalar blue
Definition: color.h:138
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::Color::red
Scalar red
Definition: color.h:128
impeller::Color::green
Scalar green
Definition: color.h:133
impeller::android::ProcTable::IsValid
bool IsValid() const
If a valid proc table could be setup. This may fail in case of setup on non-Android platforms.
Definition: proc_table.cc:65
impeller::android::TransactionInFlightData
Definition: surface_transaction.cc:27
impeller::android::TransactionInFlightData::callback
SurfaceTransaction::OnCompleteCallback callback
Definition: surface_transaction.cc:28
impeller::android::WrappedSurfaceTransaction
A wrapper class that indicates whether a SurfaceTransaction was created by the flutter engine or was ...
Definition: surface_transaction.h:24
surface_control.h
surface_transaction.h
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91