1 /*
2 * Copyright 2015 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SurfaceUtils"
19 #include <utils/Log.h>
20
21 #include <android/api-level.h>
22 #include <media/hardware/VideoAPI.h>
23 #include <media/stagefright/SurfaceUtils.h>
24 #include <gui/Surface.h>
25
26 extern "C" int android_get_application_target_sdk_version();
27
28 namespace android {
29
setNativeWindowSizeFormatAndUsage(ANativeWindow * nativeWindow,int width,int height,int format,int rotation,int usage,bool reconnect)30 status_t setNativeWindowSizeFormatAndUsage(
31 ANativeWindow *nativeWindow /* nonnull */,
32 int width, int height, int format, int rotation, int usage, bool reconnect) {
33 status_t err = NO_ERROR;
34
35 // In some cases we need to reconnect so that we can dequeue all buffers
36 if (reconnect) {
37 err = nativeWindowDisconnect(nativeWindow, "setNativeWindowSizeFormatAndUsage");
38 if (err != NO_ERROR) {
39 ALOGE("nativeWindowDisconnect failed: %s (%d)", strerror(-err), -err);
40 return err;
41 }
42
43 err = nativeWindowConnect(nativeWindow, "setNativeWindowSizeFormatAndUsage");
44 if (err != NO_ERROR) {
45 ALOGE("nativeWindowConnect failed: %s (%d)", strerror(-err), -err);
46 return err;
47 }
48 }
49
50 err = native_window_set_buffers_dimensions(nativeWindow, width, height);
51 if (err != NO_ERROR) {
52 ALOGE("native_window_set_buffers_dimensions failed: %s (%d)", strerror(-err), -err);
53 return err;
54 }
55
56 err = native_window_set_buffers_format(nativeWindow, format);
57 if (err != NO_ERROR) {
58 ALOGE("native_window_set_buffers_format failed: %s (%d)", strerror(-err), -err);
59 return err;
60 }
61
62 int transform = 0;
63 if ((rotation % 90) == 0) {
64 switch ((rotation / 90) & 3) {
65 case 1: transform = HAL_TRANSFORM_ROT_90; break;
66 case 2: transform = HAL_TRANSFORM_ROT_180; break;
67 case 3: transform = HAL_TRANSFORM_ROT_270; break;
68 default: transform = 0; break;
69 }
70 }
71
72 err = native_window_set_buffers_transform(nativeWindow, transform);
73 if (err != NO_ERROR) {
74 ALOGE("native_window_set_buffers_transform failed: %s (%d)", strerror(-err), -err);
75 return err;
76 }
77
78 int consumerUsage = 0;
79 err = nativeWindow->query(nativeWindow, NATIVE_WINDOW_CONSUMER_USAGE_BITS, &consumerUsage);
80 if (err != NO_ERROR) {
81 ALOGW("failed to get consumer usage bits. ignoring");
82 err = NO_ERROR;
83 }
84
85 // Make sure to check whether either Stagefright or the video decoder
86 // requested protected buffers.
87 if (usage & GRALLOC_USAGE_PROTECTED) {
88 // Check if the ANativeWindow sends images directly to SurfaceFlinger.
89 int queuesToNativeWindow = 0;
90 err = nativeWindow->query(
91 nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER, &queuesToNativeWindow);
92 if (err != NO_ERROR) {
93 ALOGE("error authenticating native window: %s (%d)", strerror(-err), -err);
94 return err;
95 }
96
97 // Check if the consumer end of the ANativeWindow can handle protected content.
98 int isConsumerProtected = 0;
99 err = nativeWindow->query(
100 nativeWindow, NATIVE_WINDOW_CONSUMER_IS_PROTECTED, &isConsumerProtected);
101 if (err != NO_ERROR) {
102 ALOGE("error query native window: %s (%d)", strerror(-err), -err);
103 return err;
104 }
105
106 // Deny queuing into native window if neither condition is satisfied.
107 if (queuesToNativeWindow != 1 && isConsumerProtected != 1) {
108 ALOGE("native window cannot handle protected buffers: the consumer should either be "
109 "a hardware composer or support hardware protection");
110 return PERMISSION_DENIED;
111 }
112 }
113
114 uint64_t finalUsage = (uint32_t) usage | (uint32_t) consumerUsage;
115 ALOGV("gralloc usage: %#x(producer) + %#x(consumer) = 0x%" PRIx64,
116 usage, consumerUsage, finalUsage);
117 err = native_window_set_usage(nativeWindow, finalUsage);
118 if (err != NO_ERROR) {
119 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
120 return err;
121 }
122
123 err = native_window_set_scaling_mode(
124 nativeWindow, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
125 if (err != NO_ERROR) {
126 ALOGE("native_window_set_scaling_mode failed: %s (%d)", strerror(-err), -err);
127 return err;
128 }
129
130 ALOGD("set up nativeWindow %p for %dx%d, color %#x, rotation %d, usage 0x%" PRIx64,
131 nativeWindow, width, height, format, rotation, finalUsage);
132 return NO_ERROR;
133 }
134
setNativeWindowHdrMetadata(ANativeWindow * nativeWindow,HDRStaticInfo * info)135 void setNativeWindowHdrMetadata(ANativeWindow *nativeWindow, HDRStaticInfo *info) {
136 // If mastering max and min luminance fields are 0, do not use them.
137 // It indicates the value may not be present in the stream.
138 if ((float)info->sType1.mMaxDisplayLuminance > 0.0f &&
139 (info->sType1.mMinDisplayLuminance * 0.0001f) > 0.0f) {
140 struct android_smpte2086_metadata smpte2086_meta = {
141 .displayPrimaryRed = {
142 info->sType1.mR.x * 0.00002f,
143 info->sType1.mR.y * 0.00002f
144 },
145 .displayPrimaryGreen = {
146 info->sType1.mG.x * 0.00002f,
147 info->sType1.mG.y * 0.00002f
148 },
149 .displayPrimaryBlue = {
150 info->sType1.mB.x * 0.00002f,
151 info->sType1.mB.y * 0.00002f
152 },
153 .whitePoint = {
154 info->sType1.mW.x * 0.00002f,
155 info->sType1.mW.y * 0.00002f
156 },
157 .maxLuminance = (float) info->sType1.mMaxDisplayLuminance,
158 .minLuminance = info->sType1.mMinDisplayLuminance * 0.0001f
159 };
160
161 int err = native_window_set_buffers_smpte2086_metadata(nativeWindow, &smpte2086_meta);
162 ALOGW_IF(err != 0, "failed to set smpte2086 metadata on surface (%d)", err);
163 }
164
165 // If the content light level fields are 0, do not use them, it
166 // indicates the value may not be present in the stream.
167 if ((float)info->sType1.mMaxContentLightLevel > 0.0f &&
168 (float)info->sType1.mMaxFrameAverageLightLevel > 0.0f) {
169 struct android_cta861_3_metadata cta861_meta = {
170 .maxContentLightLevel = (float) info->sType1.mMaxContentLightLevel,
171 .maxFrameAverageLightLevel = (float) info->sType1.mMaxFrameAverageLightLevel
172 };
173
174 int err = native_window_set_buffers_cta861_3_metadata(nativeWindow, &cta861_meta);
175 ALOGW_IF(err != 0, "failed to set cta861_3 metadata on surface (%d)", err);
176 }
177 }
178
setNativeWindowRotation(ANativeWindow * nativeWindow,int rotation)179 status_t setNativeWindowRotation(
180 ANativeWindow *nativeWindow /* nonnull */, int rotation) {
181
182 int transform = 0;
183 if ((rotation % 90) == 0) {
184 switch ((rotation / 90) & 3) {
185 case 1: transform = HAL_TRANSFORM_ROT_90; break;
186 case 2: transform = HAL_TRANSFORM_ROT_180; break;
187 case 3: transform = HAL_TRANSFORM_ROT_270; break;
188 default: transform = 0; break;
189 }
190 }
191
192 return native_window_set_buffers_transform(nativeWindow, transform);
193 }
194
pushBlankBuffersToNativeWindow(ANativeWindow * nativeWindow)195 status_t pushBlankBuffersToNativeWindow(ANativeWindow *nativeWindow /* nonnull */) {
196 status_t err = NO_ERROR;
197 ANativeWindowBuffer* anb = nullptr;
198 int numBufs = 0;
199 int minUndequeuedBufs = 0;
200
201 auto handleError = [](ANativeWindow *nativeWindow, ANativeWindowBuffer* anb, status_t err)
202 {
203 if (anb != nullptr) {
204 nativeWindow->cancelBuffer(nativeWindow, anb, -1);
205 anb = nullptr;
206 }
207
208 // Clean up after success or error.
209 status_t err2 = native_window_api_disconnect(nativeWindow, NATIVE_WINDOW_API_CPU);
210 if (err2 != NO_ERROR) {
211 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)",
212 strerror(-err2), -err2);
213 if (err == NO_ERROR) {
214 err = err2;
215 }
216 }
217
218 err2 = nativeWindowConnect(nativeWindow, "pushBlankBuffersToNativeWindow(err2)");
219 if (err2 != NO_ERROR) {
220 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", strerror(-err), -err);
221 if (err == NO_ERROR) {
222 err = err2;
223 }
224 }
225
226 return err;
227 };
228
229 // We need to set sidebandStream to nullptr before pushing blank buffers
230 err = native_window_set_sideband_stream(nativeWindow, nullptr);
231 if (err != NO_ERROR) {
232 ALOGE("error setting sidebandStream to nullptr: %s (%d)", strerror(-err), -err);
233 return err;
234 }
235
236 // We need to reconnect to the ANativeWindow as a CPU client to ensure that
237 // no frames get dropped by SurfaceFlinger assuming that these are video
238 // frames.
239 err = nativeWindowDisconnect(nativeWindow, "pushBlankBuffersToNativeWindow");
240 if (err != NO_ERROR) {
241 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)", strerror(-err), -err);
242 return err;
243 }
244
245 err = native_window_api_connect(nativeWindow, NATIVE_WINDOW_API_CPU);
246 if (err != NO_ERROR) {
247 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", strerror(-err), -err);
248 (void)nativeWindowConnect(nativeWindow, "pushBlankBuffersToNativeWindow(err)");
249 return err;
250 }
251
252 err = setNativeWindowSizeFormatAndUsage(
253 nativeWindow, 1, 1, HAL_PIXEL_FORMAT_RGBX_8888, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
254 false /* reconnect */);
255 if (err != NO_ERROR) {
256 return handleError(nativeWindow, anb, err);
257 }
258
259 static_cast<Surface*>(nativeWindow)->getIGraphicBufferProducer()->allowAllocation(true);
260
261 // In nonblocking mode(timetout = 0), native_window_dequeue_buffer_and_wait()
262 // can fail with timeout. Changing to blocking mode will ensure that dequeue
263 // does not timeout.
264 static_cast<Surface*>(nativeWindow)->getIGraphicBufferProducer()->setDequeueTimeout(-1);
265
266 err = nativeWindow->query(nativeWindow,
267 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs);
268 if (err != NO_ERROR) {
269 ALOGE("error pushing blank frames: MIN_UNDEQUEUED_BUFFERS query "
270 "failed: %s (%d)", strerror(-err), -err);
271 return handleError(nativeWindow, anb, err);
272 }
273
274 numBufs = minUndequeuedBufs + 1;
275 err = native_window_set_buffer_count(nativeWindow, numBufs);
276 if (err != NO_ERROR) {
277 ALOGE("error pushing blank frames: set_buffer_count failed: %s (%d)", strerror(-err), -err);
278 return handleError(nativeWindow, anb, err);
279 }
280
281 // We push numBufs + 1 buffers to ensure that we've drawn into the same
282 // buffer twice. This should guarantee that the buffer has been displayed
283 // on the screen and then been replaced, so an previous video frames are
284 // guaranteed NOT to be currently displayed.
285 for (int i = 0; i < numBufs + 1; i++) {
286 err = native_window_dequeue_buffer_and_wait(nativeWindow, &anb);
287 if (err != NO_ERROR) {
288 ALOGE("error pushing blank frames: dequeueBuffer failed: %s (%d)",
289 strerror(-err), -err);
290 break;
291 }
292
293 sp<GraphicBuffer> buf(GraphicBuffer::from(anb));
294
295 // Fill the buffer with the a 1x1 checkerboard pattern ;)
296 uint32_t *img = nullptr;
297 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
298 if (err != NO_ERROR) {
299 ALOGE("error pushing blank frames: lock failed: %s (%d)", strerror(-err), -err);
300 break;
301 }
302 if (img == nullptr) {
303 (void)buf->unlock(); // Since lock() was successful.
304 ALOGE("error pushing blank frames: lock succeeded: buf mapping is nullptr");
305 break;
306 }
307
308 *img = 0;
309
310 err = buf->unlock();
311 if (err != NO_ERROR) {
312 ALOGE("error pushing blank frames: unlock failed: %s (%d)", strerror(-err), -err);
313 break;
314 }
315
316 err = nativeWindow->queueBuffer(nativeWindow, buf->getNativeBuffer(), -1);
317 if (err != NO_ERROR) {
318 ALOGE("error pushing blank frames: queueBuffer failed: %s (%d)", strerror(-err), -err);
319 break;
320 }
321
322 anb = nullptr;
323 }
324
325 return handleError(nativeWindow, anb, err);
326 }
327
nativeWindowConnect(ANativeWindow * surface,const char * reason)328 status_t nativeWindowConnect(ANativeWindow *surface, const char *reason) {
329 ALOGD("connecting to surface %p, reason %s", surface, reason);
330
331 status_t err = native_window_api_connect(surface, NATIVE_WINDOW_API_MEDIA);
332 ALOGE_IF(err != OK, "Failed to connect to surface %p, err %d", surface, err);
333
334 return err;
335 }
336
surfaceConnectWithListener(const sp<Surface> & surface,sp<IProducerListener> listener,const char * reason)337 status_t surfaceConnectWithListener(
338 const sp<Surface> &surface, sp<IProducerListener> listener, const char *reason) {
339 ALOGD("connecting to surface %p, reason %s", surface.get(), reason);
340
341 status_t err = surface->connect(NATIVE_WINDOW_API_MEDIA, listener);
342 ALOGE_IF(err != OK, "Failed to connect from surface %p, err %d", surface.get(), err);
343
344 return err;
345 }
346
nativeWindowDisconnect(ANativeWindow * surface,const char * reason)347 status_t nativeWindowDisconnect(ANativeWindow *surface, const char *reason) {
348 ALOGD("disconnecting from surface %p, reason %s", surface, reason);
349
350 status_t err = native_window_api_disconnect(surface, NATIVE_WINDOW_API_MEDIA);
351 ALOGE_IF(err != OK, "Failed to disconnect from surface %p, err %d", surface, err);
352
353 return err;
354 }
355
disableLegacyBufferDropPostQ(const sp<Surface> & surface)356 status_t disableLegacyBufferDropPostQ(const sp<Surface> &surface) {
357 sp<IGraphicBufferProducer> igbp =
358 surface ? surface->getIGraphicBufferProducer() : nullptr;
359 if (igbp) {
360 int targetSdk = android_get_application_target_sdk_version();
361 // When the caller is not an app (e.g. MediaPlayer in mediaserver)
362 // targetSdk is __ANDROID_API_FUTURE__.
363 bool drop =
364 targetSdk < __ANDROID_API_Q__ ||
365 targetSdk == __ANDROID_API_FUTURE__;
366 if (!drop) {
367 status_t err = igbp->setLegacyBufferDrop(false);
368 if (err == NO_ERROR) {
369 ALOGD("legacy buffer drop disabled: target sdk (%d)",
370 targetSdk);
371 } else {
372 ALOGD("disabling legacy buffer drop failed: %d", err);
373 }
374 }
375 }
376 return NO_ERROR;
377 }
378 } // namespace android
379
380