blob: 31350ee9894fcd57e4fb48dbb3c2b2d837bea6bf [file] [log] [blame]
Marissa Wallf6a73fa2018-12-10 10:41:08 -08001/*
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 Wall1be5a102019-01-18 16:14:04 -080017#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Marissa Wallf6a73fa2018-12-10 10:41:08 -080018#include <android/native_window.h>
19#include <android/surface_control.h>
Jorim Jaggi71db8892021-02-03 23:19:29 +010020#include <surface_control_private.h>
Marissa Wallf6a73fa2018-12-10 10:41:08 -080021
Marissa Wall1be5a102019-01-18 16:14:04 -080022#include <configstore/Utils.h>
23
24#include <gui/HdrMetadata.h>
25#include <gui/ISurfaceComposer.h>
Marissa Wallf6a73fa2018-12-10 10:41:08 -080026#include <gui/Surface.h>
27#include <gui/SurfaceComposerClient.h>
28#include <gui/SurfaceControl.h>
29
Marin Shalamanov463ad8e2021-01-28 22:58:37 +010030#include <ui/DynamicDisplayInfo.h>
Marissa Wall1be5a102019-01-18 16:14:04 -080031
32#include <utils/Timers.h>
33
34using namespace android::hardware::configstore;
35using namespace android::hardware::configstore::V1_0;
Marissa Wallf6a73fa2018-12-10 10:41:08 -080036using namespace android;
37
38using Transaction = SurfaceComposerClient::Transaction;
39
40#define CHECK_NOT_NULL(name) \
41 LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument");
42
43#define CHECK_VALID_RECT(name) \
44 LOG_ALWAYS_FATAL_IF(!static_cast<const Rect&>(name).isValid(), \
45 "invalid arg passed as " #name " argument");
46
John Reck2b2ba932021-07-12 21:51:09 -040047static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
48static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) ==
49 static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
50static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
51static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
52static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) ==
53 static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
54static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
Valerie Hau5bbfd512019-01-22 17:39:43 -080055
Marissa Wallf6a73fa2018-12-10 10:41:08 -080056Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) {
57 return reinterpret_cast<Transaction*>(aSurfaceTransaction);
58}
59
60SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) {
61 return reinterpret_cast<SurfaceControl*>(aSurfaceControl);
62}
63
64void SurfaceControl_acquire(SurfaceControl* surfaceControl) {
65 // incStrong/decStrong token must be the same, doesn't matter what it is
66 surfaceControl->incStrong((void*)SurfaceControl_acquire);
67}
68
69void SurfaceControl_release(SurfaceControl* surfaceControl) {
70 // incStrong/decStrong token must be the same, doesn't matter what it is
71 surfaceControl->decStrong((void*)SurfaceControl_acquire);
72}
73
74ASurfaceControl* ASurfaceControl_createFromWindow(ANativeWindow* window, const char* debug_name) {
75 CHECK_NOT_NULL(window);
76 CHECK_NOT_NULL(debug_name);
77
78 sp<SurfaceComposerClient> client = new SurfaceComposerClient();
79 if (client->initCheck() != NO_ERROR) {
80 return nullptr;
81 }
82
Vishnu Nairce1a6482020-10-22 17:41:30 -070083 Surface* surface = static_cast<Surface*>(window);
84 sp<IBinder> parentHandle = surface->getSurfaceControlHandle();
85
Marissa Wallf6a73fa2018-12-10 10:41:08 -080086 uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
Vishnu Nairce1a6482020-10-22 17:41:30 -070087 sp<SurfaceControl> surfaceControl;
88 if (parentHandle) {
89 surfaceControl =
90 client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */,
91 // Format is only relevant for buffer queue layers.
92 PIXEL_FORMAT_UNKNOWN /* format */, flags, parentHandle);
93 } else {
94 surfaceControl =
95 client->createWithSurfaceParent(String8(debug_name), 0 /* width */, 0 /* height */,
96 // Format is only relevant for buffer queue layers.
97 PIXEL_FORMAT_UNKNOWN /* format */, flags,
98 static_cast<Surface*>(window));
99 }
100
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800101 if (!surfaceControl) {
102 return nullptr;
103 }
104
105 SurfaceControl_acquire(surfaceControl.get());
106 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
107}
108
109ASurfaceControl* ASurfaceControl_create(ASurfaceControl* parent, const char* debug_name) {
110 CHECK_NOT_NULL(parent);
111 CHECK_NOT_NULL(debug_name);
112
113 SurfaceComposerClient* client = ASurfaceControl_to_SurfaceControl(parent)->getClient().get();
114
115 SurfaceControl* surfaceControlParent = ASurfaceControl_to_SurfaceControl(parent);
116
117 uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
118 sp<SurfaceControl> surfaceControl =
119 client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */,
120 // Format is only relevant for buffer queue layers.
121 PIXEL_FORMAT_UNKNOWN /* format */, flags,
Vishnu Nairce1a6482020-10-22 17:41:30 -0700122 surfaceControlParent->getHandle());
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800123 if (!surfaceControl) {
124 return nullptr;
125 }
126
127 SurfaceControl_acquire(surfaceControl.get());
128 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
129}
130
Huihong Luo91697e12021-01-28 15:24:19 -0800131void ASurfaceControl_acquire(ASurfaceControl* aSurfaceControl) {
132 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800133
Huihong Luo91697e12021-01-28 15:24:19 -0800134 SurfaceControl_acquire(surfaceControl);
135}
136
137void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
138 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
139
140 SurfaceControl_release(surfaceControl);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800141}
142
Jorim Jaggi71db8892021-02-03 23:19:29 +0100143struct ASurfaceControlStats {
144 int64_t acquireTime;
145 sp<Fence> previousReleaseFence;
146 uint64_t frameNumber;
147};
148
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200149void ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl* control, int32_t id,
150 void* context,
151 ASurfaceControl_SurfaceStatsListener func) {
152 SurfaceStatsCallback callback = [func, control, id](void* callback_context, nsecs_t,
153 const sp<Fence>&,
154 const SurfaceStats& surfaceStats) {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100155 ASurfaceControlStats aSurfaceControlStats;
156
Jorim Jaggi71db8892021-02-03 23:19:29 +0100157 aSurfaceControlStats.acquireTime = surfaceStats.acquireTime;
158 aSurfaceControlStats.previousReleaseFence = surfaceStats.previousReleaseFence;
159 aSurfaceControlStats.frameNumber = surfaceStats.eventStats.frameNumber;
160
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200161 (*func)(callback_context, control, id, &aSurfaceControlStats);
Jorim Jaggi71db8892021-02-03 23:19:29 +0100162 };
Pablo Gamitobc9e5292021-08-23 17:12:29 +0200163
Jorim Jaggi71db8892021-02-03 23:19:29 +0100164 TransactionCompletedListener::getInstance()->addSurfaceStatsListener(context,
165 reinterpret_cast<void*>(func), ASurfaceControl_to_SurfaceControl(control), callback);
166}
167
Jorim Jaggi71db8892021-02-03 23:19:29 +0100168void ASurfaceControl_unregisterSurfaceStatsListener(void* context,
169 ASurfaceControl_SurfaceStatsListener func) {
170 TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context,
171 reinterpret_cast<void*>(func));
172}
173
174int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) {
175 return stats->acquireTime;
176}
177
178uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) {
179 return stats->frameNumber;
180}
181
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800182ASurfaceTransaction* ASurfaceTransaction_create() {
183 Transaction* transaction = new Transaction;
184 return reinterpret_cast<ASurfaceTransaction*>(transaction);
185}
186
187void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
188 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
189 delete transaction;
190}
191
192void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
193 CHECK_NOT_NULL(aSurfaceTransaction);
194
195 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
196
197 transaction->apply();
198}
199
Marissa Wall1be5a102019-01-18 16:14:04 -0800200struct ASurfaceTransactionStats {
201 std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
202 int64_t latchTime;
203 sp<Fence> presentFence;
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700204 bool transactionCompleted;
Marissa Wall1be5a102019-01-18 16:14:04 -0800205};
206
207int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
208 CHECK_NOT_NULL(aSurfaceTransactionStats);
209 return aSurfaceTransactionStats->latchTime;
210}
211
212int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
213 CHECK_NOT_NULL(aSurfaceTransactionStats);
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700214 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
215 "ASurfaceTransactionStats queried from an incomplete transaction callback");
216
Marissa Wall1be5a102019-01-18 16:14:04 -0800217 auto& presentFence = aSurfaceTransactionStats->presentFence;
218 return (presentFence) ? presentFence->dup() : -1;
219}
220
221void ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats* aSurfaceTransactionStats,
222 ASurfaceControl*** outASurfaceControls,
223 size_t* outASurfaceControlsSize) {
224 CHECK_NOT_NULL(aSurfaceTransactionStats);
225 CHECK_NOT_NULL(outASurfaceControls);
226 CHECK_NOT_NULL(outASurfaceControlsSize);
227
228 size_t size = aSurfaceTransactionStats->aSurfaceControlStats.size();
229
230 SurfaceControl** surfaceControls = new SurfaceControl*[size];
231 ASurfaceControl** aSurfaceControls = reinterpret_cast<ASurfaceControl**>(surfaceControls);
232
233 size_t i = 0;
234 for (auto& [aSurfaceControl, aSurfaceControlStats] : aSurfaceTransactionStats->aSurfaceControlStats) {
235 aSurfaceControls[i] = aSurfaceControl;
236 i++;
237 }
238
239 *outASurfaceControls = aSurfaceControls;
240 *outASurfaceControlsSize = size;
241}
242
243int64_t ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats* aSurfaceTransactionStats,
244 ASurfaceControl* aSurfaceControl) {
245 CHECK_NOT_NULL(aSurfaceTransactionStats);
246 CHECK_NOT_NULL(aSurfaceControl);
247
248 const auto& aSurfaceControlStats =
249 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
250 LOG_ALWAYS_FATAL_IF(
251 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
252 "ASurfaceControl not found");
253
254 return aSurfaceControlStats->second.acquireTime;
255}
256
257int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
258 ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
259 CHECK_NOT_NULL(aSurfaceTransactionStats);
260 CHECK_NOT_NULL(aSurfaceControl);
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700261 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
262 "ASurfaceTransactionStats queried from an incomplete transaction callback");
Marissa Wall1be5a102019-01-18 16:14:04 -0800263
264 const auto& aSurfaceControlStats =
265 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
266 LOG_ALWAYS_FATAL_IF(
267 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
268 "ASurfaceControl not found");
269
270 auto& previousReleaseFence = aSurfaceControlStats->second.previousReleaseFence;
271 return (previousReleaseFence) ? previousReleaseFence->dup() : -1;
272}
273
274void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
275 CHECK_NOT_NULL(aSurfaceControls);
276
277 SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
278 delete[] surfaceControls;
279}
280
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800281void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
282 ASurfaceTransaction_OnComplete func) {
283 CHECK_NOT_NULL(aSurfaceTransaction);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800284 CHECK_NOT_NULL(func);
285
286 TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
Marissa Wall1be5a102019-01-18 16:14:04 -0800287 nsecs_t latchTime,
288 const sp<Fence>& presentFence,
289 const std::vector<SurfaceControlStats>& surfaceControlStats) {
290 ASurfaceTransactionStats aSurfaceTransactionStats;
291
292 aSurfaceTransactionStats.latchTime = latchTime;
293 aSurfaceTransactionStats.presentFence = presentFence;
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700294 aSurfaceTransactionStats.transactionCompleted = true;
Marissa Wall1be5a102019-01-18 16:14:04 -0800295
296 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
297
Valerie Haud6a222e2020-01-29 14:27:09 -0800298 for (const auto& [surfaceControl, latchTime, acquireTime, presentFence, previousReleaseFence, transformHint, frameEvents] : surfaceControlStats) {
Marissa Wall1be5a102019-01-18 16:14:04 -0800299 ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
300 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
301 aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
302 }
303
304 (*func)(callback_context, &aSurfaceTransactionStats);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800305 };
306
307 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
308
309 transaction->addTransactionCompletedCallback(callback, context);
310}
311
Marissa Wall1be5a102019-01-18 16:14:04 -0800312void ASurfaceTransaction_reparent(ASurfaceTransaction* aSurfaceTransaction,
313 ASurfaceControl* aSurfaceControl,
314 ASurfaceControl* newParentASurfaceControl) {
315 CHECK_NOT_NULL(aSurfaceTransaction);
316 CHECK_NOT_NULL(aSurfaceControl);
317
318 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
319 sp<SurfaceControl> newParentSurfaceControl = ASurfaceControl_to_SurfaceControl(
320 newParentASurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800321 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
322
Pablo Gamito117040c2020-09-14 08:24:41 +0000323 transaction->reparent(surfaceControl, newParentSurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800324}
325
326void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
327 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800328 int8_t visibility) {
329 CHECK_NOT_NULL(aSurfaceTransaction);
330 CHECK_NOT_NULL(aSurfaceControl);
331
332 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
333 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
334
335 switch (visibility) {
336 case ASURFACE_TRANSACTION_VISIBILITY_SHOW:
337 transaction->show(surfaceControl);
338 break;
339 case ASURFACE_TRANSACTION_VISIBILITY_HIDE:
340 transaction->hide(surfaceControl);
341 break;
342 default:
343 LOG_ALWAYS_FATAL("invalid visibility %d", visibility);
344 }
345}
346
Marissa Wall1be5a102019-01-18 16:14:04 -0800347void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
348 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800349 int32_t z_order) {
350 CHECK_NOT_NULL(aSurfaceTransaction);
351 CHECK_NOT_NULL(aSurfaceControl);
352
353 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
354 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
355
356 transaction->setLayer(surfaceControl, z_order);
357}
358
Marissa Wall1be5a102019-01-18 16:14:04 -0800359void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
360 ASurfaceControl* aSurfaceControl,
361 AHardwareBuffer* buffer, int acquire_fence_fd) {
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800362 CHECK_NOT_NULL(aSurfaceTransaction);
363 CHECK_NOT_NULL(aSurfaceControl);
364
365 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
366 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
367
368 sp<GraphicBuffer> graphic_buffer(reinterpret_cast<GraphicBuffer*>(buffer));
369
370 transaction->setBuffer(surfaceControl, graphic_buffer);
Marissa Wall1be5a102019-01-18 16:14:04 -0800371 if (acquire_fence_fd != -1) {
372 sp<Fence> fence = new Fence(acquire_fence_fd);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800373 transaction->setAcquireFence(surfaceControl, fence);
374 }
375}
376
377void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction,
378 ASurfaceControl* aSurfaceControl, const ARect& source,
379 const ARect& destination, int32_t transform) {
380 CHECK_NOT_NULL(aSurfaceTransaction);
381 CHECK_NOT_NULL(aSurfaceControl);
chaviw87a07ea2021-04-29 09:04:41 -0500382 CHECK_VALID_RECT(source);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800383 CHECK_VALID_RECT(destination);
384
385 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
386 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
387
chaviw87a07ea2021-04-29 09:04:41 -0500388 Rect sourceRect = static_cast<const Rect&>(source);
389 Rect destRect = static_cast<const Rect&>(destination);
390 // Adjust the source so its top and left are not negative
391 sourceRect.left = std::max(sourceRect.left, 0);
392 sourceRect.top = std::max(sourceRect.top, 0);
393
394 if (!sourceRect.isValid()) {
395 sourceRect.makeInvalid();
396 }
chaviw9b2ac242021-04-27 15:52:09 -0500397 transaction->setBufferCrop(surfaceControl, sourceRect);
Vishnu Nair0d7aff72021-05-10 15:01:20 -0700398 transaction->setDestinationFrame(surfaceControl, destRect);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800399 transaction->setTransform(surfaceControl, transform);
Vishnu Nair1ad69542019-05-23 16:27:45 +0800400 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
401 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
402 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800403}
404
chaviwccf3e8b2021-03-25 15:28:44 -0500405void ASurfaceTransaction_setCrop(ASurfaceTransaction* aSurfaceTransaction,
406 ASurfaceControl* aSurfaceControl, const ARect& crop) {
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500407 CHECK_NOT_NULL(aSurfaceTransaction);
408 CHECK_NOT_NULL(aSurfaceControl);
chaviwccf3e8b2021-03-25 15:28:44 -0500409 CHECK_VALID_RECT(crop);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500410
411 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
412 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
413
chaviwccf3e8b2021-03-25 15:28:44 -0500414 transaction->setCrop(surfaceControl, static_cast<const Rect&>(crop));
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500415}
416
chaviwccf3e8b2021-03-25 15:28:44 -0500417void ASurfaceTransaction_setPosition(ASurfaceTransaction* aSurfaceTransaction,
418 ASurfaceControl* aSurfaceControl, int32_t x, int32_t y) {
419 CHECK_NOT_NULL(aSurfaceTransaction);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500420 CHECK_NOT_NULL(aSurfaceControl);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500421
422 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
423 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
424
chaviwccf3e8b2021-03-25 15:28:44 -0500425 transaction->setPosition(surfaceControl, x, y);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500426}
427
chaviwccf3e8b2021-03-25 15:28:44 -0500428void ASurfaceTransaction_setBufferTransform(ASurfaceTransaction* aSurfaceTransaction,
429 ASurfaceControl* aSurfaceControl, int32_t transform) {
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500430 CHECK_NOT_NULL(aSurfaceTransaction);
431 CHECK_NOT_NULL(aSurfaceControl);
432
433 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
434 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
435
436 transaction->setTransform(surfaceControl, transform);
437 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
438 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
439 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
440}
441
chaviwccf3e8b2021-03-25 15:28:44 -0500442void ASurfaceTransaction_setScale(ASurfaceTransaction* aSurfaceTransaction,
443 ASurfaceControl* aSurfaceControl, float xScale, float yScale) {
444 CHECK_NOT_NULL(aSurfaceTransaction);
445 CHECK_NOT_NULL(aSurfaceControl);
446 LOG_ALWAYS_FATAL_IF(xScale < 0, "negative value passed in for xScale");
447 LOG_ALWAYS_FATAL_IF(yScale < 0, "negative value passed in for yScale");
448
449 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
450 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
451
452 transaction->setMatrix(surfaceControl, xScale, 0, 0, yScale);
453}
454
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800455void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction,
456 ASurfaceControl* aSurfaceControl,
457 int8_t transparency) {
458 CHECK_NOT_NULL(aSurfaceTransaction);
459 CHECK_NOT_NULL(aSurfaceControl);
460
461 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
462 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
463
464 uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ?
465 layer_state_t::eLayerOpaque : 0;
466 transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque);
467}
468
Marissa Wall1be5a102019-01-18 16:14:04 -0800469void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
470 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800471 const ARect rects[], uint32_t count) {
472 CHECK_NOT_NULL(aSurfaceTransaction);
473 CHECK_NOT_NULL(aSurfaceControl);
474
475 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
476 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
477
478 Region region;
479 for (uint32_t i = 0; i < count; ++i) {
Marissa Wallbb9b14f2019-04-23 14:10:15 -0700480 region.orSelf(static_cast<const Rect&>(rects[i]));
481 }
482
483 // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an
484 // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing
485 // distinction for a public api. Instead, default both cases to be a fully damaged buffer.
486 if (count == 1 && region.getBounds().isEmpty()) {
487 transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION);
488 return;
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800489 }
490
491 transaction->setSurfaceDamageRegion(surfaceControl, region);
492}
Marissa Wall1be5a102019-01-18 16:14:04 -0800493
494void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction,
495 int64_t desiredPresentTime) {
496 CHECK_NOT_NULL(aSurfaceTransaction);
497
498 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
499
500 transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime));
501}
502
503void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction,
504 ASurfaceControl* aSurfaceControl,
505 float alpha) {
506 CHECK_NOT_NULL(aSurfaceTransaction);
507 CHECK_NOT_NULL(aSurfaceControl);
508
509 LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha");
510
511 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
512 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
513
514 transaction->setAlpha(surfaceControl, alpha);
515}
516
Marissa Wall7f24f792019-02-07 14:06:04 -0800517void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction,
518 ASurfaceControl* aSurfaceControl,
519 ADataSpace aDataSpace) {
520 CHECK_NOT_NULL(aSurfaceTransaction);
521 CHECK_NOT_NULL(aSurfaceControl);
522
523 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marissa Wall7f24f792019-02-07 14:06:04 -0800524 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Marissa Wall7f24f792019-02-07 14:06:04 -0800525 transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
526}
527
Marissa Wall1be5a102019-01-18 16:14:04 -0800528void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction,
529 ASurfaceControl* aSurfaceControl,
530 struct AHdrMetadata_smpte2086* metadata) {
531 CHECK_NOT_NULL(aSurfaceTransaction);
532 CHECK_NOT_NULL(aSurfaceControl);
533
534 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
535 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
536
537 HdrMetadata hdrMetadata;
538
539 if (metadata) {
540 hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x;
541 hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y;
542 hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x;
543 hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y;
544 hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x;
545 hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y;
546 hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x;
547 hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y;
548 hdrMetadata.smpte2086.minLuminance = metadata->minLuminance;
549 hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance;
550
551 hdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
552 } else {
553 hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
554 }
555
556 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
557}
558
559void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction,
560 ASurfaceControl* aSurfaceControl,
561 struct AHdrMetadata_cta861_3* metadata) {
562 CHECK_NOT_NULL(aSurfaceTransaction);
563 CHECK_NOT_NULL(aSurfaceControl);
564
565 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
566 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
567
568 HdrMetadata hdrMetadata;
569
570 if (metadata) {
571 hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel;
572 hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel;
573
574 hdrMetadata.validTypes |= HdrMetadata::CTA861_3;
575 } else {
576 hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
577 }
578
579 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
580}
Valerie Hau5bbfd512019-01-22 17:39:43 -0800581
582void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
583 ASurfaceControl* aSurfaceControl,
584 float r, float g, float b, float alpha,
585 ADataSpace dataspace) {
586 CHECK_NOT_NULL(aSurfaceTransaction);
587 CHECK_NOT_NULL(aSurfaceControl);
588
589 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Valerie Hau5bbfd512019-01-22 17:39:43 -0800590 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
591
592 half3 color;
593 color.r = r;
594 color.g = g;
595 color.b = b;
596
Marin Shalamanov511f9142021-03-16 18:03:30 +0100597 transaction->setBackgroundColor(surfaceControl, color, alpha,
598 static_cast<ui::Dataspace>(dataspace));
Valerie Hau5bbfd512019-01-22 17:39:43 -0800599}
Steven Thomas6cf051e2020-01-14 11:37:21 -0800600
601void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800602 ASurfaceControl* aSurfaceControl, float frameRate,
603 int8_t compatibility) {
Marin Shalamanov511f9142021-03-16 18:03:30 +0100604 ASurfaceTransaction_setFrameRateWithChangeStrategy(
605 aSurfaceTransaction, aSurfaceControl, frameRate, compatibility,
606 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
Marin Shalamanov41ffa8d2020-10-13 12:35:20 +0200607}
608
Marin Shalamanov511f9142021-03-16 18:03:30 +0100609void ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction* aSurfaceTransaction,
610 ASurfaceControl* aSurfaceControl,
611 float frameRate, int8_t compatibility,
612 int8_t changeFrameRateStrategy) {
Steven Thomas6cf051e2020-01-14 11:37:21 -0800613 CHECK_NOT_NULL(aSurfaceTransaction);
614 CHECK_NOT_NULL(aSurfaceControl);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800615 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800616 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marin Shalamanov511f9142021-03-16 18:03:30 +0100617 transaction->setFrameRate(surfaceControl, frameRate, compatibility, changeFrameRateStrategy);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800618}
Robert Carrf57c0162021-03-24 15:48:25 -0700619
620void ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction* aSurfaceTransaction,
621 ASurfaceControl* aSurfaceControl,
622 bool enableBackpressure) {
623 CHECK_NOT_NULL(aSurfaceControl);
624 CHECK_NOT_NULL(aSurfaceTransaction);
625
626 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
627 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
628
629 const uint32_t flags = enableBackpressure ?
630 layer_state_t::eEnableBackpressure : 0;
631 transaction->setFlags(surfaceControl, flags, layer_state_t::eEnableBackpressure);
632}
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700633
634void ASurfaceTransaction_setOnCommit(ASurfaceTransaction* aSurfaceTransaction, void* context,
635 ASurfaceTransaction_OnCommit func) {
636 CHECK_NOT_NULL(aSurfaceTransaction);
637 CHECK_NOT_NULL(func);
638
639 TransactionCompletedCallbackTakesContext callback =
640 [func](void* callback_context, nsecs_t latchTime, const sp<Fence>& /* presentFence */,
641 const std::vector<SurfaceControlStats>& surfaceControlStats) {
642 ASurfaceTransactionStats aSurfaceTransactionStats;
643 aSurfaceTransactionStats.latchTime = latchTime;
644 aSurfaceTransactionStats.transactionCompleted = false;
645
646 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
647 for (const auto&
648 [surfaceControl, latchTime, acquireTime, presentFence,
649 previousReleaseFence, transformHint,
650 frameEvents] : surfaceControlStats) {
651 ASurfaceControl* aSurfaceControl =
652 reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
653 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
654 }
655
656 (*func)(callback_context, &aSurfaceTransactionStats);
657 };
658
659 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
660
661 transaction->addTransactionCommittedCallback(callback, context);
Pablo Gamito88660d72021-08-09 14:37:56 +0000662}