blob: fe4753bd9dd6042790171a1cfacae779356def0f [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>
20
Marissa Wall1be5a102019-01-18 16:14:04 -080021#include <configstore/Utils.h>
22
23#include <gui/HdrMetadata.h>
24#include <gui/ISurfaceComposer.h>
Marissa Wallf6a73fa2018-12-10 10:41:08 -080025#include <gui/Surface.h>
26#include <gui/SurfaceComposerClient.h>
27#include <gui/SurfaceControl.h>
28
Marin Shalamanov463ad8e2021-01-28 22:58:37 +010029#include <ui/DynamicDisplayInfo.h>
Marissa Wall1be5a102019-01-18 16:14:04 -080030
31#include <utils/Timers.h>
32
33using namespace android::hardware::configstore;
34using namespace android::hardware::configstore::V1_0;
Marissa Wallf6a73fa2018-12-10 10:41:08 -080035using namespace android;
36
37using Transaction = SurfaceComposerClient::Transaction;
38
39#define CHECK_NOT_NULL(name) \
40 LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument");
41
42#define CHECK_VALID_RECT(name) \
43 LOG_ALWAYS_FATAL_IF(!static_cast<const Rect&>(name).isValid(), \
44 "invalid arg passed as " #name " argument");
45
Valerie Hau5bbfd512019-01-22 17:39:43 -080046static bool getWideColorSupport(const sp<SurfaceControl>& surfaceControl) {
47 sp<SurfaceComposerClient> client = surfaceControl->getClient();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080048
49 const sp<IBinder> display = client->getInternalDisplayToken();
50 if (display == nullptr) {
51 ALOGE("unable to get wide color support for disconnected internal display");
52 return false;
53 }
54
Peiyong Lin0d04f322019-01-24 17:49:46 -080055 bool isWideColorDisplay = false;
56 status_t err = client->isWideColorDisplay(display, &isWideColorDisplay);
Valerie Hau5bbfd512019-01-22 17:39:43 -080057 if (err) {
58 ALOGE("unable to get wide color support");
59 return false;
60 }
Peiyong Lin0d04f322019-01-24 17:49:46 -080061 return isWideColorDisplay;
Valerie Hau5bbfd512019-01-22 17:39:43 -080062}
63
64static bool getHdrSupport(const sp<SurfaceControl>& surfaceControl) {
65 sp<SurfaceComposerClient> client = surfaceControl->getClient();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080066
67 const sp<IBinder> display = client->getInternalDisplayToken();
68 if (display == nullptr) {
69 ALOGE("unable to get hdr capabilities for disconnected internal display");
70 return false;
71 }
Valerie Hau5bbfd512019-01-22 17:39:43 -080072
Marin Shalamanov463ad8e2021-01-28 22:58:37 +010073 ui::DynamicDisplayInfo info;
74 if (status_t err = client->getDynamicDisplayInfo(display, &info); err != NO_ERROR) {
Valerie Hau5bbfd512019-01-22 17:39:43 -080075 ALOGE("unable to get hdr capabilities");
Marin Shalamanov463ad8e2021-01-28 22:58:37 +010076 return err;
Valerie Hau5bbfd512019-01-22 17:39:43 -080077 }
78
Marin Shalamanov463ad8e2021-01-28 22:58:37 +010079 return !info.hdrCapabilities.getSupportedHdrTypes().empty();
Valerie Hau5bbfd512019-01-22 17:39:43 -080080}
81
82static bool isDataSpaceValid(const sp<SurfaceControl>& surfaceControl, ADataSpace dataSpace) {
83 static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
84 static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
85 static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
86 static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
87 static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
88 static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
89
90 switch (static_cast<android_dataspace_t>(dataSpace)) {
91 case HAL_DATASPACE_UNKNOWN:
92 case HAL_DATASPACE_V0_SRGB:
93 return true;
94 // These data space need wide gamut support.
95 case HAL_DATASPACE_V0_SCRGB_LINEAR:
96 case HAL_DATASPACE_V0_SCRGB:
97 case HAL_DATASPACE_DISPLAY_P3:
98 return getWideColorSupport(surfaceControl);
99 // These data space need HDR support.
100 case HAL_DATASPACE_BT2020_PQ:
Valerie Hau5e18c432019-08-26 12:48:49 -0700101 if (!getHdrSupport(surfaceControl)) {
102 ALOGE("Invalid dataspace - device does not support hdr");
103 return false;
104 }
105 return true;
Valerie Hau5bbfd512019-01-22 17:39:43 -0800106 default:
107 return false;
108 }
109}
110
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800111Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) {
112 return reinterpret_cast<Transaction*>(aSurfaceTransaction);
113}
114
115SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) {
116 return reinterpret_cast<SurfaceControl*>(aSurfaceControl);
117}
118
119void SurfaceControl_acquire(SurfaceControl* surfaceControl) {
120 // incStrong/decStrong token must be the same, doesn't matter what it is
121 surfaceControl->incStrong((void*)SurfaceControl_acquire);
122}
123
124void SurfaceControl_release(SurfaceControl* surfaceControl) {
125 // incStrong/decStrong token must be the same, doesn't matter what it is
126 surfaceControl->decStrong((void*)SurfaceControl_acquire);
127}
128
129ASurfaceControl* ASurfaceControl_createFromWindow(ANativeWindow* window, const char* debug_name) {
130 CHECK_NOT_NULL(window);
131 CHECK_NOT_NULL(debug_name);
132
133 sp<SurfaceComposerClient> client = new SurfaceComposerClient();
134 if (client->initCheck() != NO_ERROR) {
135 return nullptr;
136 }
137
Vishnu Nairce1a6482020-10-22 17:41:30 -0700138 Surface* surface = static_cast<Surface*>(window);
139 sp<IBinder> parentHandle = surface->getSurfaceControlHandle();
140
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800141 uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
Vishnu Nairce1a6482020-10-22 17:41:30 -0700142 sp<SurfaceControl> surfaceControl;
143 if (parentHandle) {
144 surfaceControl =
145 client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */,
146 // Format is only relevant for buffer queue layers.
147 PIXEL_FORMAT_UNKNOWN /* format */, flags, parentHandle);
148 } else {
149 surfaceControl =
150 client->createWithSurfaceParent(String8(debug_name), 0 /* width */, 0 /* height */,
151 // Format is only relevant for buffer queue layers.
152 PIXEL_FORMAT_UNKNOWN /* format */, flags,
153 static_cast<Surface*>(window));
154 }
155
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800156 if (!surfaceControl) {
157 return nullptr;
158 }
159
160 SurfaceControl_acquire(surfaceControl.get());
161 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
162}
163
164ASurfaceControl* ASurfaceControl_create(ASurfaceControl* parent, const char* debug_name) {
165 CHECK_NOT_NULL(parent);
166 CHECK_NOT_NULL(debug_name);
167
168 SurfaceComposerClient* client = ASurfaceControl_to_SurfaceControl(parent)->getClient().get();
169
170 SurfaceControl* surfaceControlParent = ASurfaceControl_to_SurfaceControl(parent);
171
172 uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
173 sp<SurfaceControl> surfaceControl =
174 client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */,
175 // Format is only relevant for buffer queue layers.
176 PIXEL_FORMAT_UNKNOWN /* format */, flags,
Vishnu Nairce1a6482020-10-22 17:41:30 -0700177 surfaceControlParent->getHandle());
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800178 if (!surfaceControl) {
179 return nullptr;
180 }
181
182 SurfaceControl_acquire(surfaceControl.get());
183 return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
184}
185
Huihong Luo91697e12021-01-28 15:24:19 -0800186void ASurfaceControl_acquire(ASurfaceControl* aSurfaceControl) {
187 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800188
Huihong Luo91697e12021-01-28 15:24:19 -0800189 SurfaceControl_acquire(surfaceControl);
190}
191
192void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
193 SurfaceControl* surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
194
195 SurfaceControl_release(surfaceControl);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800196}
197
198ASurfaceTransaction* ASurfaceTransaction_create() {
199 Transaction* transaction = new Transaction;
200 return reinterpret_cast<ASurfaceTransaction*>(transaction);
201}
202
203void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
204 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
205 delete transaction;
206}
207
208void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
209 CHECK_NOT_NULL(aSurfaceTransaction);
210
211 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
212
213 transaction->apply();
214}
215
Marissa Wall1be5a102019-01-18 16:14:04 -0800216typedef struct ASurfaceControlStats {
217 int64_t acquireTime;
218 sp<Fence> previousReleaseFence;
219} ASurfaceControlStats;
220
221struct ASurfaceTransactionStats {
222 std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
223 int64_t latchTime;
224 sp<Fence> presentFence;
225};
226
227int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
228 CHECK_NOT_NULL(aSurfaceTransactionStats);
229 return aSurfaceTransactionStats->latchTime;
230}
231
232int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
233 CHECK_NOT_NULL(aSurfaceTransactionStats);
234 auto& presentFence = aSurfaceTransactionStats->presentFence;
235 return (presentFence) ? presentFence->dup() : -1;
236}
237
238void ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats* aSurfaceTransactionStats,
239 ASurfaceControl*** outASurfaceControls,
240 size_t* outASurfaceControlsSize) {
241 CHECK_NOT_NULL(aSurfaceTransactionStats);
242 CHECK_NOT_NULL(outASurfaceControls);
243 CHECK_NOT_NULL(outASurfaceControlsSize);
244
245 size_t size = aSurfaceTransactionStats->aSurfaceControlStats.size();
246
247 SurfaceControl** surfaceControls = new SurfaceControl*[size];
248 ASurfaceControl** aSurfaceControls = reinterpret_cast<ASurfaceControl**>(surfaceControls);
249
250 size_t i = 0;
251 for (auto& [aSurfaceControl, aSurfaceControlStats] : aSurfaceTransactionStats->aSurfaceControlStats) {
252 aSurfaceControls[i] = aSurfaceControl;
253 i++;
254 }
255
256 *outASurfaceControls = aSurfaceControls;
257 *outASurfaceControlsSize = size;
258}
259
260int64_t ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats* aSurfaceTransactionStats,
261 ASurfaceControl* aSurfaceControl) {
262 CHECK_NOT_NULL(aSurfaceTransactionStats);
263 CHECK_NOT_NULL(aSurfaceControl);
264
265 const auto& aSurfaceControlStats =
266 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
267 LOG_ALWAYS_FATAL_IF(
268 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
269 "ASurfaceControl not found");
270
271 return aSurfaceControlStats->second.acquireTime;
272}
273
274int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
275 ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
276 CHECK_NOT_NULL(aSurfaceTransactionStats);
277 CHECK_NOT_NULL(aSurfaceControl);
278
279 const auto& aSurfaceControlStats =
280 aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
281 LOG_ALWAYS_FATAL_IF(
282 aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
283 "ASurfaceControl not found");
284
285 auto& previousReleaseFence = aSurfaceControlStats->second.previousReleaseFence;
286 return (previousReleaseFence) ? previousReleaseFence->dup() : -1;
287}
288
289void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
290 CHECK_NOT_NULL(aSurfaceControls);
291
292 SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
293 delete[] surfaceControls;
294}
295
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800296void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
297 ASurfaceTransaction_OnComplete func) {
298 CHECK_NOT_NULL(aSurfaceTransaction);
299 CHECK_NOT_NULL(context);
300 CHECK_NOT_NULL(func);
301
302 TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
Marissa Wall1be5a102019-01-18 16:14:04 -0800303 nsecs_t latchTime,
304 const sp<Fence>& presentFence,
305 const std::vector<SurfaceControlStats>& surfaceControlStats) {
306 ASurfaceTransactionStats aSurfaceTransactionStats;
307
308 aSurfaceTransactionStats.latchTime = latchTime;
309 aSurfaceTransactionStats.presentFence = presentFence;
310
311 auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
312
Valerie Haud6a222e2020-01-29 14:27:09 -0800313 for (const auto& [surfaceControl, latchTime, acquireTime, presentFence, previousReleaseFence, transformHint, frameEvents] : surfaceControlStats) {
Marissa Wall1be5a102019-01-18 16:14:04 -0800314 ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
315 aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
316 aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
317 }
318
319 (*func)(callback_context, &aSurfaceTransactionStats);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800320 };
321
322 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
323
324 transaction->addTransactionCompletedCallback(callback, context);
325}
326
Marissa Wall1be5a102019-01-18 16:14:04 -0800327void ASurfaceTransaction_reparent(ASurfaceTransaction* aSurfaceTransaction,
328 ASurfaceControl* aSurfaceControl,
329 ASurfaceControl* newParentASurfaceControl) {
330 CHECK_NOT_NULL(aSurfaceTransaction);
331 CHECK_NOT_NULL(aSurfaceControl);
332
333 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
334 sp<SurfaceControl> newParentSurfaceControl = ASurfaceControl_to_SurfaceControl(
335 newParentASurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800336 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
337
Pablo Gamito117040c2020-09-14 08:24:41 +0000338 transaction->reparent(surfaceControl, newParentSurfaceControl);
Marissa Wall1be5a102019-01-18 16:14:04 -0800339}
340
341void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
342 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800343 int8_t visibility) {
344 CHECK_NOT_NULL(aSurfaceTransaction);
345 CHECK_NOT_NULL(aSurfaceControl);
346
347 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
348 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
349
350 switch (visibility) {
351 case ASURFACE_TRANSACTION_VISIBILITY_SHOW:
352 transaction->show(surfaceControl);
353 break;
354 case ASURFACE_TRANSACTION_VISIBILITY_HIDE:
355 transaction->hide(surfaceControl);
356 break;
357 default:
358 LOG_ALWAYS_FATAL("invalid visibility %d", visibility);
359 }
360}
361
Marissa Wall1be5a102019-01-18 16:14:04 -0800362void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
363 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800364 int32_t z_order) {
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 transaction->setLayer(surfaceControl, z_order);
372}
373
Marissa Wall1be5a102019-01-18 16:14:04 -0800374void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
375 ASurfaceControl* aSurfaceControl,
376 AHardwareBuffer* buffer, int acquire_fence_fd) {
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800377 CHECK_NOT_NULL(aSurfaceTransaction);
378 CHECK_NOT_NULL(aSurfaceControl);
379
380 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
381 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
382
383 sp<GraphicBuffer> graphic_buffer(reinterpret_cast<GraphicBuffer*>(buffer));
384
385 transaction->setBuffer(surfaceControl, graphic_buffer);
Marissa Wall1be5a102019-01-18 16:14:04 -0800386 if (acquire_fence_fd != -1) {
387 sp<Fence> fence = new Fence(acquire_fence_fd);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800388 transaction->setAcquireFence(surfaceControl, fence);
389 }
390}
391
392void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction,
393 ASurfaceControl* aSurfaceControl, const ARect& source,
394 const ARect& destination, int32_t transform) {
395 CHECK_NOT_NULL(aSurfaceTransaction);
396 CHECK_NOT_NULL(aSurfaceControl);
397 CHECK_VALID_RECT(source);
398 CHECK_VALID_RECT(destination);
399
400 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
401 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
402
403 transaction->setCrop(surfaceControl, static_cast<const Rect&>(source));
404 transaction->setFrame(surfaceControl, static_cast<const Rect&>(destination));
405 transaction->setTransform(surfaceControl, transform);
Vishnu Nair1ad69542019-05-23 16:27:45 +0800406 bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
407 NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
408 transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800409}
410
411void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction,
412 ASurfaceControl* aSurfaceControl,
413 int8_t transparency) {
414 CHECK_NOT_NULL(aSurfaceTransaction);
415 CHECK_NOT_NULL(aSurfaceControl);
416
417 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
418 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
419
420 uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ?
421 layer_state_t::eLayerOpaque : 0;
422 transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque);
423}
424
Marissa Wall1be5a102019-01-18 16:14:04 -0800425void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
426 ASurfaceControl* aSurfaceControl,
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800427 const ARect rects[], uint32_t count) {
428 CHECK_NOT_NULL(aSurfaceTransaction);
429 CHECK_NOT_NULL(aSurfaceControl);
430
431 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
432 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
433
434 Region region;
435 for (uint32_t i = 0; i < count; ++i) {
Marissa Wallbb9b14f2019-04-23 14:10:15 -0700436 region.orSelf(static_cast<const Rect&>(rects[i]));
437 }
438
439 // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an
440 // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing
441 // distinction for a public api. Instead, default both cases to be a fully damaged buffer.
442 if (count == 1 && region.getBounds().isEmpty()) {
443 transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION);
444 return;
Marissa Wallf6a73fa2018-12-10 10:41:08 -0800445 }
446
447 transaction->setSurfaceDamageRegion(surfaceControl, region);
448}
Marissa Wall1be5a102019-01-18 16:14:04 -0800449
450void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction,
451 int64_t desiredPresentTime) {
452 CHECK_NOT_NULL(aSurfaceTransaction);
453
454 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
455
456 transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime));
457}
458
459void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction,
460 ASurfaceControl* aSurfaceControl,
461 float alpha) {
462 CHECK_NOT_NULL(aSurfaceTransaction);
463 CHECK_NOT_NULL(aSurfaceControl);
464
465 LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha");
466
467 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
468 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
469
470 transaction->setAlpha(surfaceControl, alpha);
471}
472
Marissa Wall7f24f792019-02-07 14:06:04 -0800473void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction,
474 ASurfaceControl* aSurfaceControl,
475 ADataSpace aDataSpace) {
476 CHECK_NOT_NULL(aSurfaceTransaction);
477 CHECK_NOT_NULL(aSurfaceControl);
478
479 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Valerie Hau5e18c432019-08-26 12:48:49 -0700480 if (!isDataSpaceValid(surfaceControl, aDataSpace)) {
481 ALOGE("Failed to set buffer dataspace - invalid dataspace");
482 return;
483 }
Marissa Wall7f24f792019-02-07 14:06:04 -0800484 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Marissa Wall7f24f792019-02-07 14:06:04 -0800485 transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
486}
487
Marissa Wall1be5a102019-01-18 16:14:04 -0800488void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction,
489 ASurfaceControl* aSurfaceControl,
490 struct AHdrMetadata_smpte2086* metadata) {
491 CHECK_NOT_NULL(aSurfaceTransaction);
492 CHECK_NOT_NULL(aSurfaceControl);
493
494 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
495 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
496
497 HdrMetadata hdrMetadata;
498
499 if (metadata) {
500 hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x;
501 hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y;
502 hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x;
503 hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y;
504 hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x;
505 hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y;
506 hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x;
507 hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y;
508 hdrMetadata.smpte2086.minLuminance = metadata->minLuminance;
509 hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance;
510
511 hdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
512 } else {
513 hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
514 }
515
516 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
517}
518
519void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction,
520 ASurfaceControl* aSurfaceControl,
521 struct AHdrMetadata_cta861_3* metadata) {
522 CHECK_NOT_NULL(aSurfaceTransaction);
523 CHECK_NOT_NULL(aSurfaceControl);
524
525 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
526 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
527
528 HdrMetadata hdrMetadata;
529
530 if (metadata) {
531 hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel;
532 hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel;
533
534 hdrMetadata.validTypes |= HdrMetadata::CTA861_3;
535 } else {
536 hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
537 }
538
539 transaction->setHdrMetadata(surfaceControl, hdrMetadata);
540}
Valerie Hau5bbfd512019-01-22 17:39:43 -0800541
542void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
543 ASurfaceControl* aSurfaceControl,
544 float r, float g, float b, float alpha,
545 ADataSpace dataspace) {
546 CHECK_NOT_NULL(aSurfaceTransaction);
547 CHECK_NOT_NULL(aSurfaceControl);
548
549 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Valerie Hau5e18c432019-08-26 12:48:49 -0700550 if (!isDataSpaceValid(surfaceControl, dataspace)) {
551 ALOGE("Failed to set buffer dataspace - invalid dataspace");
552 return;
553 }
Valerie Hau5bbfd512019-01-22 17:39:43 -0800554 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
555
556 half3 color;
557 color.r = r;
558 color.g = g;
559 color.b = b;
560
Valerie Hau6bc482312019-01-29 15:01:53 -0800561 transaction->setBackgroundColor(surfaceControl, color, alpha, static_cast<ui::Dataspace>(dataspace));
Valerie Hau5bbfd512019-01-22 17:39:43 -0800562}
Steven Thomas6cf051e2020-01-14 11:37:21 -0800563
564void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800565 ASurfaceControl* aSurfaceControl, float frameRate,
566 int8_t compatibility) {
Marin Shalamanov41ffa8d2020-10-13 12:35:20 +0200567 ASurfaceTransaction_setFrameRateWithSeamlessness(aSurfaceTransaction, aSurfaceControl,
568 frameRate, compatibility,
569 /*shouldBeSeamless*/ true);
570}
571
572void ASurfaceTransaction_setFrameRateWithSeamlessness(ASurfaceTransaction* aSurfaceTransaction,
573 ASurfaceControl* aSurfaceControl,
574 float frameRate, int8_t compatibility,
575 bool shouldBeSeamless) {
Steven Thomas6cf051e2020-01-14 11:37:21 -0800576 CHECK_NOT_NULL(aSurfaceTransaction);
577 CHECK_NOT_NULL(aSurfaceControl);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800578 Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
Steven Thomasdd7bf2f2020-01-31 18:50:02 -0800579 sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
Marin Shalamanov41ffa8d2020-10-13 12:35:20 +0200580 transaction->setFrameRate(surfaceControl, frameRate, compatibility, shouldBeSeamless);
Steven Thomas6cf051e2020-01-14 11:37:21 -0800581}