| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 17 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 18 | #include <android/native_window.h> |
| 19 | #include <android/surface_control.h> |
| Bo Liu | 789103b | 2022-09-15 16:24:31 -0400 | [diff] [blame] | 20 | #include <android/surface_control_jni.h> |
| 21 | #include <android_runtime/android_view_SurfaceControl.h> |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 22 | #include <configstore/Utils.h> |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 23 | #include <gui/HdrMetadata.h> |
| 24 | #include <gui/ISurfaceComposer.h> |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 25 | #include <gui/Surface.h> |
| 26 | #include <gui/SurfaceComposerClient.h> |
| 27 | #include <gui/SurfaceControl.h> |
| Rachel Lee | b6c93aa | 2022-02-22 15:48:28 -0800 | [diff] [blame] | 28 | #include <private/android/choreographer.h> |
| 29 | #include <surface_control_private.h> |
| Marin Shalamanov | 463ad8e | 2021-01-28 22:58:37 +0100 | [diff] [blame] | 30 | #include <ui/DynamicDisplayInfo.h> |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 31 | #include <utils/Timers.h> |
| 32 | |
| Bo Liu | 789103b | 2022-09-15 16:24:31 -0400 | [diff] [blame] | 33 | #include <utility> |
| 34 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 35 | using namespace android::hardware::configstore; |
| 36 | using namespace android::hardware::configstore::V1_0; |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 37 | using namespace android; |
| 38 | |
| 39 | using Transaction = SurfaceComposerClient::Transaction; |
| 40 | |
| 41 | #define CHECK_NOT_NULL(name) \ |
| 42 | LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument"); |
| 43 | |
| 44 | #define CHECK_VALID_RECT(name) \ |
| 45 | LOG_ALWAYS_FATAL_IF(!static_cast<const Rect&>(name).isValid(), \ |
| 46 | "invalid arg passed as " #name " argument"); |
| 47 | |
| John Reck | 2b2ba93 | 2021-07-12 21:51:09 -0400 | [diff] [blame] | 48 | static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN)); |
| 49 | static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == |
| 50 | static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR)); |
| 51 | static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB)); |
| 52 | static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB)); |
| 53 | static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == |
| 54 | static_cast<int>(HAL_DATASPACE_DISPLAY_P3)); |
| 55 | static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ)); |
| Valerie Hau | 5bbfd51 | 2019-01-22 17:39:43 -0800 | [diff] [blame] | 56 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 57 | Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) { |
| 58 | return reinterpret_cast<Transaction*>(aSurfaceTransaction); |
| 59 | } |
| 60 | |
| 61 | SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) { |
| 62 | return reinterpret_cast<SurfaceControl*>(aSurfaceControl); |
| 63 | } |
| 64 | |
| 65 | void SurfaceControl_acquire(SurfaceControl* surfaceControl) { |
| 66 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 67 | surfaceControl->incStrong((void*)SurfaceControl_acquire); |
| 68 | } |
| 69 | |
| 70 | void SurfaceControl_release(SurfaceControl* surfaceControl) { |
| 71 | // incStrong/decStrong token must be the same, doesn't matter what it is |
| 72 | surfaceControl->decStrong((void*)SurfaceControl_acquire); |
| 73 | } |
| 74 | |
| 75 | ASurfaceControl* ASurfaceControl_createFromWindow(ANativeWindow* window, const char* debug_name) { |
| 76 | CHECK_NOT_NULL(window); |
| 77 | CHECK_NOT_NULL(debug_name); |
| 78 | |
| 79 | sp<SurfaceComposerClient> client = new SurfaceComposerClient(); |
| 80 | if (client->initCheck() != NO_ERROR) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| Vishnu Nair | ce1a648 | 2020-10-22 17:41:30 -0700 | [diff] [blame] | 84 | Surface* surface = static_cast<Surface*>(window); |
| 85 | sp<IBinder> parentHandle = surface->getSurfaceControlHandle(); |
| 86 | |
| Huihong Luo | 36b55bc | 2022-03-08 14:50:45 -0800 | [diff] [blame] | 87 | int32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState; |
| Vishnu Nair | ce1a648 | 2020-10-22 17:41:30 -0700 | [diff] [blame] | 88 | sp<SurfaceControl> surfaceControl; |
| 89 | if (parentHandle) { |
| 90 | surfaceControl = |
| 91 | client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */, |
| 92 | // Format is only relevant for buffer queue layers. |
| 93 | PIXEL_FORMAT_UNKNOWN /* format */, flags, parentHandle); |
| 94 | } else { |
| Huihong Luo | 36b55bc | 2022-03-08 14:50:45 -0800 | [diff] [blame] | 95 | // deprecated, this should no longer be used |
| 96 | surfaceControl = nullptr; |
| Vishnu Nair | ce1a648 | 2020-10-22 17:41:30 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 99 | if (!surfaceControl) { |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | SurfaceControl_acquire(surfaceControl.get()); |
| 104 | return reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| 105 | } |
| 106 | |
| 107 | ASurfaceControl* ASurfaceControl_create(ASurfaceControl* parent, const char* debug_name) { |
| 108 | CHECK_NOT_NULL(parent); |
| 109 | CHECK_NOT_NULL(debug_name); |
| 110 | |
| 111 | SurfaceComposerClient* client = ASurfaceControl_to_SurfaceControl(parent)->getClient().get(); |
| 112 | |
| 113 | SurfaceControl* surfaceControlParent = ASurfaceControl_to_SurfaceControl(parent); |
| 114 | |
| 115 | uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState; |
| 116 | sp<SurfaceControl> surfaceControl = |
| 117 | client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */, |
| 118 | // Format is only relevant for buffer queue layers. |
| 119 | PIXEL_FORMAT_UNKNOWN /* format */, flags, |
| Vishnu Nair | ce1a648 | 2020-10-22 17:41:30 -0700 | [diff] [blame] | 120 | surfaceControlParent->getHandle()); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 121 | if (!surfaceControl) { |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | SurfaceControl_acquire(surfaceControl.get()); |
| 126 | return reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| 127 | } |
| 128 | |
| Huihong Luo | 91697e1 | 2021-01-28 15:24:19 -0800 | [diff] [blame] | 129 | void ASurfaceControl_acquire(ASurfaceControl* aSurfaceControl) { |
| 130 | SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 131 | |
| Huihong Luo | 91697e1 | 2021-01-28 15:24:19 -0800 | [diff] [blame] | 132 | SurfaceControl_acquire(surfaceControl); |
| 133 | } |
| 134 | |
| 135 | void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) { |
| 136 | SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 137 | |
| 138 | SurfaceControl_release(surfaceControl); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 141 | ASurfaceControl* ASurfaceControl_fromJava(JNIEnv* env, jobject surfaceControlObj) { |
| 142 | LOG_ALWAYS_FATAL_IF(!env, "nullptr passed to ASurfaceControl_fromJava as env argument"); |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 143 | LOG_ALWAYS_FATAL_IF(!surfaceControlObj, |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 144 | "nullptr passed to ASurfaceControl_fromJava as surfaceControlObj argument"); |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 145 | SurfaceControl* surfaceControl = |
| 146 | android_view_SurfaceControl_getNativeSurfaceControl(env, surfaceControlObj); |
| 147 | LOG_ALWAYS_FATAL_IF(!surfaceControl, |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 148 | "surfaceControlObj passed to ASurfaceControl_fromJava is not valid"); |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 149 | SurfaceControl_acquire(surfaceControl); |
| 150 | return reinterpret_cast<ASurfaceControl*>(surfaceControl); |
| Bo Liu | 789103b | 2022-09-15 16:24:31 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 153 | struct ASurfaceControlStats { |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 154 | std::variant<int64_t, sp<Fence>> acquireTimeOrFence; |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 155 | sp<Fence> previousReleaseFence; |
| 156 | uint64_t frameNumber; |
| 157 | }; |
| 158 | |
| Pablo Gamito | bc9e529 | 2021-08-23 17:12:29 +0200 | [diff] [blame] | 159 | void ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl* control, int32_t id, |
| 160 | void* context, |
| 161 | ASurfaceControl_SurfaceStatsListener func) { |
| Pablo Gamito | 14b28ce9c | 2021-09-06 16:33:23 +0000 | [diff] [blame] | 162 | SurfaceStatsCallback callback = [func, id](void* callback_context, nsecs_t, const sp<Fence>&, |
| 163 | const SurfaceStats& surfaceStats) { |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 164 | ASurfaceControlStats aSurfaceControlStats; |
| 165 | |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 166 | aSurfaceControlStats.acquireTimeOrFence = surfaceStats.acquireTimeOrFence; |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 167 | aSurfaceControlStats.previousReleaseFence = surfaceStats.previousReleaseFence; |
| 168 | aSurfaceControlStats.frameNumber = surfaceStats.eventStats.frameNumber; |
| 169 | |
| Pablo Gamito | 14b28ce9c | 2021-09-06 16:33:23 +0000 | [diff] [blame] | 170 | (*func)(callback_context, id, &aSurfaceControlStats); |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 171 | }; |
| Pablo Gamito | bc9e529 | 2021-08-23 17:12:29 +0200 | [diff] [blame] | 172 | |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 173 | TransactionCompletedListener::getInstance()->addSurfaceStatsListener(context, |
| 174 | reinterpret_cast<void*>(func), ASurfaceControl_to_SurfaceControl(control), callback); |
| 175 | } |
| 176 | |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 177 | void ASurfaceControl_unregisterSurfaceStatsListener(void* context, |
| 178 | ASurfaceControl_SurfaceStatsListener func) { |
| 179 | TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context, |
| 180 | reinterpret_cast<void*>(func)); |
| 181 | } |
| 182 | |
| Rachel Lee | 93d2d0b | 2023-01-06 14:06:29 -0800 | [diff] [blame] | 183 | AChoreographer* ASurfaceControl_getChoreographer(ASurfaceControl* aSurfaceControl) { |
| 184 | LOG_ALWAYS_FATAL_IF(aSurfaceControl == nullptr, "aSurfaceControl should not be nullptr"); |
| 185 | SurfaceControl* surfaceControl = |
| 186 | ASurfaceControl_to_SurfaceControl(reinterpret_cast<ASurfaceControl*>(aSurfaceControl)); |
| 187 | if (!surfaceControl->isValid()) { |
| 188 | ALOGE("Attempted to get choreographer from invalid surface control"); |
| 189 | return nullptr; |
| 190 | } |
| 191 | SurfaceControl_acquire(surfaceControl); |
| 192 | return reinterpret_cast<AChoreographer*>(surfaceControl->getChoreographer().get()); |
| 193 | } |
| 194 | |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 195 | int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) { |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 196 | if (const auto* fence = std::get_if<sp<Fence>>(&stats->acquireTimeOrFence)) { |
| 197 | // We got a fence instead of the acquire time due to latch unsignaled. |
| 198 | // Ideally the client could just get the acquire time dericly from |
| 199 | // the fence instead of calling this function which needs to block. |
| 200 | (*fence)->waitForever("ASurfaceControlStats_getAcquireTime"); |
| 201 | return (*fence)->getSignalTime(); |
| 202 | } |
| 203 | |
| 204 | return std::get<int64_t>(stats->acquireTimeOrFence); |
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) { |
| 208 | return stats->frameNumber; |
| 209 | } |
| 210 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 211 | ASurfaceTransaction* ASurfaceTransaction_create() { |
| 212 | Transaction* transaction = new Transaction; |
| 213 | return reinterpret_cast<ASurfaceTransaction*>(transaction); |
| 214 | } |
| 215 | |
| 216 | void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) { |
| 217 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 218 | delete transaction; |
| 219 | } |
| 220 | |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 221 | ASurfaceTransaction* ASurfaceTransaction_fromJava(JNIEnv* env, jobject transactionObj) { |
| 222 | LOG_ALWAYS_FATAL_IF(!env, "nullptr passed to ASurfaceTransaction_fromJava as env argument"); |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 223 | LOG_ALWAYS_FATAL_IF(!transactionObj, |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 224 | "nullptr passed to ASurfaceTransaction_fromJava as transactionObj " |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 225 | "argument"); |
| 226 | Transaction* transaction = |
| 227 | android_view_SurfaceTransaction_getNativeSurfaceTransaction(env, transactionObj); |
| 228 | LOG_ALWAYS_FATAL_IF(!transaction, |
| Bo Liu | 23af702 | 2022-11-11 15:00:19 -0500 | [diff] [blame] | 229 | "surfaceControlObj passed to ASurfaceTransaction_fromJava is not valid"); |
| Bo Liu | dd89c3b | 2022-10-11 20:26:31 -0400 | [diff] [blame] | 230 | return reinterpret_cast<ASurfaceTransaction*>(transaction); |
| Bo Liu | 789103b | 2022-09-15 16:24:31 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 233 | void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) { |
| 234 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 235 | |
| 236 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 237 | |
| 238 | transaction->apply(); |
| 239 | } |
| 240 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 241 | struct ASurfaceTransactionStats { |
| 242 | std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats; |
| 243 | int64_t latchTime; |
| 244 | sp<Fence> presentFence; |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 245 | bool transactionCompleted; |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) { |
| 249 | CHECK_NOT_NULL(aSurfaceTransactionStats); |
| 250 | return aSurfaceTransactionStats->latchTime; |
| 251 | } |
| 252 | |
| 253 | int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) { |
| 254 | CHECK_NOT_NULL(aSurfaceTransactionStats); |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 255 | LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted, |
| 256 | "ASurfaceTransactionStats queried from an incomplete transaction callback"); |
| 257 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 258 | auto& presentFence = aSurfaceTransactionStats->presentFence; |
| 259 | return (presentFence) ? presentFence->dup() : -1; |
| 260 | } |
| 261 | |
| 262 | void ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats* aSurfaceTransactionStats, |
| 263 | ASurfaceControl*** outASurfaceControls, |
| 264 | size_t* outASurfaceControlsSize) { |
| 265 | CHECK_NOT_NULL(aSurfaceTransactionStats); |
| 266 | CHECK_NOT_NULL(outASurfaceControls); |
| 267 | CHECK_NOT_NULL(outASurfaceControlsSize); |
| 268 | |
| 269 | size_t size = aSurfaceTransactionStats->aSurfaceControlStats.size(); |
| 270 | |
| 271 | SurfaceControl** surfaceControls = new SurfaceControl*[size]; |
| 272 | ASurfaceControl** aSurfaceControls = reinterpret_cast<ASurfaceControl**>(surfaceControls); |
| 273 | |
| 274 | size_t i = 0; |
| 275 | for (auto& [aSurfaceControl, aSurfaceControlStats] : aSurfaceTransactionStats->aSurfaceControlStats) { |
| 276 | aSurfaceControls[i] = aSurfaceControl; |
| 277 | i++; |
| 278 | } |
| 279 | |
| 280 | *outASurfaceControls = aSurfaceControls; |
| 281 | *outASurfaceControlsSize = size; |
| 282 | } |
| 283 | |
| 284 | int64_t ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats* aSurfaceTransactionStats, |
| 285 | ASurfaceControl* aSurfaceControl) { |
| 286 | CHECK_NOT_NULL(aSurfaceTransactionStats); |
| 287 | CHECK_NOT_NULL(aSurfaceControl); |
| 288 | |
| 289 | const auto& aSurfaceControlStats = |
| 290 | aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl); |
| 291 | LOG_ALWAYS_FATAL_IF( |
| 292 | aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(), |
| 293 | "ASurfaceControl not found"); |
| 294 | |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 295 | return ASurfaceControlStats_getAcquireTime(&aSurfaceControlStats->second); |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int ASurfaceTransactionStats_getPreviousReleaseFenceFd( |
| 299 | ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) { |
| 300 | CHECK_NOT_NULL(aSurfaceTransactionStats); |
| 301 | CHECK_NOT_NULL(aSurfaceControl); |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 302 | LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted, |
| 303 | "ASurfaceTransactionStats queried from an incomplete transaction callback"); |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 304 | |
| 305 | const auto& aSurfaceControlStats = |
| 306 | aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl); |
| 307 | LOG_ALWAYS_FATAL_IF( |
| 308 | aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(), |
| 309 | "ASurfaceControl not found"); |
| 310 | |
| 311 | auto& previousReleaseFence = aSurfaceControlStats->second.previousReleaseFence; |
| 312 | return (previousReleaseFence) ? previousReleaseFence->dup() : -1; |
| 313 | } |
| 314 | |
| 315 | void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) { |
| 316 | CHECK_NOT_NULL(aSurfaceControls); |
| 317 | |
| 318 | SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls); |
| 319 | delete[] surfaceControls; |
| 320 | } |
| 321 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 322 | void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context, |
| 323 | ASurfaceTransaction_OnComplete func) { |
| 324 | CHECK_NOT_NULL(aSurfaceTransaction); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 325 | CHECK_NOT_NULL(func); |
| 326 | |
| 327 | TransactionCompletedCallbackTakesContext callback = [func](void* callback_context, |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 328 | nsecs_t latchTime, |
| 329 | const sp<Fence>& presentFence, |
| 330 | const std::vector<SurfaceControlStats>& surfaceControlStats) { |
| 331 | ASurfaceTransactionStats aSurfaceTransactionStats; |
| 332 | |
| 333 | aSurfaceTransactionStats.latchTime = latchTime; |
| 334 | aSurfaceTransactionStats.presentFence = presentFence; |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 335 | aSurfaceTransactionStats.transactionCompleted = true; |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 336 | |
| 337 | auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats; |
| 338 | |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 339 | for (const auto& [surfaceControl, latchTime, acquireTimeOrFence, presentFence, |
| Rob Carr | 7c70373 | 2022-09-27 00:57:27 +0000 | [diff] [blame] | 340 | previousReleaseFence, transformHint, frameEvents, ignore] : surfaceControlStats) { |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 341 | ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 342 | aSurfaceControlStats[aSurfaceControl].acquireTimeOrFence = acquireTimeOrFence; |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 343 | aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence; |
| 344 | } |
| 345 | |
| 346 | (*func)(callback_context, &aSurfaceTransactionStats); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 350 | |
| 351 | transaction->addTransactionCompletedCallback(callback, context); |
| 352 | } |
| 353 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 354 | void ASurfaceTransaction_reparent(ASurfaceTransaction* aSurfaceTransaction, |
| 355 | ASurfaceControl* aSurfaceControl, |
| 356 | ASurfaceControl* newParentASurfaceControl) { |
| 357 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 358 | CHECK_NOT_NULL(aSurfaceControl); |
| 359 | |
| 360 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 361 | sp<SurfaceControl> newParentSurfaceControl = ASurfaceControl_to_SurfaceControl( |
| 362 | newParentASurfaceControl); |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 363 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 364 | |
| Pablo Gamito | 117040c | 2020-09-14 08:24:41 +0000 | [diff] [blame] | 365 | transaction->reparent(surfaceControl, newParentSurfaceControl); |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction, |
| 369 | ASurfaceControl* aSurfaceControl, |
| Dan Albert | 21c4dd4 | 2024-04-03 21:01:09 +0000 | [diff] [blame] | 370 | ASurfaceTransactionVisibility visibility) { |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 371 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 372 | CHECK_NOT_NULL(aSurfaceControl); |
| 373 | |
| 374 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 375 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 376 | |
| 377 | switch (visibility) { |
| 378 | case ASURFACE_TRANSACTION_VISIBILITY_SHOW: |
| 379 | transaction->show(surfaceControl); |
| 380 | break; |
| 381 | case ASURFACE_TRANSACTION_VISIBILITY_HIDE: |
| 382 | transaction->hide(surfaceControl); |
| 383 | break; |
| 384 | default: |
| 385 | LOG_ALWAYS_FATAL("invalid visibility %d", visibility); |
| 386 | } |
| 387 | } |
| 388 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 389 | void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction, |
| 390 | ASurfaceControl* aSurfaceControl, |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 391 | int32_t z_order) { |
| 392 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 393 | CHECK_NOT_NULL(aSurfaceControl); |
| 394 | |
| 395 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 396 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 397 | |
| 398 | transaction->setLayer(surfaceControl, z_order); |
| 399 | } |
| 400 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 401 | void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction, |
| 402 | ASurfaceControl* aSurfaceControl, |
| 403 | AHardwareBuffer* buffer, int acquire_fence_fd) { |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 404 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 405 | CHECK_NOT_NULL(aSurfaceControl); |
| 406 | |
| 407 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 408 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 409 | |
| John Reck | 7f55b13 | 2021-09-20 14:42:29 -0400 | [diff] [blame] | 410 | sp<GraphicBuffer> graphic_buffer(GraphicBuffer::fromAHardwareBuffer(buffer)); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 411 | |
| chaviw | 5513c61 | 2021-09-15 15:26:23 -0500 | [diff] [blame] | 412 | std::optional<sp<Fence>> fence = std::nullopt; |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 413 | if (acquire_fence_fd != -1) { |
| chaviw | 5513c61 | 2021-09-15 15:26:23 -0500 | [diff] [blame] | 414 | fence = new Fence(acquire_fence_fd); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 415 | } |
| chaviw | 5513c61 | 2021-09-15 15:26:23 -0500 | [diff] [blame] | 416 | transaction->setBuffer(surfaceControl, graphic_buffer, fence); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| Vishnu Nair | 3f6b103 | 2024-09-16 05:19:46 +0000 | [diff] [blame] | 419 | void ASurfaceTransaction_setBufferWithRelease( |
| 420 | ASurfaceTransaction* aSurfaceTransaction, ASurfaceControl* aSurfaceControl, |
| 421 | AHardwareBuffer* buffer, int acquire_fence_fd, void* _Null_unspecified context, |
| 422 | ASurfaceTransaction_OnBufferRelease aReleaseCallback) { |
| 423 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 424 | CHECK_NOT_NULL(aSurfaceControl); |
| 425 | CHECK_NOT_NULL(aReleaseCallback); |
| 426 | |
| 427 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 428 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 429 | |
| 430 | sp<GraphicBuffer> graphic_buffer(GraphicBuffer::fromAHardwareBuffer(buffer)); |
| 431 | |
| 432 | std::optional<sp<Fence>> fence = std::nullopt; |
| 433 | if (acquire_fence_fd != -1) { |
| 434 | fence = new Fence(acquire_fence_fd); |
| 435 | } |
| 436 | |
| 437 | ReleaseBufferCallback releaseBufferCallback = |
| 438 | [context, |
| 439 | aReleaseCallback](const ReleaseCallbackId&, const sp<Fence>& releaseFence, |
| 440 | std::optional<uint32_t> /* currentMaxAcquiredBufferCount */) { |
| 441 | (*aReleaseCallback)(context, (releaseFence) ? releaseFence->dup() : -1); |
| 442 | }; |
| 443 | |
| 444 | transaction->setBuffer(surfaceControl, graphic_buffer, fence, /* frameNumber */ std::nullopt, |
| 445 | /* producerId */ 0, releaseBufferCallback); |
| 446 | } |
| 447 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 448 | void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction, |
| 449 | ASurfaceControl* aSurfaceControl, const ARect& source, |
| 450 | const ARect& destination, int32_t transform) { |
| 451 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 452 | CHECK_NOT_NULL(aSurfaceControl); |
| chaviw | 87a07ea | 2021-04-29 09:04:41 -0500 | [diff] [blame] | 453 | CHECK_VALID_RECT(source); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 454 | CHECK_VALID_RECT(destination); |
| 455 | |
| 456 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 457 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 458 | |
| chaviw | 87a07ea | 2021-04-29 09:04:41 -0500 | [diff] [blame] | 459 | Rect sourceRect = static_cast<const Rect&>(source); |
| 460 | Rect destRect = static_cast<const Rect&>(destination); |
| 461 | // Adjust the source so its top and left are not negative |
| 462 | sourceRect.left = std::max(sourceRect.left, 0); |
| 463 | sourceRect.top = std::max(sourceRect.top, 0); |
| 464 | |
| 465 | if (!sourceRect.isValid()) { |
| 466 | sourceRect.makeInvalid(); |
| 467 | } |
| chaviw | 9b2ac24 | 2021-04-27 15:52:09 -0500 | [diff] [blame] | 468 | transaction->setBufferCrop(surfaceControl, sourceRect); |
| Vishnu Nair | 0d7aff7 | 2021-05-10 15:01:20 -0700 | [diff] [blame] | 469 | transaction->setDestinationFrame(surfaceControl, destRect); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 470 | transaction->setTransform(surfaceControl, transform); |
| Vishnu Nair | 1ad6954 | 2019-05-23 16:27:45 +0800 | [diff] [blame] | 471 | bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) == |
| 472 | NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 473 | transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay); |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 476 | void ASurfaceTransaction_setCrop(ASurfaceTransaction* aSurfaceTransaction, |
| 477 | ASurfaceControl* aSurfaceControl, const ARect& crop) { |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 478 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 479 | CHECK_NOT_NULL(aSurfaceControl); |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 480 | CHECK_VALID_RECT(crop); |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 481 | |
| 482 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 483 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 484 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 485 | transaction->setCrop(surfaceControl, static_cast<const Rect&>(crop)); |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 488 | void ASurfaceTransaction_setPosition(ASurfaceTransaction* aSurfaceTransaction, |
| 489 | ASurfaceControl* aSurfaceControl, int32_t x, int32_t y) { |
| 490 | CHECK_NOT_NULL(aSurfaceTransaction); |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 491 | CHECK_NOT_NULL(aSurfaceControl); |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 492 | |
| 493 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 494 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 495 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 496 | transaction->setPosition(surfaceControl, x, y); |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 497 | } |
| 498 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 499 | void ASurfaceTransaction_setBufferTransform(ASurfaceTransaction* aSurfaceTransaction, |
| 500 | ASurfaceControl* aSurfaceControl, int32_t transform) { |
| Vasiliy Telezhnikov | 5ead3aa5 | 2021-03-13 19:55:00 -0500 | [diff] [blame] | 501 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 502 | CHECK_NOT_NULL(aSurfaceControl); |
| 503 | |
| 504 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 505 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 506 | |
| 507 | transaction->setTransform(surfaceControl, transform); |
| 508 | bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) == |
| 509 | NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; |
| 510 | transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay); |
| 511 | } |
| 512 | |
| chaviw | ccf3e8b | 2021-03-25 15:28:44 -0500 | [diff] [blame] | 513 | void ASurfaceTransaction_setScale(ASurfaceTransaction* aSurfaceTransaction, |
| 514 | ASurfaceControl* aSurfaceControl, float xScale, float yScale) { |
| 515 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 516 | CHECK_NOT_NULL(aSurfaceControl); |
| 517 | LOG_ALWAYS_FATAL_IF(xScale < 0, "negative value passed in for xScale"); |
| 518 | LOG_ALWAYS_FATAL_IF(yScale < 0, "negative value passed in for yScale"); |
| 519 | |
| 520 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 521 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 522 | |
| 523 | transaction->setMatrix(surfaceControl, xScale, 0, 0, yScale); |
| 524 | } |
| 525 | |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 526 | void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction, |
| 527 | ASurfaceControl* aSurfaceControl, |
| Dan Albert | 21c4dd4 | 2024-04-03 21:01:09 +0000 | [diff] [blame] | 528 | ASurfaceTransactionTransparency transparency) { |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 529 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 530 | CHECK_NOT_NULL(aSurfaceControl); |
| 531 | |
| 532 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 533 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 534 | |
| 535 | uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ? |
| 536 | layer_state_t::eLayerOpaque : 0; |
| 537 | transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque); |
| 538 | } |
| 539 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 540 | void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction, |
| 541 | ASurfaceControl* aSurfaceControl, |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 542 | const ARect rects[], uint32_t count) { |
| 543 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 544 | CHECK_NOT_NULL(aSurfaceControl); |
| 545 | |
| 546 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 547 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 548 | |
| 549 | Region region; |
| 550 | for (uint32_t i = 0; i < count; ++i) { |
| Marissa Wall | bb9b14f | 2019-04-23 14:10:15 -0700 | [diff] [blame] | 551 | region.orSelf(static_cast<const Rect&>(rects[i])); |
| 552 | } |
| 553 | |
| 554 | // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an |
| 555 | // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing |
| 556 | // distinction for a public api. Instead, default both cases to be a fully damaged buffer. |
| 557 | if (count == 1 && region.getBounds().isEmpty()) { |
| 558 | transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION); |
| 559 | return; |
| Marissa Wall | f6a73fa | 2018-12-10 10:41:08 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | transaction->setSurfaceDamageRegion(surfaceControl, region); |
| 563 | } |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 564 | |
| 565 | void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction, |
| 566 | int64_t desiredPresentTime) { |
| 567 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 568 | |
| 569 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 570 | |
| 571 | transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime)); |
| 572 | } |
| 573 | |
| 574 | void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction, |
| 575 | ASurfaceControl* aSurfaceControl, |
| 576 | float alpha) { |
| 577 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 578 | CHECK_NOT_NULL(aSurfaceControl); |
| 579 | |
| 580 | LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha"); |
| 581 | |
| 582 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 583 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 584 | |
| 585 | transaction->setAlpha(surfaceControl, alpha); |
| 586 | } |
| 587 | |
| Marissa Wall | 7f24f79 | 2019-02-07 14:06:04 -0800 | [diff] [blame] | 588 | void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction, |
| 589 | ASurfaceControl* aSurfaceControl, |
| 590 | ADataSpace aDataSpace) { |
| 591 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 592 | CHECK_NOT_NULL(aSurfaceControl); |
| 593 | |
| 594 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| Marissa Wall | 7f24f79 | 2019-02-07 14:06:04 -0800 | [diff] [blame] | 595 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| Marissa Wall | 7f24f79 | 2019-02-07 14:06:04 -0800 | [diff] [blame] | 596 | transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace)); |
| 597 | } |
| 598 | |
| Marissa Wall | 1be5a10 | 2019-01-18 16:14:04 -0800 | [diff] [blame] | 599 | void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction, |
| 600 | ASurfaceControl* aSurfaceControl, |
| 601 | struct AHdrMetadata_smpte2086* metadata) { |
| 602 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 603 | CHECK_NOT_NULL(aSurfaceControl); |
| 604 | |
| 605 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 606 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 607 | |
| 608 | HdrMetadata hdrMetadata; |
| 609 | |
| 610 | if (metadata) { |
| 611 | hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x; |
| 612 | hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y; |
| 613 | hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x; |
| 614 | hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y; |
| 615 | hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x; |
| 616 | hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y; |
| 617 | hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x; |
| 618 | hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y; |
| 619 | hdrMetadata.smpte2086.minLuminance = metadata->minLuminance; |
| 620 | hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance; |
| 621 | |
| 622 | hdrMetadata.validTypes |= HdrMetadata::SMPTE2086; |
| 623 | } else { |
| 624 | hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086; |
| 625 | } |
| 626 | |
| 627 | transaction->setHdrMetadata(surfaceControl, hdrMetadata); |
| 628 | } |
| 629 | |
| 630 | void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction, |
| 631 | ASurfaceControl* aSurfaceControl, |
| 632 | struct AHdrMetadata_cta861_3* metadata) { |
| 633 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 634 | CHECK_NOT_NULL(aSurfaceControl); |
| 635 | |
| 636 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 637 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 638 | |
| 639 | HdrMetadata hdrMetadata; |
| 640 | |
| 641 | if (metadata) { |
| 642 | hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel; |
| 643 | hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel; |
| 644 | |
| 645 | hdrMetadata.validTypes |= HdrMetadata::CTA861_3; |
| 646 | } else { |
| 647 | hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3; |
| 648 | } |
| 649 | |
| 650 | transaction->setHdrMetadata(surfaceControl, hdrMetadata); |
| 651 | } |
| Valerie Hau | 5bbfd51 | 2019-01-22 17:39:43 -0800 | [diff] [blame] | 652 | |
| John Reck | a40ad03 | 2023-02-13 10:20:24 -0500 | [diff] [blame] | 653 | void ASurfaceTransaction_setExtendedRangeBrightness(ASurfaceTransaction* aSurfaceTransaction, |
| 654 | ASurfaceControl* aSurfaceControl, |
| 655 | float currentBufferRatio, float desiredRatio) { |
| 656 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 657 | CHECK_NOT_NULL(aSurfaceControl); |
| 658 | |
| 659 | if (!isfinite(currentBufferRatio) || currentBufferRatio < 1.0f) { |
| John Reck | 732442f | 2023-03-16 14:28:50 -0400 | [diff] [blame] | 660 | LOG_ALWAYS_FATAL("setExtendedRangeBrightness, currentBufferRatio %f isn't finite or >= " |
| 661 | "1.0f", |
| 662 | currentBufferRatio); |
| John Reck | a40ad03 | 2023-02-13 10:20:24 -0500 | [diff] [blame] | 663 | return; |
| 664 | } |
| 665 | |
| 666 | if (!isfinite(desiredRatio) || desiredRatio < 1.0f) { |
| John Reck | 732442f | 2023-03-16 14:28:50 -0400 | [diff] [blame] | 667 | LOG_ALWAYS_FATAL("setExtendedRangeBrightness, desiredRatio %f isn't finite or >= 1.0f", |
| 668 | desiredRatio); |
| John Reck | a40ad03 | 2023-02-13 10:20:24 -0500 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
| 672 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 673 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 674 | |
| 675 | transaction->setExtendedRangeBrightness(surfaceControl, currentBufferRatio, desiredRatio); |
| 676 | } |
| 677 | |
| Alec Mouri | f5b4b66 | 2024-02-10 22:47:59 +0000 | [diff] [blame] | 678 | void ASurfaceTransaction_setDesiredHdrHeadroom(ASurfaceTransaction* aSurfaceTransaction, |
| 679 | ASurfaceControl* aSurfaceControl, |
| 680 | float desiredRatio) { |
| 681 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 682 | CHECK_NOT_NULL(aSurfaceControl); |
| 683 | |
| 684 | if (!isfinite(desiredRatio) || (desiredRatio < 1.0f && desiredRatio > 0.0f)) { |
| 685 | LOG_ALWAYS_FATAL("setDesiredHdrHeadroom, desiredRatio isn't finite && >= 1.0f or 0, got %f", |
| 686 | desiredRatio); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 691 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 692 | |
| 693 | transaction->setDesiredHdrHeadroom(surfaceControl, desiredRatio); |
| 694 | } |
| 695 | |
| Valerie Hau | 5bbfd51 | 2019-01-22 17:39:43 -0800 | [diff] [blame] | 696 | void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction, |
| 697 | ASurfaceControl* aSurfaceControl, |
| 698 | float r, float g, float b, float alpha, |
| 699 | ADataSpace dataspace) { |
| 700 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 701 | CHECK_NOT_NULL(aSurfaceControl); |
| 702 | |
| 703 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| Valerie Hau | 5bbfd51 | 2019-01-22 17:39:43 -0800 | [diff] [blame] | 704 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 705 | |
| 706 | half3 color; |
| 707 | color.r = r; |
| 708 | color.g = g; |
| 709 | color.b = b; |
| 710 | |
| Marin Shalamanov | 511f914 | 2021-03-16 18:03:30 +0100 | [diff] [blame] | 711 | transaction->setBackgroundColor(surfaceControl, color, alpha, |
| 712 | static_cast<ui::Dataspace>(dataspace)); |
| Valerie Hau | 5bbfd51 | 2019-01-22 17:39:43 -0800 | [diff] [blame] | 713 | } |
| Steven Thomas | 6cf051e | 2020-01-14 11:37:21 -0800 | [diff] [blame] | 714 | |
| 715 | void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction, |
| Steven Thomas | dd7bf2f | 2020-01-31 18:50:02 -0800 | [diff] [blame] | 716 | ASurfaceControl* aSurfaceControl, float frameRate, |
| 717 | int8_t compatibility) { |
| Marin Shalamanov | 511f914 | 2021-03-16 18:03:30 +0100 | [diff] [blame] | 718 | ASurfaceTransaction_setFrameRateWithChangeStrategy( |
| 719 | aSurfaceTransaction, aSurfaceControl, frameRate, compatibility, |
| 720 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS); |
| Marin Shalamanov | 41ffa8d | 2020-10-13 12:35:20 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| Marin Shalamanov | 511f914 | 2021-03-16 18:03:30 +0100 | [diff] [blame] | 723 | void ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction* aSurfaceTransaction, |
| 724 | ASurfaceControl* aSurfaceControl, |
| 725 | float frameRate, int8_t compatibility, |
| 726 | int8_t changeFrameRateStrategy) { |
| Steven Thomas | 6cf051e | 2020-01-14 11:37:21 -0800 | [diff] [blame] | 727 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 728 | CHECK_NOT_NULL(aSurfaceControl); |
| Steven Thomas | 6cf051e | 2020-01-14 11:37:21 -0800 | [diff] [blame] | 729 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| Steven Thomas | dd7bf2f | 2020-01-31 18:50:02 -0800 | [diff] [blame] | 730 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| Marin Shalamanov | 511f914 | 2021-03-16 18:03:30 +0100 | [diff] [blame] | 731 | transaction->setFrameRate(surfaceControl, frameRate, compatibility, changeFrameRateStrategy); |
| Steven Thomas | 6cf051e | 2020-01-14 11:37:21 -0800 | [diff] [blame] | 732 | } |
| Robert Carr | f57c016 | 2021-03-24 15:48:25 -0700 | [diff] [blame] | 733 | |
| Kriti Dang | 4bfeeb4 | 2022-08-11 15:15:36 +0200 | [diff] [blame] | 734 | void ASurfaceTransaction_clearFrameRate(ASurfaceTransaction* aSurfaceTransaction, |
| 735 | ASurfaceControl* aSurfaceControl) { |
| 736 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 737 | CHECK_NOT_NULL(aSurfaceControl); |
| 738 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 739 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 740 | transaction->setFrameRate(surfaceControl, 0, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, |
| 741 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS); |
| 742 | } |
| 743 | |
| Robert Carr | f57c016 | 2021-03-24 15:48:25 -0700 | [diff] [blame] | 744 | void ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction* aSurfaceTransaction, |
| 745 | ASurfaceControl* aSurfaceControl, |
| 746 | bool enableBackpressure) { |
| 747 | CHECK_NOT_NULL(aSurfaceControl); |
| 748 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 749 | |
| 750 | sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl); |
| 751 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 752 | |
| 753 | const uint32_t flags = enableBackpressure ? |
| 754 | layer_state_t::eEnableBackpressure : 0; |
| 755 | transaction->setFlags(surfaceControl, flags, layer_state_t::eEnableBackpressure); |
| 756 | } |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 757 | |
| 758 | void ASurfaceTransaction_setOnCommit(ASurfaceTransaction* aSurfaceTransaction, void* context, |
| 759 | ASurfaceTransaction_OnCommit func) { |
| 760 | CHECK_NOT_NULL(aSurfaceTransaction); |
| 761 | CHECK_NOT_NULL(func); |
| 762 | |
| 763 | TransactionCompletedCallbackTakesContext callback = |
| 764 | [func](void* callback_context, nsecs_t latchTime, const sp<Fence>& /* presentFence */, |
| 765 | const std::vector<SurfaceControlStats>& surfaceControlStats) { |
| 766 | ASurfaceTransactionStats aSurfaceTransactionStats; |
| 767 | aSurfaceTransactionStats.latchTime = latchTime; |
| 768 | aSurfaceTransactionStats.transactionCompleted = false; |
| 769 | |
| 770 | auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats; |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 771 | for (const auto& [surfaceControl, latchTime, acquireTimeOrFence, presentFence, |
| Rob Carr | 7c70373 | 2022-09-27 00:57:27 +0000 | [diff] [blame] | 772 | previousReleaseFence, transformHint, frameEvents, ignore] : |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 773 | surfaceControlStats) { |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 774 | ASurfaceControl* aSurfaceControl = |
| 775 | reinterpret_cast<ASurfaceControl*>(surfaceControl.get()); |
| Ady Abraham | 62e15f0 | 2022-01-21 17:00:50 -0800 | [diff] [blame] | 776 | aSurfaceControlStats[aSurfaceControl].acquireTimeOrFence = acquireTimeOrFence; |
| Vishnu Nair | beb3b48 | 2021-04-21 08:31:27 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | (*func)(callback_context, &aSurfaceTransactionStats); |
| 780 | }; |
| 781 | |
| 782 | Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction); |
| 783 | |
| 784 | transaction->addTransactionCommittedCallback(callback, context); |
| Pablo Gamito | 88660d7 | 2021-08-09 14:37:56 +0000 | [diff] [blame] | 785 | } |
| Rachel Lee | e1e77cc | 2021-10-19 16:40:41 -0700 | [diff] [blame] | 786 | |
| 787 | void ASurfaceTransaction_setFrameTimeline(ASurfaceTransaction* aSurfaceTransaction, |
| Rachel Lee | 7e47b3d | 2022-01-12 14:28:26 -0800 | [diff] [blame] | 788 | AVsyncId vsyncId) { |
| Rachel Lee | e1e77cc | 2021-10-19 16:40:41 -0700 | [diff] [blame] | 789 | CHECK_NOT_NULL(aSurfaceTransaction); |
| Rachel Lee | b6c93aa | 2022-02-22 15:48:28 -0800 | [diff] [blame] | 790 | const auto startTime = AChoreographer_getStartTimeNanosForVsyncId(vsyncId); |
| Huihong Luo | 4fed9b4 | 2022-03-03 15:10:33 -0800 | [diff] [blame] | 791 | FrameTimelineInfo ftInfo; |
| 792 | ftInfo.vsyncId = vsyncId; |
| 793 | ftInfo.startTimeNanos = startTime; |
| 794 | ASurfaceTransaction_to_Transaction(aSurfaceTransaction)->setFrameTimelineInfo(ftInfo); |
| Rachel Lee | e1e77cc | 2021-10-19 16:40:41 -0700 | [diff] [blame] | 795 | } |