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 
21 #include <configstore/Utils.h>
22 
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 
29 #include <ui/HdrCapabilities.h>
30 
31 #include <utils/Timers.h>
32 
33 using namespace android::hardware::configstore;
34 using namespace android::hardware::configstore::V1_0;
35 using namespace android;
36 using android::hardware::configstore::V1_0::ISurfaceFlingerConfigs;
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 
getWideColorSupport(const sp<SurfaceControl> & surfaceControl)47 static bool getWideColorSupport(const sp<SurfaceControl>& surfaceControl) {
48     sp<SurfaceComposerClient> client = surfaceControl->getClient();
49 
50     const sp<IBinder> display = client->getInternalDisplayToken();
51     if (display == nullptr) {
52         ALOGE("unable to get wide color support for disconnected internal display");
53         return false;
54     }
55 
56     bool isWideColorDisplay = false;
57     status_t err = client->isWideColorDisplay(display, &isWideColorDisplay);
58     if (err) {
59         ALOGE("unable to get wide color support");
60         return false;
61     }
62     return isWideColorDisplay;
63 }
64 
getHdrSupport(const sp<SurfaceControl> & surfaceControl)65 static bool getHdrSupport(const sp<SurfaceControl>& surfaceControl) {
66     sp<SurfaceComposerClient> client = surfaceControl->getClient();
67 
68     const sp<IBinder> display = client->getInternalDisplayToken();
69     if (display == nullptr) {
70         ALOGE("unable to get hdr capabilities for disconnected internal display");
71         return false;
72     }
73 
74     HdrCapabilities hdrCapabilities;
75     status_t err = client->getHdrCapabilities(display, &hdrCapabilities);
76     if (err) {
77         ALOGE("unable to get hdr capabilities");
78         return false;
79     }
80 
81     return !hdrCapabilities.getSupportedHdrTypes().empty();
82 }
83 
isDataSpaceValid(const sp<SurfaceControl> & surfaceControl,ADataSpace dataSpace)84 static bool isDataSpaceValid(const sp<SurfaceControl>& surfaceControl, ADataSpace dataSpace) {
85     static_assert(static_cast<int>(ADATASPACE_UNKNOWN) == static_cast<int>(HAL_DATASPACE_UNKNOWN));
86     static_assert(static_cast<int>(ADATASPACE_SCRGB_LINEAR) == static_cast<int>(HAL_DATASPACE_V0_SCRGB_LINEAR));
87     static_assert(static_cast<int>(ADATASPACE_SRGB) == static_cast<int>(HAL_DATASPACE_V0_SRGB));
88     static_assert(static_cast<int>(ADATASPACE_SCRGB) == static_cast<int>(HAL_DATASPACE_V0_SCRGB));
89     static_assert(static_cast<int>(ADATASPACE_DISPLAY_P3) == static_cast<int>(HAL_DATASPACE_DISPLAY_P3));
90     static_assert(static_cast<int>(ADATASPACE_BT2020_PQ) == static_cast<int>(HAL_DATASPACE_BT2020_PQ));
91 
92     switch (static_cast<android_dataspace_t>(dataSpace)) {
93         case HAL_DATASPACE_UNKNOWN:
94         case HAL_DATASPACE_V0_SRGB:
95             return true;
96         // These data space need wide gamut support.
97         case HAL_DATASPACE_V0_SCRGB_LINEAR:
98         case HAL_DATASPACE_V0_SCRGB:
99         case HAL_DATASPACE_DISPLAY_P3:
100             return getWideColorSupport(surfaceControl);
101         // These data space need HDR support.
102         case HAL_DATASPACE_BT2020_PQ:
103             if (!getHdrSupport(surfaceControl)) {
104                 ALOGE("Invalid dataspace - device does not support hdr");
105                 return false;
106             }
107             return true;
108         default:
109             return false;
110     }
111 }
112 
ASurfaceTransaction_to_Transaction(ASurfaceTransaction * aSurfaceTransaction)113 Transaction* ASurfaceTransaction_to_Transaction(ASurfaceTransaction* aSurfaceTransaction) {
114     return reinterpret_cast<Transaction*>(aSurfaceTransaction);
115 }
116 
ASurfaceControl_to_SurfaceControl(ASurfaceControl * aSurfaceControl)117 SurfaceControl* ASurfaceControl_to_SurfaceControl(ASurfaceControl* aSurfaceControl) {
118     return reinterpret_cast<SurfaceControl*>(aSurfaceControl);
119 }
120 
SurfaceControl_acquire(SurfaceControl * surfaceControl)121 void SurfaceControl_acquire(SurfaceControl* surfaceControl) {
122     // incStrong/decStrong token must be the same, doesn't matter what it is
123     surfaceControl->incStrong((void*)SurfaceControl_acquire);
124 }
125 
SurfaceControl_release(SurfaceControl * surfaceControl)126 void SurfaceControl_release(SurfaceControl* surfaceControl) {
127     // incStrong/decStrong token must be the same, doesn't matter what it is
128     surfaceControl->decStrong((void*)SurfaceControl_acquire);
129 }
130 
ASurfaceControl_createFromWindow(ANativeWindow * window,const char * debug_name)131 ASurfaceControl* ASurfaceControl_createFromWindow(ANativeWindow* window, const char* debug_name) {
132     CHECK_NOT_NULL(window);
133     CHECK_NOT_NULL(debug_name);
134 
135     sp<SurfaceComposerClient> client = new SurfaceComposerClient();
136     if (client->initCheck() != NO_ERROR) {
137         return nullptr;
138     }
139 
140     uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
141     sp<SurfaceControl> surfaceControl =
142             client->createWithSurfaceParent(String8(debug_name), 0 /* width */, 0 /* height */,
143                                             // Format is only relevant for buffer queue layers.
144                                             PIXEL_FORMAT_UNKNOWN /* format */, flags,
145                                             static_cast<Surface*>(window));
146     if (!surfaceControl) {
147         return nullptr;
148     }
149 
150     SurfaceControl_acquire(surfaceControl.get());
151     return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
152 }
153 
ASurfaceControl_create(ASurfaceControl * parent,const char * debug_name)154 ASurfaceControl* ASurfaceControl_create(ASurfaceControl* parent, const char* debug_name) {
155     CHECK_NOT_NULL(parent);
156     CHECK_NOT_NULL(debug_name);
157 
158     SurfaceComposerClient* client = ASurfaceControl_to_SurfaceControl(parent)->getClient().get();
159 
160     SurfaceControl* surfaceControlParent = ASurfaceControl_to_SurfaceControl(parent);
161 
162     uint32_t flags = ISurfaceComposerClient::eFXSurfaceBufferState;
163     sp<SurfaceControl> surfaceControl =
164             client->createSurface(String8(debug_name), 0 /* width */, 0 /* height */,
165                                   // Format is only relevant for buffer queue layers.
166                                   PIXEL_FORMAT_UNKNOWN /* format */, flags,
167                                   surfaceControlParent);
168     if (!surfaceControl) {
169         return nullptr;
170     }
171 
172     SurfaceControl_acquire(surfaceControl.get());
173     return reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
174 }
175 
ASurfaceControl_release(ASurfaceControl * aSurfaceControl)176 void ASurfaceControl_release(ASurfaceControl* aSurfaceControl) {
177     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
178 
179     SurfaceControl_release(surfaceControl.get());
180 }
181 
ASurfaceTransaction_create()182 ASurfaceTransaction* ASurfaceTransaction_create() {
183     Transaction* transaction = new Transaction;
184     return reinterpret_cast<ASurfaceTransaction*>(transaction);
185 }
186 
ASurfaceTransaction_delete(ASurfaceTransaction * aSurfaceTransaction)187 void ASurfaceTransaction_delete(ASurfaceTransaction* aSurfaceTransaction) {
188     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
189     delete transaction;
190 }
191 
ASurfaceTransaction_apply(ASurfaceTransaction * aSurfaceTransaction)192 void ASurfaceTransaction_apply(ASurfaceTransaction* aSurfaceTransaction) {
193     CHECK_NOT_NULL(aSurfaceTransaction);
194 
195     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
196 
197     transaction->apply();
198 }
199 
200 typedef struct ASurfaceControlStats {
201     int64_t acquireTime;
202     sp<Fence> previousReleaseFence;
203 } ASurfaceControlStats;
204 
205 struct ASurfaceTransactionStats {
206     std::unordered_map<ASurfaceControl*, ASurfaceControlStats> aSurfaceControlStats;
207     int64_t latchTime;
208     sp<Fence> presentFence;
209 };
210 
ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats * aSurfaceTransactionStats)211 int64_t ASurfaceTransactionStats_getLatchTime(ASurfaceTransactionStats* aSurfaceTransactionStats) {
212     CHECK_NOT_NULL(aSurfaceTransactionStats);
213     return aSurfaceTransactionStats->latchTime;
214 }
215 
ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats)216 int ASurfaceTransactionStats_getPresentFenceFd(ASurfaceTransactionStats* aSurfaceTransactionStats) {
217     CHECK_NOT_NULL(aSurfaceTransactionStats);
218     auto& presentFence = aSurfaceTransactionStats->presentFence;
219     return (presentFence) ? presentFence->dup() : -1;
220 }
221 
ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl *** outASurfaceControls,size_t * outASurfaceControlsSize)222 void ASurfaceTransactionStats_getASurfaceControls(ASurfaceTransactionStats* aSurfaceTransactionStats,
223                                                   ASurfaceControl*** outASurfaceControls,
224                                                   size_t* outASurfaceControlsSize) {
225     CHECK_NOT_NULL(aSurfaceTransactionStats);
226     CHECK_NOT_NULL(outASurfaceControls);
227     CHECK_NOT_NULL(outASurfaceControlsSize);
228 
229     size_t size = aSurfaceTransactionStats->aSurfaceControlStats.size();
230 
231     SurfaceControl** surfaceControls = new SurfaceControl*[size];
232     ASurfaceControl** aSurfaceControls = reinterpret_cast<ASurfaceControl**>(surfaceControls);
233 
234     size_t i = 0;
235     for (auto& [aSurfaceControl, aSurfaceControlStats] : aSurfaceTransactionStats->aSurfaceControlStats) {
236         aSurfaceControls[i] = aSurfaceControl;
237         i++;
238     }
239 
240     *outASurfaceControls = aSurfaceControls;
241     *outASurfaceControlsSize = size;
242 }
243 
ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)244 int64_t ASurfaceTransactionStats_getAcquireTime(ASurfaceTransactionStats* aSurfaceTransactionStats,
245                                                 ASurfaceControl* aSurfaceControl) {
246     CHECK_NOT_NULL(aSurfaceTransactionStats);
247     CHECK_NOT_NULL(aSurfaceControl);
248 
249     const auto& aSurfaceControlStats =
250             aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
251     LOG_ALWAYS_FATAL_IF(
252             aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
253             "ASurfaceControl not found");
254 
255     return aSurfaceControlStats->second.acquireTime;
256 }
257 
ASurfaceTransactionStats_getPreviousReleaseFenceFd(ASurfaceTransactionStats * aSurfaceTransactionStats,ASurfaceControl * aSurfaceControl)258 int ASurfaceTransactionStats_getPreviousReleaseFenceFd(
259             ASurfaceTransactionStats* aSurfaceTransactionStats, ASurfaceControl* aSurfaceControl) {
260     CHECK_NOT_NULL(aSurfaceTransactionStats);
261     CHECK_NOT_NULL(aSurfaceControl);
262 
263     const auto& aSurfaceControlStats =
264             aSurfaceTransactionStats->aSurfaceControlStats.find(aSurfaceControl);
265     LOG_ALWAYS_FATAL_IF(
266             aSurfaceControlStats == aSurfaceTransactionStats->aSurfaceControlStats.end(),
267             "ASurfaceControl not found");
268 
269     auto& previousReleaseFence = aSurfaceControlStats->second.previousReleaseFence;
270     return (previousReleaseFence) ? previousReleaseFence->dup() : -1;
271 }
272 
ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl ** aSurfaceControls)273 void ASurfaceTransactionStats_releaseASurfaceControls(ASurfaceControl** aSurfaceControls) {
274     CHECK_NOT_NULL(aSurfaceControls);
275 
276     SurfaceControl** surfaceControls = reinterpret_cast<SurfaceControl**>(aSurfaceControls);
277     delete[] surfaceControls;
278 }
279 
ASurfaceTransaction_setOnComplete(ASurfaceTransaction * aSurfaceTransaction,void * context,ASurfaceTransaction_OnComplete func)280 void ASurfaceTransaction_setOnComplete(ASurfaceTransaction* aSurfaceTransaction, void* context,
281                                        ASurfaceTransaction_OnComplete func) {
282     CHECK_NOT_NULL(aSurfaceTransaction);
283     CHECK_NOT_NULL(context);
284     CHECK_NOT_NULL(func);
285 
286     TransactionCompletedCallbackTakesContext callback = [func](void* callback_context,
287                                                                nsecs_t latchTime,
288                                                                const sp<Fence>& presentFence,
289                                                                const std::vector<SurfaceControlStats>& surfaceControlStats) {
290         ASurfaceTransactionStats aSurfaceTransactionStats;
291 
292         aSurfaceTransactionStats.latchTime = latchTime;
293         aSurfaceTransactionStats.presentFence = presentFence;
294 
295         auto& aSurfaceControlStats = aSurfaceTransactionStats.aSurfaceControlStats;
296 
297         for (const auto& [surfaceControl, latchTime, acquireTime, presentFence, previousReleaseFence, transformHint, frameEvents] : surfaceControlStats) {
298             ASurfaceControl* aSurfaceControl = reinterpret_cast<ASurfaceControl*>(surfaceControl.get());
299             aSurfaceControlStats[aSurfaceControl].acquireTime = acquireTime;
300             aSurfaceControlStats[aSurfaceControl].previousReleaseFence = previousReleaseFence;
301         }
302 
303         (*func)(callback_context, &aSurfaceTransactionStats);
304     };
305 
306     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
307 
308     transaction->addTransactionCompletedCallback(callback, context);
309 }
310 
ASurfaceTransaction_reparent(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ASurfaceControl * newParentASurfaceControl)311 void ASurfaceTransaction_reparent(ASurfaceTransaction* aSurfaceTransaction,
312                                   ASurfaceControl* aSurfaceControl,
313                                   ASurfaceControl* newParentASurfaceControl) {
314     CHECK_NOT_NULL(aSurfaceTransaction);
315     CHECK_NOT_NULL(aSurfaceControl);
316 
317     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
318     sp<SurfaceControl> newParentSurfaceControl = ASurfaceControl_to_SurfaceControl(
319             newParentASurfaceControl);
320     sp<IBinder> newParentHandle = (newParentSurfaceControl)? newParentSurfaceControl->getHandle() : nullptr;
321     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
322 
323     transaction->reparent(surfaceControl, newParentHandle);
324 }
325 
ASurfaceTransaction_setVisibility(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int8_t visibility)326 void ASurfaceTransaction_setVisibility(ASurfaceTransaction* aSurfaceTransaction,
327                                        ASurfaceControl* aSurfaceControl,
328                                        int8_t visibility) {
329     CHECK_NOT_NULL(aSurfaceTransaction);
330     CHECK_NOT_NULL(aSurfaceControl);
331 
332     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
333     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
334 
335     switch (visibility) {
336     case ASURFACE_TRANSACTION_VISIBILITY_SHOW:
337         transaction->show(surfaceControl);
338         break;
339     case ASURFACE_TRANSACTION_VISIBILITY_HIDE:
340         transaction->hide(surfaceControl);
341         break;
342     default:
343         LOG_ALWAYS_FATAL("invalid visibility %d", visibility);
344     }
345 }
346 
ASurfaceTransaction_setZOrder(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int32_t z_order)347 void ASurfaceTransaction_setZOrder(ASurfaceTransaction* aSurfaceTransaction,
348                                    ASurfaceControl* aSurfaceControl,
349                                    int32_t z_order) {
350     CHECK_NOT_NULL(aSurfaceTransaction);
351     CHECK_NOT_NULL(aSurfaceControl);
352 
353     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
354     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
355 
356     transaction->setLayer(surfaceControl, z_order);
357 }
358 
ASurfaceTransaction_setBuffer(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,AHardwareBuffer * buffer,int acquire_fence_fd)359 void ASurfaceTransaction_setBuffer(ASurfaceTransaction* aSurfaceTransaction,
360                                    ASurfaceControl* aSurfaceControl,
361                                    AHardwareBuffer* buffer, int acquire_fence_fd) {
362     CHECK_NOT_NULL(aSurfaceTransaction);
363     CHECK_NOT_NULL(aSurfaceControl);
364 
365     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
366     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
367 
368     sp<GraphicBuffer> graphic_buffer(reinterpret_cast<GraphicBuffer*>(buffer));
369 
370     transaction->setBuffer(surfaceControl, graphic_buffer);
371     if (acquire_fence_fd != -1) {
372         sp<Fence> fence = new Fence(acquire_fence_fd);
373         transaction->setAcquireFence(surfaceControl, fence);
374     }
375 }
376 
ASurfaceTransaction_setGeometry(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect & source,const ARect & destination,int32_t transform)377 void ASurfaceTransaction_setGeometry(ASurfaceTransaction* aSurfaceTransaction,
378                                      ASurfaceControl* aSurfaceControl, const ARect& source,
379                                      const ARect& destination, int32_t transform) {
380     CHECK_NOT_NULL(aSurfaceTransaction);
381     CHECK_NOT_NULL(aSurfaceControl);
382     CHECK_VALID_RECT(source);
383     CHECK_VALID_RECT(destination);
384 
385     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
386     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
387 
388     transaction->setCrop(surfaceControl, static_cast<const Rect&>(source));
389     transaction->setFrame(surfaceControl, static_cast<const Rect&>(destination));
390     transaction->setTransform(surfaceControl, transform);
391     bool transformToInverseDisplay = (NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY & transform) ==
392             NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
393     transaction->setTransformToDisplayInverse(surfaceControl, transformToInverseDisplay);
394 }
395 
ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,int8_t transparency)396 void ASurfaceTransaction_setBufferTransparency(ASurfaceTransaction* aSurfaceTransaction,
397                                                ASurfaceControl* aSurfaceControl,
398                                                int8_t transparency) {
399     CHECK_NOT_NULL(aSurfaceTransaction);
400     CHECK_NOT_NULL(aSurfaceControl);
401 
402     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
403     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
404 
405     uint32_t flags = (transparency == ASURFACE_TRANSACTION_TRANSPARENCY_OPAQUE) ?
406                       layer_state_t::eLayerOpaque : 0;
407     transaction->setFlags(surfaceControl, flags, layer_state_t::eLayerOpaque);
408 }
409 
ASurfaceTransaction_setDamageRegion(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,const ARect rects[],uint32_t count)410 void ASurfaceTransaction_setDamageRegion(ASurfaceTransaction* aSurfaceTransaction,
411                                          ASurfaceControl* aSurfaceControl,
412                                          const ARect rects[], uint32_t count) {
413     CHECK_NOT_NULL(aSurfaceTransaction);
414     CHECK_NOT_NULL(aSurfaceControl);
415 
416     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
417     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
418 
419     Region region;
420     for (uint32_t i = 0; i < count; ++i) {
421         region.orSelf(static_cast<const Rect&>(rects[i]));
422     }
423 
424     // Hardware composer interprets a DamageRegion with a single Rect of {0,0,0,0} to be an
425     // undamaged region and {0,0,-1,-1} to be a fully damaged buffer. This is a confusing
426     // distinction for a public api. Instead, default both cases to be a fully damaged buffer.
427     if (count == 1 && region.getBounds().isEmpty()) {
428         transaction->setSurfaceDamageRegion(surfaceControl, Region::INVALID_REGION);
429         return;
430     }
431 
432     transaction->setSurfaceDamageRegion(surfaceControl, region);
433 }
434 
ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction * aSurfaceTransaction,int64_t desiredPresentTime)435 void ASurfaceTransaction_setDesiredPresentTime(ASurfaceTransaction* aSurfaceTransaction,
436                                          int64_t desiredPresentTime) {
437     CHECK_NOT_NULL(aSurfaceTransaction);
438 
439     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
440 
441     transaction->setDesiredPresentTime(static_cast<nsecs_t>(desiredPresentTime));
442 }
443 
ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float alpha)444 void ASurfaceTransaction_setBufferAlpha(ASurfaceTransaction* aSurfaceTransaction,
445                                          ASurfaceControl* aSurfaceControl,
446                                          float alpha) {
447     CHECK_NOT_NULL(aSurfaceTransaction);
448     CHECK_NOT_NULL(aSurfaceControl);
449 
450     LOG_ALWAYS_FATAL_IF(alpha < 0.0 || alpha > 1.0, "invalid alpha");
451 
452     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
453     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
454 
455     transaction->setAlpha(surfaceControl, alpha);
456 }
457 
ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,ADataSpace aDataSpace)458 void ASurfaceTransaction_setBufferDataSpace(ASurfaceTransaction* aSurfaceTransaction,
459                                          ASurfaceControl* aSurfaceControl,
460                                          ADataSpace aDataSpace) {
461     CHECK_NOT_NULL(aSurfaceTransaction);
462     CHECK_NOT_NULL(aSurfaceControl);
463 
464     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
465     if (!isDataSpaceValid(surfaceControl, aDataSpace)) {
466         ALOGE("Failed to set buffer dataspace - invalid dataspace");
467         return;
468     }
469     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
470     transaction->setDataspace(surfaceControl, static_cast<ui::Dataspace>(aDataSpace));
471 }
472 
ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_smpte2086 * metadata)473 void ASurfaceTransaction_setHdrMetadata_smpte2086(ASurfaceTransaction* aSurfaceTransaction,
474                                                   ASurfaceControl* aSurfaceControl,
475                                                   struct AHdrMetadata_smpte2086* metadata) {
476     CHECK_NOT_NULL(aSurfaceTransaction);
477     CHECK_NOT_NULL(aSurfaceControl);
478 
479     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
480     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
481 
482     HdrMetadata hdrMetadata;
483 
484     if (metadata) {
485         hdrMetadata.smpte2086.displayPrimaryRed.x = metadata->displayPrimaryRed.x;
486         hdrMetadata.smpte2086.displayPrimaryRed.y = metadata->displayPrimaryRed.y;
487         hdrMetadata.smpte2086.displayPrimaryGreen.x = metadata->displayPrimaryGreen.x;
488         hdrMetadata.smpte2086.displayPrimaryGreen.y = metadata->displayPrimaryGreen.y;
489         hdrMetadata.smpte2086.displayPrimaryBlue.x = metadata->displayPrimaryBlue.x;
490         hdrMetadata.smpte2086.displayPrimaryBlue.y = metadata->displayPrimaryBlue.y;
491         hdrMetadata.smpte2086.whitePoint.x = metadata->whitePoint.x;
492         hdrMetadata.smpte2086.whitePoint.y = metadata->whitePoint.y;
493         hdrMetadata.smpte2086.minLuminance = metadata->minLuminance;
494         hdrMetadata.smpte2086.maxLuminance = metadata->maxLuminance;
495 
496         hdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
497     } else {
498         hdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
499     }
500 
501     transaction->setHdrMetadata(surfaceControl, hdrMetadata);
502 }
503 
ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,struct AHdrMetadata_cta861_3 * metadata)504 void ASurfaceTransaction_setHdrMetadata_cta861_3(ASurfaceTransaction* aSurfaceTransaction,
505                                                  ASurfaceControl* aSurfaceControl,
506                                                  struct AHdrMetadata_cta861_3* metadata) {
507     CHECK_NOT_NULL(aSurfaceTransaction);
508     CHECK_NOT_NULL(aSurfaceControl);
509 
510     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
511     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
512 
513     HdrMetadata hdrMetadata;
514 
515     if (metadata) {
516         hdrMetadata.cta8613.maxContentLightLevel = metadata->maxContentLightLevel;
517         hdrMetadata.cta8613.maxFrameAverageLightLevel = metadata->maxFrameAverageLightLevel;
518 
519         hdrMetadata.validTypes |= HdrMetadata::CTA861_3;
520     } else {
521         hdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
522     }
523 
524     transaction->setHdrMetadata(surfaceControl, hdrMetadata);
525 }
526 
ASurfaceTransaction_setColor(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float r,float g,float b,float alpha,ADataSpace dataspace)527 void ASurfaceTransaction_setColor(ASurfaceTransaction* aSurfaceTransaction,
528                                   ASurfaceControl* aSurfaceControl,
529                                   float r, float g, float b, float alpha,
530                                   ADataSpace dataspace) {
531     CHECK_NOT_NULL(aSurfaceTransaction);
532     CHECK_NOT_NULL(aSurfaceControl);
533 
534     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
535     if (!isDataSpaceValid(surfaceControl, dataspace)) {
536         ALOGE("Failed to set buffer dataspace - invalid dataspace");
537         return;
538     }
539     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
540 
541     half3 color;
542     color.r = r;
543     color.g = g;
544     color.b = b;
545 
546     transaction->setBackgroundColor(surfaceControl, color, alpha, static_cast<ui::Dataspace>(dataspace));
547 }
548 
ASurfaceTransaction_setFrameRate(ASurfaceTransaction * aSurfaceTransaction,ASurfaceControl * aSurfaceControl,float frameRate,int8_t compatibility)549 void ASurfaceTransaction_setFrameRate(ASurfaceTransaction* aSurfaceTransaction,
550                                       ASurfaceControl* aSurfaceControl, float frameRate,
551                                       int8_t compatibility) {
552     CHECK_NOT_NULL(aSurfaceTransaction);
553     CHECK_NOT_NULL(aSurfaceControl);
554     Transaction* transaction = ASurfaceTransaction_to_Transaction(aSurfaceTransaction);
555     sp<SurfaceControl> surfaceControl = ASurfaceControl_to_SurfaceControl(aSurfaceControl);
556     transaction->setFrameRate(surfaceControl, frameRate, compatibility);
557 }
558