blob: 1f246e52d45991467f3d37a0014b8356d25653ea [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
149void ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl* control, void* context,
150 ASurfaceControl_SurfaceStatsListener func) {
151 SurfaceStatsCallback callback = [func](void* callback_context,
152 nsecs_t,
153 const sp<Fence>&,
154 const SurfaceStats& surfaceStats) {
155
156 ASurfaceControlStats aSurfaceControlStats;
157
158 ASurfaceControl* aSurfaceControl =
159 reinterpret_cast<ASurfaceControl*>(surfaceStats.surfaceControl.get());
160 aSurfaceControlStats.acquireTime = surfaceStats.acquireTime;
161 aSurfaceControlStats.previousReleaseFence = surfaceStats.previousReleaseFence;
162 aSurfaceControlStats.frameNumber = surfaceStats.eventStats.frameNumber;
163
164 (*func)(callback_context, aSurfaceControl, &aSurfaceControlStats);
165 };
166 TransactionCompletedListener::getInstance()->addSurfaceStatsListener(context,
167 reinterpret_cast<void*>(func), ASurfaceControl_to_SurfaceControl(control), callback);
168}
169
170
171void ASurfaceControl_unregisterSurfaceStatsListener(void* context,
172 ASurfaceControl_SurfaceStatsListener func) {
173 TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context,
174 reinterpret_cast<void*>(func));
175}
176
177int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) {
178 return stats->acquireTime;
179}
180
181uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) {
182 return stats->frameNumber;
183}
184
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800185ASurfaceTransaction* ASurfaceTransaction_create() {
186 Transaction* transaction = new Transaction;
187 return reinterpret_cast<ASurfaceTransaction*>(transaction);
188}
189
190void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
191 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
192 delete transaction;
193}
194
195void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
196 CHECK_NOT_NULL(aSurfaceTransaction);
197
198 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
199
200 transaction->apply();
201}
202
Marissa Wall1be5a102019-01-18 16:14:04 -0800203struct ASurfaceTransactionStats {
204 std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
205 int64_t latchTime;
206 sp<Fence> presentFence;
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700207 bool transactionCompleted;
Marissa Wall1be5a102019-01-18 16:14:04 -0800208};
209
210int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
211 CHECK_NOT_NULL(aSurfaceTransactionStats);
212 return aSurfaceTransactionStats->latchTime;
213}
214
215int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
216 CHECK_NOT_NULL(aSurfaceTransactionStats);
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700217 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
218 "ASurfaceTransactionStats queried from an incomplete transaction callback");
219
Marissa Wall1be5a102019-01-18 16:14:04 -0800220 auto& presentFence = aSurfaceTransactionStats->presentFence;
221 return (presentFence) ? presentFence->dup() : -1;
222}
223
224void ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats* aSurfaceTransactionStats,
225 ASurfaceControl*** outASurfaceControls,
226 size_t* outASurfaceControlsSize) {
227 CHECK_NOT_NULL(aSurfaceTransactionStats);
228 CHECK_NOT_NULL(outASurfaceControls);
229 CHECK_NOT_NULL(outASurfaceControlsSize);
230
231 size_t size = aSurfaceTransactionStats->aSurfaceControlStats.size();
232
233 SurfaceControl** surfaceControls = new SurfaceControl*[size];
234 ASurfaceControl** aSurfaceControls = reinterpret_cast<ASurfaceControl**>(surfaceControls);
235
236 size_t i = 0;
237 for (auto& [aSurfaceControl, aSurfaceControlStats] : aSurfaceTransactionStats->aSurfaceControlStats) {
238 aSurfaceControls[i] = aSurfaceControl;
239 i++;
240 }
241
242 *outASurfaceControls = aSurfaceControls;
243 *outASurfaceControlsSize = size;
244}
245
246int64_t ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats* aSurfaceTransactionStats,
247 ASurfaceControl* aSurfaceControl) {
248 CHECK_NOT_NULL(aSurfaceTransactionStats);
249 CHECK_NOT_NULL(aSurfaceControl);
250
251 const auto& aSurfaceControlStats =
252 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
253 LOG_ALWAYS_FATAL_IF(
254 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
255 "ASurfaceControl not found");
256
257 return aSurfaceControlStats->second.acquireTime;
258}
259
260int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
261 ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
262 CHECK_NOT_NULL(aSurfaceTransactionStats);
263 CHECK_NOT_NULL(aSurfaceControl);
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700264 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
265 "ASurfaceTransactionStats queried from an incomplete transaction callback");
Marissa Wall1be5a102019-01-18 16:14:04 -0800266
267 const auto& aSurfaceControlStats =
268 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
269 LOG_ALWAYS_FATAL_IF(
270 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
271 "ASurfaceControl not found");
272
273 auto& previousReleaseFence = aSurfaceControlStats->second.previousReleaseFence;
274 return (previousReleaseFence) ? previousReleaseFence->dup() : -1;
275}
276
277void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
278 CHECK_NOT_NULL(aSurfaceControls);
279
280 SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
281 delete[] surfaceControls;
282}
283
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800284void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
285 ASurfaceTransaction_OnComplete func) {
286 CHECK_NOT_NULL(aSurfaceTransaction);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800287 CHECK_NOT_NULL(func);
288
289 TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
Marissa Wall1be5a102019-01-18 16:14:04 -0800290 nsecs_t latchTime,
291 const sp<Fence>& presentFence,
292 const std::vector<SurfaceControlStats>& surfaceControlStats) {
293 ASurfaceTransactionStats aSurfaceTransactionStats;
294
295 aSurfaceTransactionStats.latchTime = latchTime;
296 aSurfaceTransactionStats.presentFence = presentFence;
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700297 aSurfaceTransactionStats.transactionCompleted = true;
Marissa Wall1be5a102019-01-18 16:14:04 -0800298
299 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
300
Valerie Haud6a222e2020-01-29 14:27:09 -0800301 for (const auto& [surfaceControl, latchTime, acquireTime, presentFence, previousReleaseFence, transformHint, frameEvents] : surfaceControlStats) {
Marissa Wall1be5a102019-01-18 16:14:04 -0800302 ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
303 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
304 aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
305 }
306
307 (*func)(callback_context, &aSurfaceTransactionStats);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800308 };
309
310 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
311
312 transaction->addTransactionCompletedCallback(callback, context);
313}
314
Marissa Wall1be5a102019-01-18 16:14:04 -0800315void ASurfaceTransaction_reparent(ASurfaceTransaction* aSurfaceTransaction,
316 ASurfaceControl* aSurfaceControl,
317 ASurfaceControl* newParentASurfaceControl) {
318 CHECK_NOT_NULL(aSurfaceTransaction);
319 CHECK_NOT_NULL(aSurfaceControl);
320
321 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
322 sp<SurfaceControl> newParentSurfaceControl = ASurfaceControl_to_SurfaceControl(
323 newParentASurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800324 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
325
Pablo Gamito117040c2020-09-14 08:24:41 +0000326 transaction->reparent(surfaceControl, newParentSurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800327}
328
329void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
330 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800331 int8_t visibility) {
332 CHECK_NOT_NULL(aSurfaceTransaction);
333 CHECK_NOT_NULL(aSurfaceControl);
334
335 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
336 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
337
338 switch (visibility) {
339 case ASURFACE_TRANSACTION_VISIBILITY_SHOW:
340 transaction->show(surfaceControl);
341 break;
342 case ASURFACE_TRANSACTION_VISIBILITY_HIDE:
343 transaction->hide(surfaceControl);
344 break;
345 default:
346 LOG_ALWAYS_FATAL("invalid visibility %d", visibility);
347 }
348}
349
Marissa Wall1be5a102019-01-18 16:14:04 -0800350void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
351 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800352 int32_t z_order) {
353 CHECK_NOT_NULL(aSurfaceTransaction);
354 CHECK_NOT_NULL(aSurfaceControl);
355
356 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
357 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
358
359 transaction->setLayer(surfaceControl, z_order);
360}
361
Marissa Wall1be5a102019-01-18 16:14:04 -0800362void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
363 ASurfaceControl* aSurfaceControl,
364 AHardwareBuffer* buffer, int acquire_fence_fd) {
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800365 CHECK_NOT_NULL(aSurfaceTransaction);
366 CHECK_NOT_NULL(aSurfaceControl);
367
368 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
369 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
370
371 sp<GraphicBuffer> graphic_buffer(reinterpret_cast<GraphicBuffer*>(buffer));
372
373 transaction->setBuffer(surfaceControl, graphic_buffer);
Marissa Wall1be5a102019-01-18 16:14:04 -0800374 if (acquire_fence_fd != -1) {
375 sp<Fence> fence = new Fence(acquire_fence_fd);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800376 transaction->setAcquireFence(surfaceControl, fence);
377 }
378}
379
380void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction,
381 ASurfaceControl* aSurfaceControl, const ARect& source,
382 const ARect& destination, int32_t transform) {
383 CHECK_NOT_NULL(aSurfaceTransaction);
384 CHECK_NOT_NULL(aSurfaceControl);
chaviw87a07ea2021-04-29 09:04:41 -0500385 CHECK_VALID_RECT(source);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800386 CHECK_VALID_RECT(destination);
387
388 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
389 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
390
chaviw87a07ea2021-04-29 09:04:41 -0500391 Rect sourceRect = static_cast<const Rect&>(source);
392 Rect destRect = static_cast<const Rect&>(destination);
393 // Adjust the source so its top and left are not negative
394 sourceRect.left = std::max(sourceRect.left, 0);
395 sourceRect.top = std::max(sourceRect.top, 0);
396
397 if (!sourceRect.isValid()) {
398 sourceRect.makeInvalid();
399 }
chaviw9b2ac242021-04-27 15:52:09 -0500400 transaction->setBufferCrop(surfaceControl, sourceRect);
Vishnu Nair0d7aff72021-05-10 15:01:20 -0700401 transaction->setDestinationFrame(surfaceControl, destRect);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800402 transaction->setTransform(surfaceControl, transform);
Vishnu Nair1ad69542019-05-23 16:27:45 +0800403 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
404 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
405 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800406}
407
chaviwccf3e8b2021-03-25 15:28:44 -0500408void ASurfaceTransaction_setCrop(ASurfaceTransaction* aSurfaceTransaction,
409 ASurfaceControl* aSurfaceControl, const ARect& crop) {
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500410 CHECK_NOT_NULL(aSurfaceTransaction);
411 CHECK_NOT_NULL(aSurfaceControl);
chaviwccf3e8b2021-03-25 15:28:44 -0500412 CHECK_VALID_RECT(crop);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500413
414 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
415 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
416
chaviwccf3e8b2021-03-25 15:28:44 -0500417 transaction->setCrop(surfaceControl, static_cast<const Rect&>(crop));
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500418}
419
chaviwccf3e8b2021-03-25 15:28:44 -0500420void ASurfaceTransaction_setPosition(ASurfaceTransaction* aSurfaceTransaction,
421 ASurfaceControl* aSurfaceControl, int32_t x, int32_t y) {
422 CHECK_NOT_NULL(aSurfaceTransaction);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500423 CHECK_NOT_NULL(aSurfaceControl);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500424
425 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
426 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
427
chaviwccf3e8b2021-03-25 15:28:44 -0500428 transaction->setPosition(surfaceControl, x, y);
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500429}
430
chaviwccf3e8b2021-03-25 15:28:44 -0500431void ASurfaceTransaction_setBufferTransform(ASurfaceTransaction* aSurfaceTransaction,
432 ASurfaceControl* aSurfaceControl, int32_t transform) {
Vasiliy Telezhnikov5ead3aa2021-03-13 19:55:00 -0500433 CHECK_NOT_NULL(aSurfaceTransaction);
434 CHECK_NOT_NULL(aSurfaceControl);
435
436 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
437 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
438
439 transaction->setTransform(surfaceControl, transform);
440 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
441 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
442 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
443}
444
chaviwccf3e8b2021-03-25 15:28:44 -0500445void ASurfaceTransaction_setScale(ASurfaceTransaction* aSurfaceTransaction,
446 ASurfaceControl* aSurfaceControl, float xScale, float yScale) {
447 CHECK_NOT_NULL(aSurfaceTransaction);
448 CHECK_NOT_NULL(aSurfaceControl);
449 LOG_ALWAYS_FATAL_IF(xScale < 0, "negative value passed in for xScale");
450 LOG_ALWAYS_FATAL_IF(yScale < 0, "negative value passed in for yScale");
451
452 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
453 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
454
455 transaction->setMatrix(surfaceControl, xScale, 0, 0, yScale);
456}
457
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800458void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction,
459 ASurfaceControl* aSurfaceControl,
460 int8_t transparency) {
461 CHECK_NOT_NULL(aSurfaceTransaction);
462 CHECK_NOT_NULL(aSurfaceControl);
463
464 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
465 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
466
467 uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ?
468 layer_state_t::eLayerOpaque : 0;
469 transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque);
470}
471
Marissa Wall1be5a102019-01-18 16:14:04 -0800472void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
473 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800474 const ARect rects[], uint32_t count) {
475 CHECK_NOT_NULL(aSurfaceTransaction);
476 CHECK_NOT_NULL(aSurfaceControl);
477
478 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
479 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
480
481 Region region;
482 for (uint32_t i = 0; i < count; ++i) {
Marissa Wallbb9b14f2019-04-23 14:10:15 -0700483 region.orSelf(static_cast<const Rect&>(rects[i]));
484 }
485
486 // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an
487 // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing
488 // distinction for a public api. Instead, default both cases to be a fully damaged buffer.
489 if (count == 1 && region.getBounds().isEmpty()) {
490 transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION);
491 return;
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800492 }
493
494 transaction->setSurfaceDamageRegion(surfaceControl, region);
495}
Marissa Wall1be5a102019-01-18 16:14:04 -0800496
497void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction,
498 int64_t desiredPresentTime) {
499 CHECK_NOT_NULL(aSurfaceTransaction);
500
501 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
502
503 transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime));
504}
505
506void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction,
507 ASurfaceControl* aSurfaceControl,
508 float alpha) {
509 CHECK_NOT_NULL(aSurfaceTransaction);
510 CHECK_NOT_NULL(aSurfaceControl);
511
512 LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha");
513
514 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
515 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
516
517 transaction->setAlpha(surfaceControl, alpha);
518}
519
Marissa Wall7f24f792019-02-07 14:06:04 -0800520void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction,
521 ASurfaceControl* aSurfaceControl,
522 ADataSpace aDataSpace) {
523 CHECK_NOT_NULL(aSurfaceTransaction);
524 CHECK_NOT_NULL(aSurfaceControl);
525
526 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marissa Wall7f24f792019-02-07 14:06:04 -0800527 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Marissa Wall7f24f792019-02-07 14:06:04 -0800528 transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
529}
530
Marissa Wall1be5a102019-01-18 16:14:04 -0800531void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction,
532 ASurfaceControl* aSurfaceControl,
533 struct AHdrMetadata_smpte2086* metadata) {
534 CHECK_NOT_NULL(aSurfaceTransaction);
535 CHECK_NOT_NULL(aSurfaceControl);
536
537 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
538 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
539
540 HdrMetadata hdrMetadata;
541
542 if (metadata) {
543 hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x;
544 hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y;
545 hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x;
546 hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y;
547 hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x;
548 hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y;
549 hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x;
550 hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y;
551 hdrMetadata.smpte2086.minLuminance = metadata->minLuminance;
552 hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance;
553
554 hdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
555 } else {
556 hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
557 }
558
559 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
560}
561
562void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction,
563 ASurfaceControl* aSurfaceControl,
564 struct AHdrMetadata_cta861_3* metadata) {
565 CHECK_NOT_NULL(aSurfaceTransaction);
566 CHECK_NOT_NULL(aSurfaceControl);
567
568 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
569 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
570
571 HdrMetadata hdrMetadata;
572
573 if (metadata) {
574 hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel;
575 hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel;
576
577 hdrMetadata.validTypes |= HdrMetadata::CTA861_3;
578 } else {
579 hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
580 }
581
582 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
583}
Valerie Hau5bbfd512019-01-22 17:39:43 -0800584
585void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
586 ASurfaceControl* aSurfaceControl,
587 float r, float g, float b, float alpha,
588 ADataSpace dataspace) {
589 CHECK_NOT_NULL(aSurfaceTransaction);
590 CHECK_NOT_NULL(aSurfaceControl);
591
592 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Valerie Hau5bbfd512019-01-22 17:39:43 -0800593 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
594
595 half3 color;
596 color.r = r;
597 color.g = g;
598 color.b = b;
599
Marin Shalamanov511f9142021-03-16 18:03:30 +0100600 transaction->setBackgroundColor(surfaceControl, color, alpha,
601 static_cast<ui::Dataspace>(dataspace));
Valerie Hau5bbfd512019-01-22 17:39:43 -0800602}
Steven Thomas6cf051e2020-01-14 11:37:21 -0800603
604void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800605 ASurfaceControl* aSurfaceControl, float frameRate,
606 int8_t compatibility) {
Marin Shalamanov511f9142021-03-16 18:03:30 +0100607 ASurfaceTransaction_setFrameRateWithChangeStrategy(
608 aSurfaceTransaction, aSurfaceControl, frameRate, compatibility,
609 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
Marin Shalamanov41ffa8d2020-10-13 12:35:20 +0200610}
611
Marin Shalamanov511f9142021-03-16 18:03:30 +0100612void ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction* aSurfaceTransaction,
613 ASurfaceControl* aSurfaceControl,
614 float frameRate, int8_t compatibility,
615 int8_t changeFrameRateStrategy) {
Steven Thomas6cf051e2020-01-14 11:37:21 -0800616 CHECK_NOT_NULL(aSurfaceTransaction);
617 CHECK_NOT_NULL(aSurfaceControl);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800618 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800619 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marin Shalamanov511f9142021-03-16 18:03:30 +0100620 transaction->setFrameRate(surfaceControl, frameRate, compatibility, changeFrameRateStrategy);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800621}
Robert Carrf57c0162021-03-24 15:48:25 -0700622
623void ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction* aSurfaceTransaction,
624 ASurfaceControl* aSurfaceControl,
625 bool enableBackpressure) {
626 CHECK_NOT_NULL(aSurfaceControl);
627 CHECK_NOT_NULL(aSurfaceTransaction);
628
629 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
630 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
631
632 const uint32_t flags = enableBackpressure ?
633 layer_state_t::eEnableBackpressure : 0;
634 transaction->setFlags(surfaceControl, flags, layer_state_t::eEnableBackpressure);
635}
Vishnu Nairbeb3b482021-04-21 08:31:27 -0700636
637void ASurfaceTransaction_setOnCommit(ASurfaceTransaction* aSurfaceTransaction, void* context,
638 ASurfaceTransaction_OnCommit func) {
639 CHECK_NOT_NULL(aSurfaceTransaction);
640 CHECK_NOT_NULL(func);
641
642 TransactionCompletedCallbackTakesContext callback =
643 [func](void* callback_context, nsecs_t latchTime, const sp<Fence>& /* presentFence */,
644 const std::vector<SurfaceControlStats>& surfaceControlStats) {
645 ASurfaceTransactionStats aSurfaceTransactionStats;
646 aSurfaceTransactionStats.latchTime = latchTime;
647 aSurfaceTransactionStats.transactionCompleted = false;
648
649 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
650 for (const auto&
651 [surfaceControl, latchTime, acquireTime, presentFence,
652 previousReleaseFence, transformHint,
653 frameEvents] : surfaceControlStats) {
654 ASurfaceControl* aSurfaceControl =
655 reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
656 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
657 }
658
659 (*func)(callback_context, &aSurfaceTransactionStats);
660 };
661
662 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
663
664 transaction->addTransactionCommittedCallback(callback, context);
Pablo Gamito88660d72021-08-09 14:37:56 +0000665}