Polish up metadata propagation for native window

Allow for native window apis to attempt to apply gralloc4 metadata
whenever a buffer is queued onto a native window for dataspace and
existing apis for HDR metadata.

Motivation:
1. We are attempting to move to Gralloc 4 metadata as the source of
   truth for this information, rather than a separate side-channel.
2. ANativeWindow_setBuffersDataspace is a public api, but gralloc4
   metadata is not public, so the only way to keep the gralloc4 metadata
   in sync for dataspace is to intercept on Surface.
3. HDR Metadata may be applied on eglSurface and vkSwapchain, which must
   be propagated to gralloc4 metadata to ensure consistency.
4. None of the plumbing for setting gralloc4 metadata existed, so now's
   a good time as any to add the plumbing so that we can start at least
   best-effort using this metadata infrastructure in the framework and
   so that vendor's don't have to reinvent setting metadata.

Bug: 210498094
Test: builds, boots
Change-Id: I6d19dc4ceca8fa885363faf84f57885e58e88240
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index 20c4146..1fb11e0 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -1096,6 +1096,17 @@
     *out = input;
 }
 
+void Surface::applyGrallocMetadataLocked(
+        android_native_buffer_t* buffer,
+        const IGraphicBufferProducer::QueueBufferInput& queueBufferInput) {
+    ATRACE_CALL();
+    auto& mapper = GraphicBufferMapper::get();
+    mapper.setDataspace(buffer->handle, static_cast<ui::Dataspace>(queueBufferInput.dataSpace));
+    mapper.setSmpte2086(buffer->handle, queueBufferInput.getHdrMetadata().getSmpte2086());
+    mapper.setCta861_3(buffer->handle, queueBufferInput.getHdrMetadata().getCta8613());
+    mapper.setSmpte2094_40(buffer->handle, queueBufferInput.getHdrMetadata().getHdr10Plus());
+}
+
 void Surface::onBufferQueuedLocked(int slot, sp<Fence> fence,
         const IGraphicBufferProducer::QueueBufferOutput& output) {
     mDequeuedSlots.erase(slot);
@@ -1166,9 +1177,11 @@
     IGraphicBufferProducer::QueueBufferOutput output;
     IGraphicBufferProducer::QueueBufferInput input;
     getQueueBufferInputLocked(buffer, fenceFd, mTimestamp, &input);
+    applyGrallocMetadataLocked(buffer, input);
     sp<Fence> fence = input.fence;
 
     nsecs_t now = systemTime();
+
     status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
     mLastQueueDuration = systemTime() - now;
     if (err != OK)  {