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 <surface_control_private.h>
21
22 #include <configstore/Utils.h>
23
24 #include <gui/HdrMetadata.h>
25 #include <gui/ISurfaceComposer.h>
26 #include <gui/Surface.h>
27 #include <gui/SurfaceComposerClient.h>
28 #include <gui/SurfaceControl.h>
29
30 #include <ui/DynamicDisplayInfo.h>
31
32 #include <utils/Timers.h>
33
34 using namespace android::hardware::configstore;
35 using namespace android::hardware::configstore::V1_0;
36 using namespace android;
37
38 using 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
47 static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
48 static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) ==
49 static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
50 static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
51 static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
52 static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) ==
53 static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
54 static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
55
ASurfaceTransaction_to_Transaction(ASurfaceTransaction * aSurfaceTransaction)56 Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) {
57 return reinterpret_cast<Transaction*>(aSurfaceTransaction);
58 }
59
ASurfaceControl_to_SurfaceControl(ASurfaceControl * aSurfaceControl)60 SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) {
61 return reinterpret_cast<SurfaceControl*>(aSurfaceControl);
62 }
63
SurfaceControl_acquire(SurfaceControl * surfaceControl)64 void 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
SurfaceControl_release(SurfaceControl * surfaceControl)69 void 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
ASurfaceControl_createFromWindow(ANativeWindow * window,const char * debug_name)74 ASurfaceControl* 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
83 Surface* surface = static_cast<Surface*>(window);
84 sp<IBinder> parentHandle = surface->getSurfaceControlHandle();
85
86 uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
87 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
101 if (!surfaceControl) {
102 return nullptr;
103 }
104
105 SurfaceControl_acquire(surfaceControl.get());
106 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
107 }
108
ASurfaceControl_create(ASurfaceControl * parent,const char * debug_name)109 ASurfaceControl* 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,
122 surfaceControlParent->getHandle());
123 if (!surfaceControl) {
124 return nullptr;
125 }
126
127 SurfaceControl_acquire(surfaceControl.get());
128 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
129 }
130
ASurfaceControl_acquire(ASurfaceControl * aSurfaceControl)131 void ASurfaceControl_acquire(ASurfaceControl* aSurfaceControl) {
132 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
133
134 SurfaceControl_acquire(surfaceControl);
135 }
136
ASurfaceControl_release(ASurfaceControl * aSurfaceControl)137 void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
138 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
139
140 SurfaceControl_release(surfaceControl);
141 }
142
143 struct ASurfaceControlStats {
144 int64_t acquireTime;
145 sp<Fence> previousReleaseFence;
146 uint64_t frameNumber;
147 };
148
ASurfaceControl_registerSurfaceStatsListener(ASurfaceControl * control,void * context,ASurfaceControl_SurfaceStatsListener func)149 void 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
ASurfaceControl_unregisterSurfaceStatsListener(void * context,ASurfaceControl_SurfaceStatsListener func)171 void ASurfaceControl_unregisterSurfaceStatsListener(void* context,
172 ASurfaceControl_SurfaceStatsListener func) {
173 TransactionCompletedListener::getInstance()->removeSurfaceStatsListener(context,
174 reinterpret_cast<void*>(func));
175 }
176
ASurfaceControlStats_getAcquireTime(ASurfaceControlStats * stats)177 int64_t ASurfaceControlStats_getAcquireTime(ASurfaceControlStats* stats) {
178 return stats->acquireTime;
179 }
180
ASurfaceControlStats_getFrameNumber(ASurfaceControlStats * stats)181 uint64_t ASurfaceControlStats_getFrameNumber(ASurfaceControlStats* stats) {
182 return stats->frameNumber;
183 }
184
ASurfaceTransaction_create()185 ASurfaceTransaction* ASurfaceTransaction_create() {
186 Transaction* transaction = new Transaction;
187 return reinterpret_cast<ASurfaceTransaction*>(transaction);
188 }
189
ASurfaceTransaction_delete(ASurfaceTransaction * aSurfaceTransaction)190 void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
191 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
192 delete transaction;
193 }
194
ASurfaceTransaction_apply(ASurfaceTransaction * aSurfaceTransaction)195 void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
196 CHECK_NOT_NULL(aSurfaceTransaction);
197
198 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
199
200 transaction->apply();
201 }
202
203 struct ASurfaceTransactionStats {
204 std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
205 int64_t latchTime;
206 sp<Fence> presentFence;
207 bool transactionCompleted;
208 };
209
ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats * aSurfaceTransactionStats)210 int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
211 CHECK_NOT_NULL(aSurfaceTransactionStats);
212 return aSurfaceTransactionStats->latchTime;
213 }
214
ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats)215 int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
216 CHECK_NOT_NULL(aSurfaceTransactionStats);
217 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
218 "ASurfaceTransactionStats queried from an incomplete transaction callback");
219
220 auto& presentFence = aSurfaceTransactionStats->presentFence;
221 return (presentFence) ? presentFence->dup() : -1;
222 }
223
ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl *** outASurfaceControls,size_t * outASurfaceControlsSize)224 void 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
ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)246 int64_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
ASurfaceTransactionStats_getPreviousReleaseFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)260 int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
261 ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
262 CHECK_NOT_NULL(aSurfaceTransactionStats);
263 CHECK_NOT_NULL(aSurfaceControl);
264 LOG_ALWAYS_FATAL_IF(!aSurfaceTransactionStats->transactionCompleted,
265 "ASurfaceTransactionStats queried from an incomplete transaction callback");
266
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
ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl ** aSurfaceControls)277 void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
278 CHECK_NOT_NULL(aSurfaceControls);
279
280 SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
281 delete[] surfaceControls;
282 }
283
ASurfaceTransaction_setOnComplete(ASurfaceTransaction * aSurfaceTransaction,void * context,ASurfaceTransaction_OnComplete func)284 void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
285 ASurfaceTransaction_OnComplete func) {
286 CHECK_NOT_NULL(aSurfaceTransaction);
287 CHECK_NOT_NULL(func);
288
289 TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
290 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;
297 aSurfaceTransactionStats.transactionCompleted = true;
298
299 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
300
301 for (const auto& [surfaceControl, latchTime, acquireTime, presentFence, previousReleaseFence, transformHint, frameEvents] : surfaceControlStats) {
302 ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
303 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
304 aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
305 }
306
307 (*func)(callback_context, &aSurfaceTransactionStats);
308 };
309
310 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
311
312 transaction->addTransactionCompletedCallback(callback, context);
313 }
314
ASurfaceTransaction_reparent(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ASurfaceControl * newParentASurfaceControl)315 void 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);
324 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
325
326 transaction->reparent(surfaceControl, newParentSurfaceControl);
327 }
328
ASurfaceTransaction_setVisibility(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int8_t visibility)329 void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
330 ASurfaceControl* aSurfaceControl,
331 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
ASurfaceTransaction_setZOrder(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t z_order)350 void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
351 ASurfaceControl* aSurfaceControl,
352 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
ASurfaceTransaction_setBuffer(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,AHardwareBuffer * buffer,int acquire_fence_fd)362 void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
363 ASurfaceControl* aSurfaceControl,
364 AHardwareBuffer* buffer, int acquire_fence_fd) {
365 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);
374 if (acquire_fence_fd != -1) {
375 sp<Fence> fence = new Fence(acquire_fence_fd);
376 transaction->setAcquireFence(surfaceControl, fence);
377 }
378 }
379
ASurfaceTransaction_setGeometry(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect & source,const ARect & destination,int32_t transform)380 void 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);
385 CHECK_VALID_RECT(source);
386 CHECK_VALID_RECT(destination);
387
388 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
389 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
390
391 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 }
400 transaction->setBufferCrop(surfaceControl, sourceRect);
401 transaction->setDestinationFrame(surfaceControl, destRect);
402 transaction->setTransform(surfaceControl, transform);
403 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
404 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
405 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
406 }
407
ASurfaceTransaction_setCrop(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect & crop)408 void ASurfaceTransaction_setCrop(ASurfaceTransaction* aSurfaceTransaction,
409 ASurfaceControl* aSurfaceControl, const ARect& crop) {
410 CHECK_NOT_NULL(aSurfaceTransaction);
411 CHECK_NOT_NULL(aSurfaceControl);
412 CHECK_VALID_RECT(crop);
413
414 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
415 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
416
417 transaction->setCrop(surfaceControl, static_cast<const Rect&>(crop));
418 }
419
ASurfaceTransaction_setPosition(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t x,int32_t y)420 void ASurfaceTransaction_setPosition(ASurfaceTransaction* aSurfaceTransaction,
421 ASurfaceControl* aSurfaceControl, int32_t x, int32_t y) {
422 CHECK_NOT_NULL(aSurfaceTransaction);
423 CHECK_NOT_NULL(aSurfaceControl);
424
425 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
426 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
427
428 transaction->setPosition(surfaceControl, x, y);
429 }
430
ASurfaceTransaction_setBufferTransform(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t transform)431 void ASurfaceTransaction_setBufferTransform(ASurfaceTransaction* aSurfaceTransaction,
432 ASurfaceControl* aSurfaceControl, int32_t transform) {
433 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
ASurfaceTransaction_setScale(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float xScale,float yScale)445 void 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
ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int8_t transparency)458 void 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
ASurfaceTransaction_setDamageRegion(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect rects[],uint32_t count)472 void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
473 ASurfaceControl* aSurfaceControl,
474 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) {
483 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;
492 }
493
494 transaction->setSurfaceDamageRegion(surfaceControl, region);
495 }
496
ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction * aSurfaceTransaction,int64_t desiredPresentTime)497 void 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
ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float alpha)506 void 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
ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ADataSpace aDataSpace)520 void 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);
527 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
528 transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
529 }
530
ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_smpte2086 * metadata)531 void 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
ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_cta861_3 * metadata)562 void 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 }
584
ASurfaceTransaction_setColor(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float r,float g,float b,float alpha,ADataSpace dataspace)585 void 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);
593 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
594
595 half3 color;
596 color.r = r;
597 color.g = g;
598 color.b = b;
599
600 transaction->setBackgroundColor(surfaceControl, color, alpha,
601 static_cast<ui::Dataspace>(dataspace));
602 }
603
ASurfaceTransaction_setFrameRate(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float frameRate,int8_t compatibility)604 void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
605 ASurfaceControl* aSurfaceControl, float frameRate,
606 int8_t compatibility) {
607 ASurfaceTransaction_setFrameRateWithChangeStrategy(
608 aSurfaceTransaction, aSurfaceControl, frameRate, compatibility,
609 ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS);
610 }
611
ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy)612 void ASurfaceTransaction_setFrameRateWithChangeStrategy(ASurfaceTransaction* aSurfaceTransaction,
613 ASurfaceControl* aSurfaceControl,
614 float frameRate, int8_t compatibility,
615 int8_t changeFrameRateStrategy) {
616 CHECK_NOT_NULL(aSurfaceTransaction);
617 CHECK_NOT_NULL(aSurfaceControl);
618 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
619 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
620 transaction->setFrameRate(surfaceControl, frameRate, compatibility, changeFrameRateStrategy);
621 }
622
ASurfaceTransaction_setEnableBackPressure(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,bool enableBackpressure)623 void 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 }
636
ASurfaceTransaction_setOnCommit(ASurfaceTransaction * aSurfaceTransaction,void * context,ASurfaceTransaction_OnCommit func)637 void 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);
665 }