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
17 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
18 #include <android/native_window.h>
19 #include <android/surface_control.h>
20 #include <android/surface_control_jni.h>
21 #include <android_runtime/android_view_SurfaceControl.h>
22 #include <configstore/Utils.h>
23 #include <gui/HdrMetadata.h>
24 #include <gui/ISurfaceComposer.h>
25 #include <gui/Surface.h>
26 #include <gui/SurfaceComposerClient.h>
27 #include <gui/SurfaceControl.h>
28 #include <private/android/choreographer.h>
29 #include <surface_control_private.h>
30 #include <ui/DynamicDisplayInfo.h>
31 #include <utils/Timers.h>
32
33 #include <utility>
34
35 using namespace android::hardware::configstore;
36 using namespace android::hardware::configstore::V1_0;
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
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));
56
ASurfaceTransaction_to_Transaction(ASurfaceTransaction * aSurfaceTransaction)57 Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) {
58 return reinterpret_cast<Transaction*>(aSurfaceTransaction);
59 }
60
ASurfaceControl_to_SurfaceControl(ASurfaceControl * aSurfaceControl)61 SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) {
62 return reinterpret_cast<SurfaceControl*>(aSurfaceControl);
63 }
64
SurfaceControl_acquire(SurfaceControl * surfaceControl)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
SurfaceControl_release(SurfaceControl * surfaceControl)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
ASurfaceControl_createFromWindow(ANativeWindow * window,const char * debug_name)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
84 Surface* surface = static_cast<Surface*>(window);
85 sp<IBinder> parentHandle = surface->getSurfaceControlHandle();
86
87 int32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
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 {
95 // deprecated, this should no longer be used
96 surfaceControl = nullptr;
97 }
98
99 if (!surfaceControl) {
100 return nullptr;
101 }
102
103 SurfaceControl_acquire(surfaceControl.get());
104 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
105 }
106
ASurfaceControl_create(ASurfaceControl * parent,const char * debug_name)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,
120 surfaceControlParent->getHandle());
121 if (!surfaceControl) {
122 return nullptr;
123 }
124
125 SurfaceControl_acquire(surfaceControl.get());
126 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
127 }
128
ASurfaceControl_acquire(ASurfaceControl * aSurfaceControl)129 void ASurfaceControl_acquire(ASurfaceControl* aSurfaceControl) {
130 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
131
132 SurfaceControl_acquire(surfaceControl);
133 }
134
ASurfaceControl_release(ASurfaceControl * aSurfaceControl)135 void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
136 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
137
138 SurfaceControl_release(surfaceControl);
139 }
140
ASurfaceControl_fromJava(JNIEnv * env,jobject surfaceControlObj)141 ASurfaceControl* ASurfaceControl_fromJava(JNIEnv* env, jobject surfaceControlObj) {
142 LOG_ALWAYS_FATAL_IF(!env, "nullptr passed to ASurfaceControl_fromJava as env argument");
143 LOG_ALWAYS_FATAL_IF(!surfaceControlObj,
144 "nullptr passed to ASurfaceControl_fromJava as surfaceControlObj argument");
145 SurfaceControl* surfaceControl =
146 android_view_SurfaceControl_getNativeSurfaceControl(env, surfaceControlObj);
147 LOG_ALWAYS_FATAL_IF(!surfaceControl,
148 "surfaceControlObj passed to ASurfaceControl_fromJava is not valid");
149 SurfaceControl_acquire(surfaceControl);
150 return reinterpret_cast<ASurfaceControl*>(surfaceControl);
151 }
152
153 struct ASurfaceControlStats {
154 std::variant<int64_t, sp<Fence>> acquireTimeOrFence;
155 sp<Fence> previousReleaseFence;
156 uint64_t frameNumber;
157 };
158
ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl * control,int32_t id,void * context,ASurfaceControl_SurfaceStatsListener func)159 void ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl* control, int32_t id,
160 void* context,
161 ASurfaceControl_SurfaceStatsListener func) {
162 SurfaceStatsCallback callback = [func, id](void* callback_context, nsecs_t, const sp<Fence>&,
163 const SurfaceStats& surfaceStats) {
164 ASurfaceControlStats aSurfaceControlStats;
165
166 aSurfaceControlStats.acquireTimeOrFence = surfaceStats.acquireTimeOrFence;
167 aSurfaceControlStats.previousReleaseFence = surfaceStats.previousReleaseFence;
168 aSurfaceControlStats.frameNumber = surfaceStats.eventStats.frameNumber;
169
170 (*func)(callback_context, id, &aSurfaceControlStats);
171 };
172
173 TransactionCompletedListener::getInstance()->addSurfaceStatsListener(context,
174 reinterpret_cast<void*>(func), ASurfaceControl_to_SurfaceControl(control), callback);
175 }
176
ASurfaceControl_unregisterSurfaceStatsListener(void * context,ASurfaceControl_SurfaceStatsListener func)177 void ASurfaceControl_unregisterSurfaceStatsListener(void* context,
178 ASurfaceControl_SurfaceStatsListener func) {
179 TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context,
180 reinterpret_cast<void*>(func));
181 }
182
ASurfaceControl_getChoreographer(ASurfaceControl * aSurfaceControl)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
ASurfaceControlStats_getAcquireTime(ASurfaceControlStats * stats)195 int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) {
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);
205 }
206
ASurfaceControlStats_getFrameNumber(ASurfaceControlStats * stats)207 uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) {
208 return stats->frameNumber;
209 }
210
ASurfaceTransaction_create()211 ASurfaceTransaction* ASurfaceTransaction_create() {
212 Transaction* transaction = new Transaction;
213 return reinterpret_cast<ASurfaceTransaction*>(transaction);
214 }
215
ASurfaceTransaction_delete(ASurfaceTransaction * aSurfaceTransaction)216 void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
217 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
218 delete transaction;
219 }
220
ASurfaceTransaction_fromJava(JNIEnv * env,jobject transactionObj)221 ASurfaceTransaction* ASurfaceTransaction_fromJava(JNIEnv* env, jobject transactionObj) {
222 LOG_ALWAYS_FATAL_IF(!env, "nullptr passed to ASurfaceTransaction_fromJava as env argument");
223 LOG_ALWAYS_FATAL_IF(!transactionObj,
224 "nullptr passed to ASurfaceTransaction_fromJava as transactionObj "
225 "argument");
226 Transaction* transaction =
227 android_view_SurfaceTransaction_getNativeSurfaceTransaction(env, transactionObj);
228 LOG_ALWAYS_FATAL_IF(!transaction,
229 "surfaceControlObj passed to ASurfaceTransaction_fromJava is not valid");
230 return reinterpret_cast<ASurfaceTransaction*>(transaction);
231 }
232
ASurfaceTransaction_apply(ASurfaceTransaction * aSurfaceTransaction)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
241 struct ASurfaceTransactionStats {
242 std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
243 int64_t latchTime;
244 sp<Fence> presentFence;
245 bool transactionCompleted;
246 };
247
ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats * aSurfaceTransactionStats)248 int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
249 CHECK_NOT_NULL(aSurfaceTransactionStats);
250 return aSurfaceTransactionStats->latchTime;
251 }
252
ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats)253 int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
254 CHECK_NOT_NULL(aSurfaceTransactionStats);
255 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
256 "ASurfaceTransactionStats queried from an incomplete transaction callback");
257
258 auto& presentFence = aSurfaceTransactionStats->presentFence;
259 return (presentFence) ? presentFence->dup() : -1;
260 }
261
ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl *** outASurfaceControls,size_t * outASurfaceControlsSize)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
ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)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
295 return ASurfaceControlStats_getAcquireTime(&aSurfaceControlStats->second);
296 }
297
ASurfaceTransactionStats_getPreviousReleaseFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)298 int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
299 ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
300 CHECK_NOT_NULL(aSurfaceTransactionStats);
301 CHECK_NOT_NULL(aSurfaceControl);
302 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
303 "ASurfaceTransactionStats queried from an incomplete transaction callback");
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
ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl ** aSurfaceControls)315 void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
316 CHECK_NOT_NULL(aSurfaceControls);
317
318 SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
319 delete[] surfaceControls;
320 }
321
ASurfaceTransaction_setOnComplete(ASurfaceTransaction * aSurfaceTransaction,void * context,ASurfaceTransaction_OnComplete func)322 void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
323 ASurfaceTransaction_OnComplete func) {
324 CHECK_NOT_NULL(aSurfaceTransaction);
325 CHECK_NOT_NULL(func);
326
327 TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
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;
335 aSurfaceTransactionStats.transactionCompleted = true;
336
337 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
338
339 for (const auto& [surfaceControl, latchTime, acquireTimeOrFence, presentFence,
340 previousReleaseFence, transformHint, frameEvents, ignore] : surfaceControlStats) {
341 ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
342 aSurfaceControlStats[aSurfaceControl].acquireTimeOrFence = acquireTimeOrFence;
343 aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
344 }
345
346 (*func)(callback_context, &aSurfaceTransactionStats);
347 };
348
349 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
350
351 transaction->addTransactionCompletedCallback(callback, context);
352 }
353
ASurfaceTransaction_reparent(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ASurfaceControl * newParentASurfaceControl)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);
363 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
364
365 transaction->reparent(surfaceControl, newParentSurfaceControl);
366 }
367
ASurfaceTransaction_setVisibility(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ASurfaceTransactionVisibility visibility)368 void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
369 ASurfaceControl* aSurfaceControl,
370 ASurfaceTransactionVisibility visibility) {
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
ASurfaceTransaction_setZOrder(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t z_order)389 void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
390 ASurfaceControl* aSurfaceControl,
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
ASurfaceTransaction_setBuffer(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,AHardwareBuffer * buffer,int acquire_fence_fd)401 void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
402 ASurfaceControl* aSurfaceControl,
403 AHardwareBuffer* buffer, int acquire_fence_fd) {
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
410 sp<GraphicBuffer> graphic_buffer(GraphicBuffer::fromAHardwareBuffer(buffer));
411
412 std::optional<sp<Fence>> fence = std::nullopt;
413 if (acquire_fence_fd != -1) {
414 fence = new Fence(acquire_fence_fd);
415 }
416 transaction->setBuffer(surfaceControl, graphic_buffer, fence);
417 }
418
ASurfaceTransaction_setGeometry(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect & source,const ARect & destination,int32_t transform)419 void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction,
420 ASurfaceControl* aSurfaceControl, const ARect& source,
421 const ARect& destination, int32_t transform) {
422 CHECK_NOT_NULL(aSurfaceTransaction);
423 CHECK_NOT_NULL(aSurfaceControl);
424 CHECK_VALID_RECT(source);
425 CHECK_VALID_RECT(destination);
426
427 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
428 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
429
430 Rect sourceRect = static_cast<const Rect&>(source);
431 Rect destRect = static_cast<const Rect&>(destination);
432 // Adjust the source so its top and left are not negative
433 sourceRect.left = std::max(sourceRect.left, 0);
434 sourceRect.top = std::max(sourceRect.top, 0);
435
436 if (!sourceRect.isValid()) {
437 sourceRect.makeInvalid();
438 }
439 transaction->setBufferCrop(surfaceControl, sourceRect);
440 transaction->setDestinationFrame(surfaceControl, destRect);
441 transaction->setTransform(surfaceControl, transform);
442 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
443 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
444 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
445 }
446
ASurfaceTransaction_setCrop(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect & crop)447 void ASurfaceTransaction_setCrop(ASurfaceTransaction* aSurfaceTransaction,
448 ASurfaceControl* aSurfaceControl, const ARect& crop) {
449 CHECK_NOT_NULL(aSurfaceTransaction);
450 CHECK_NOT_NULL(aSurfaceControl);
451 CHECK_VALID_RECT(crop);
452
453 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
454 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
455
456 transaction->setCrop(surfaceControl, static_cast<const Rect&>(crop));
457 }
458
ASurfaceTransaction_setPosition(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t x,int32_t y)459 void ASurfaceTransaction_setPosition(ASurfaceTransaction* aSurfaceTransaction,
460 ASurfaceControl* aSurfaceControl, int32_t x, int32_t y) {
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 transaction->setPosition(surfaceControl, x, y);
468 }
469
ASurfaceTransaction_setBufferTransform(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t transform)470 void ASurfaceTransaction_setBufferTransform(ASurfaceTransaction* aSurfaceTransaction,
471 ASurfaceControl* aSurfaceControl, int32_t transform) {
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 transaction->setTransform(surfaceControl, transform);
479 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
480 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
481 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
482 }
483
ASurfaceTransaction_setScale(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float xScale,float yScale)484 void ASurfaceTransaction_setScale(ASurfaceTransaction* aSurfaceTransaction,
485 ASurfaceControl* aSurfaceControl, float xScale, float yScale) {
486 CHECK_NOT_NULL(aSurfaceTransaction);
487 CHECK_NOT_NULL(aSurfaceControl);
488 LOG_ALWAYS_FATAL_IF(xScale < 0, "negative value passed in for xScale");
489 LOG_ALWAYS_FATAL_IF(yScale < 0, "negative value passed in for yScale");
490
491 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
492 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
493
494 transaction->setMatrix(surfaceControl, xScale, 0, 0, yScale);
495 }
496
ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ASurfaceTransactionTransparency transparency)497 void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction,
498 ASurfaceControl* aSurfaceControl,
499 ASurfaceTransactionTransparency transparency) {
500 CHECK_NOT_NULL(aSurfaceTransaction);
501 CHECK_NOT_NULL(aSurfaceControl);
502
503 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
504 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
505
506 uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ?
507 layer_state_t::eLayerOpaque : 0;
508 transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque);
509 }
510
ASurfaceTransaction_setDamageRegion(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect rects[],uint32_t count)511 void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
512 ASurfaceControl* aSurfaceControl,
513 const ARect rects[], uint32_t count) {
514 CHECK_NOT_NULL(aSurfaceTransaction);
515 CHECK_NOT_NULL(aSurfaceControl);
516
517 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
518 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
519
520 Region region;
521 for (uint32_t i = 0; i < count; ++i) {
522 region.orSelf(static_cast<const Rect&>(rects[i]));
523 }
524
525 // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an
526 // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing
527 // distinction for a public api. Instead, default both cases to be a fully damaged buffer.
528 if (count == 1 && region.getBounds().isEmpty()) {
529 transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION);
530 return;
531 }
532
533 transaction->setSurfaceDamageRegion(surfaceControl, region);
534 }
535
ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction * aSurfaceTransaction,int64_t desiredPresentTime)536 void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction,
537 int64_t desiredPresentTime) {
538 CHECK_NOT_NULL(aSurfaceTransaction);
539
540 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
541
542 transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime));
543 }
544
ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float alpha)545 void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction,
546 ASurfaceControl* aSurfaceControl,
547 float alpha) {
548 CHECK_NOT_NULL(aSurfaceTransaction);
549 CHECK_NOT_NULL(aSurfaceControl);
550
551 LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha");
552
553 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
554 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
555
556 transaction->setAlpha(surfaceControl, alpha);
557 }
558
ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ADataSpace aDataSpace)559 void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction,
560 ASurfaceControl* aSurfaceControl,
561 ADataSpace aDataSpace) {
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 transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
568 }
569
ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_smpte2086 * metadata)570 void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction,
571 ASurfaceControl* aSurfaceControl,
572 struct AHdrMetadata_smpte2086* metadata) {
573 CHECK_NOT_NULL(aSurfaceTransaction);
574 CHECK_NOT_NULL(aSurfaceControl);
575
576 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
577 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
578
579 HdrMetadata hdrMetadata;
580
581 if (metadata) {
582 hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x;
583 hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y;
584 hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x;
585 hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y;
586 hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x;
587 hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y;
588 hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x;
589 hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y;
590 hdrMetadata.smpte2086.minLuminance = metadata->minLuminance;
591 hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance;
592
593 hdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
594 } else {
595 hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
596 }
597
598 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
599 }
600
ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_cta861_3 * metadata)601 void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction,
602 ASurfaceControl* aSurfaceControl,
603 struct AHdrMetadata_cta861_3* metadata) {
604 CHECK_NOT_NULL(aSurfaceTransaction);
605 CHECK_NOT_NULL(aSurfaceControl);
606
607 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
608 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
609
610 HdrMetadata hdrMetadata;
611
612 if (metadata) {
613 hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel;
614 hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel;
615
616 hdrMetadata.validTypes |= HdrMetadata::CTA861_3;
617 } else {
618 hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
619 }
620
621 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
622 }
623
ASurfaceTransaction_setExtendedRangeBrightness(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float currentBufferRatio,float desiredRatio)624 void ASurfaceTransaction_setExtendedRangeBrightness(ASurfaceTransaction* aSurfaceTransaction,
625 ASurfaceControl* aSurfaceControl,
626 float currentBufferRatio, float desiredRatio) {
627 CHECK_NOT_NULL(aSurfaceTransaction);
628 CHECK_NOT_NULL(aSurfaceControl);
629
630 if (!isfinite(currentBufferRatio) || currentBufferRatio < 1.0f) {
631 LOG_ALWAYS_FATAL("setExtendedRangeBrightness, currentBufferRatio %f isn't finite or >= "
632 "1.0f",
633 currentBufferRatio);
634 return;
635 }
636
637 if (!isfinite(desiredRatio) || desiredRatio < 1.0f) {
638 LOG_ALWAYS_FATAL("setExtendedRangeBrightness, desiredRatio %f isn't finite or >= 1.0f",
639 desiredRatio);
640 return;
641 }
642
643 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
644 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
645
646 transaction->setExtendedRangeBrightness(surfaceControl, currentBufferRatio, desiredRatio);
647 }
648
ASurfaceTransaction_setDesiredHdrHeadroom(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float desiredRatio)649 void ASurfaceTransaction_setDesiredHdrHeadroom(ASurfaceTransaction* aSurfaceTransaction,
650 ASurfaceControl* aSurfaceControl,
651 float desiredRatio) {
652 CHECK_NOT_NULL(aSurfaceTransaction);
653 CHECK_NOT_NULL(aSurfaceControl);
654
655 if (!isfinite(desiredRatio) || (desiredRatio < 1.0f && desiredRatio > 0.0f)) {
656 LOG_ALWAYS_FATAL("setDesiredHdrHeadroom, desiredRatio isn't finite && >= 1.0f or 0, got %f",
657 desiredRatio);
658 return;
659 }
660
661 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
662 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
663
664 transaction->setDesiredHdrHeadroom(surfaceControl, desiredRatio);
665 }
666
ASurfaceTransaction_setColor(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float r,float g,float b,float alpha,ADataSpace dataspace)667 void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
668 ASurfaceControl* aSurfaceControl,
669 float r, float g, float b, float alpha,
670 ADataSpace dataspace) {
671 CHECK_NOT_NULL(aSurfaceTransaction);
672 CHECK_NOT_NULL(aSurfaceControl);
673
674 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
675 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
676
677 half3 color;
678 color.r = r;
679 color.g = g;
680 color.b = b;
681
682 transaction->setBackgroundColor(surfaceControl, color, alpha,
683 static_cast<ui::Dataspace>(dataspace));
684 }
685
ASurfaceTransaction_setFrameRate(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float frameRate,int8_t compatibility)686 void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
687 ASurfaceControl* aSurfaceControl, float frameRate,
688 int8_t compatibility) {
689 ASurfaceTransaction_setFrameRateWithChangeStrategy(
690 aSurfaceTransaction, aSurfaceControl, frameRate, compatibility,
691 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
692 }
693
ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy)694 void ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction* aSurfaceTransaction,
695 ASurfaceControl* aSurfaceControl,
696 float frameRate, int8_t compatibility,
697 int8_t changeFrameRateStrategy) {
698 CHECK_NOT_NULL(aSurfaceTransaction);
699 CHECK_NOT_NULL(aSurfaceControl);
700 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
701 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
702 transaction->setFrameRate(surfaceControl, frameRate, compatibility, changeFrameRateStrategy);
703 }
704
ASurfaceTransaction_clearFrameRate(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl)705 void ASurfaceTransaction_clearFrameRate(ASurfaceTransaction* aSurfaceTransaction,
706 ASurfaceControl* aSurfaceControl) {
707 CHECK_NOT_NULL(aSurfaceTransaction);
708 CHECK_NOT_NULL(aSurfaceControl);
709 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
710 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
711 transaction->setFrameRate(surfaceControl, 0, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT,
712 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
713 }
714
ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,bool enableBackpressure)715 void ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction* aSurfaceTransaction,
716 ASurfaceControl* aSurfaceControl,
717 bool enableBackpressure) {
718 CHECK_NOT_NULL(aSurfaceControl);
719 CHECK_NOT_NULL(aSurfaceTransaction);
720
721 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
722 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
723
724 const uint32_t flags = enableBackpressure ?
725 layer_state_t::eEnableBackpressure : 0;
726 transaction->setFlags(surfaceControl, flags, layer_state_t::eEnableBackpressure);
727 }
728
ASurfaceTransaction_setOnCommit(ASurfaceTransaction * aSurfaceTransaction,void * context,ASurfaceTransaction_OnCommit func)729 void ASurfaceTransaction_setOnCommit(ASurfaceTransaction* aSurfaceTransaction, void* context,
730 ASurfaceTransaction_OnCommit func) {
731 CHECK_NOT_NULL(aSurfaceTransaction);
732 CHECK_NOT_NULL(func);
733
734 TransactionCompletedCallbackTakesContext callback =
735 [func](void* callback_context, nsecs_t latchTime, const sp<Fence>& /* presentFence */,
736 const std::vector<SurfaceControlStats>& surfaceControlStats) {
737 ASurfaceTransactionStats aSurfaceTransactionStats;
738 aSurfaceTransactionStats.latchTime = latchTime;
739 aSurfaceTransactionStats.transactionCompleted = false;
740
741 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
742 for (const auto& [surfaceControl, latchTime, acquireTimeOrFence, presentFence,
743 previousReleaseFence, transformHint, frameEvents, ignore] :
744 surfaceControlStats) {
745 ASurfaceControl* aSurfaceControl =
746 reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
747 aSurfaceControlStats[aSurfaceControl].acquireTimeOrFence = acquireTimeOrFence;
748 }
749
750 (*func)(callback_context, &aSurfaceTransactionStats);
751 };
752
753 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
754
755 transaction->addTransactionCommittedCallback(callback, context);
756 }
757
ASurfaceTransaction_setFrameTimeline(ASurfaceTransaction * aSurfaceTransaction,AVsyncId vsyncId)758 void ASurfaceTransaction_setFrameTimeline(ASurfaceTransaction* aSurfaceTransaction,
759 AVsyncId vsyncId) {
760 CHECK_NOT_NULL(aSurfaceTransaction);
761 const auto startTime = AChoreographer_getStartTimeNanosForVsyncId(vsyncId);
762 FrameTimelineInfo ftInfo;
763 ftInfo.vsyncId = vsyncId;
764 ftInfo.startTimeNanos = startTime;
765 ASurfaceTransaction_to_Transaction(aSurfaceTransaction)->setFrameTimelineInfo(ftInfo);
766 }
767