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 int finalUsage = usage | consumerUsage;
115 ALOGV("gralloc usage: %#x(producer) + %#x(consumer) = %#x", usage, consumerUsage, finalUsage);
116 err = native_window_set_usage(nativeWindow, finalUsage);
117 if (err != NO_ERROR) {
118 ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
119 return err;
120 }
121
122 err = native_window_set_scaling_mode(
123 nativeWindow, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
124 if (err != NO_ERROR) {
125 ALOGE("native_window_set_scaling_mode failed: %s (%d)", strerror(-err), -err);
126 return err;
127 }
128
129 ALOGD("set up nativeWindow %p for %dx%d, color %#x, rotation %d, usage %#x",
130 nativeWindow, width, height, format, rotation, finalUsage);
131 return NO_ERROR;
132 }
133
setNativeWindowHdrMetadata(ANativeWindow * nativeWindow,HDRStaticInfo * info)134 void setNativeWindowHdrMetadata(ANativeWindow *nativeWindow, HDRStaticInfo *info) {
135 // If mastering max and min luminance fields are 0, do not use them.
136 // It indicates the value may not be present in the stream.
137 if ((float)info->sType1.mMaxDisplayLuminance > 0.0f &&
138 (info->sType1.mMinDisplayLuminance * 0.0001f) > 0.0f) {
139 struct android_smpte2086_metadata smpte2086_meta = {
140 .displayPrimaryRed = {
141 info->sType1.mR.x * 0.00002f,
142 info->sType1.mR.y * 0.00002f
143 },
144 .displayPrimaryGreen = {
145 info->sType1.mG.x * 0.00002f,
146 info->sType1.mG.y * 0.00002f
147 },
148 .displayPrimaryBlue = {
149 info->sType1.mB.x * 0.00002f,
150 info->sType1.mB.y * 0.00002f
151 },
152 .whitePoint = {
153 info->sType1.mW.x * 0.00002f,
154 info->sType1.mW.y * 0.00002f
155 },
156 .maxLuminance = (float) info->sType1.mMaxDisplayLuminance,
157 .minLuminance = info->sType1.mMinDisplayLuminance * 0.0001f
158 };
159
160 int err = native_window_set_buffers_smpte2086_metadata(nativeWindow, &smpte2086_meta);
161 ALOGW_IF(err != 0, "failed to set smpte2086 metadata on surface (%d)", err);
162 }
163
164 // If the content light level fields are 0, do not use them, it
165 // indicates the value may not be present in the stream.
166 if ((float)info->sType1.mMaxContentLightLevel > 0.0f &&
167 (float)info->sType1.mMaxFrameAverageLightLevel > 0.0f) {
168 struct android_cta861_3_metadata cta861_meta = {
169 .maxContentLightLevel = (float) info->sType1.mMaxContentLightLevel,
170 .maxFrameAverageLightLevel = (float) info->sType1.mMaxFrameAverageLightLevel
171 };
172
173 int err = native_window_set_buffers_cta861_3_metadata(nativeWindow, &cta861_meta);
174 ALOGW_IF(err != 0, "failed to set cta861_3 metadata on surface (%d)", err);
175 }
176 }
177
setNativeWindowRotation(ANativeWindow * nativeWindow,int rotation)178 status_t setNativeWindowRotation(
179 ANativeWindow *nativeWindow /* nonnull */, int rotation) {
180
181 int transform = 0;
182 if ((rotation % 90) == 0) {
183 switch ((rotation / 90) & 3) {
184 case 1: transform = HAL_TRANSFORM_ROT_90; break;
185 case 2: transform = HAL_TRANSFORM_ROT_180; break;
186 case 3: transform = HAL_TRANSFORM_ROT_270; break;
187 default: transform = 0; break;
188 }
189 }
190
191 return native_window_set_buffers_transform(nativeWindow, transform);
192 }
193
pushBlankBuffersToNativeWindow(ANativeWindow * nativeWindow)194 status_t pushBlankBuffersToNativeWindow(ANativeWindow *nativeWindow /* nonnull */) {
195 status_t err = NO_ERROR;
196 ANativeWindowBuffer* anb = NULL;
197 int numBufs = 0;
198 int minUndequeuedBufs = 0;
199
200 // We need to reconnect to the ANativeWindow as a CPU client to ensure that
201 // no frames get dropped by SurfaceFlinger assuming that these are video
202 // frames.
203 err = nativeWindowDisconnect(nativeWindow, "pushBlankBuffersToNativeWindow");
204 if (err != NO_ERROR) {
205 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)", strerror(-err), -err);
206 return err;
207 }
208
209 err = native_window_api_connect(nativeWindow, NATIVE_WINDOW_API_CPU);
210 if (err != NO_ERROR) {
211 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", strerror(-err), -err);
212 (void)nativeWindowConnect(nativeWindow, "pushBlankBuffersToNativeWindow(err)");
213 return err;
214 }
215
216 err = setNativeWindowSizeFormatAndUsage(
217 nativeWindow, 1, 1, HAL_PIXEL_FORMAT_RGBX_8888, 0, GRALLOC_USAGE_SW_WRITE_OFTEN,
218 false /* reconnect */);
219 if (err != NO_ERROR) {
220 goto error;
221 }
222
223 static_cast<Surface*>(nativeWindow)->getIGraphicBufferProducer()->allowAllocation(true);
224
225 err = nativeWindow->query(nativeWindow,
226 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs);
227 if (err != NO_ERROR) {
228 ALOGE("error pushing blank frames: MIN_UNDEQUEUED_BUFFERS query "
229 "failed: %s (%d)", strerror(-err), -err);
230 goto error;
231 }
232
233 numBufs = minUndequeuedBufs + 1;
234 err = native_window_set_buffer_count(nativeWindow, numBufs);
235 if (err != NO_ERROR) {
236 ALOGE("error pushing blank frames: set_buffer_count failed: %s (%d)", strerror(-err), -err);
237 goto error;
238 }
239
240 // We push numBufs + 1 buffers to ensure that we've drawn into the same
241 // buffer twice. This should guarantee that the buffer has been displayed
242 // on the screen and then been replaced, so an previous video frames are
243 // guaranteed NOT to be currently displayed.
244 for (int i = 0; i < numBufs + 1; i++) {
245 err = native_window_dequeue_buffer_and_wait(nativeWindow, &anb);
246 if (err != NO_ERROR) {
247 ALOGE("error pushing blank frames: dequeueBuffer failed: %s (%d)",
248 strerror(-err), -err);
249 break;
250 }
251
252 sp<GraphicBuffer> buf(GraphicBuffer::from(anb));
253
254 // Fill the buffer with the a 1x1 checkerboard pattern ;)
255 uint32_t *img = NULL;
256 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
257 if (err != NO_ERROR) {
258 ALOGE("error pushing blank frames: lock failed: %s (%d)", strerror(-err), -err);
259 break;
260 }
261
262 *img = 0;
263
264 err = buf->unlock();
265 if (err != NO_ERROR) {
266 ALOGE("error pushing blank frames: unlock failed: %s (%d)", strerror(-err), -err);
267 break;
268 }
269
270 err = nativeWindow->queueBuffer(nativeWindow, buf->getNativeBuffer(), -1);
271 if (err != NO_ERROR) {
272 ALOGE("error pushing blank frames: queueBuffer failed: %s (%d)", strerror(-err), -err);
273 break;
274 }
275
276 anb = NULL;
277 }
278
279 error:
280
281 if (anb != NULL) {
282 nativeWindow->cancelBuffer(nativeWindow, anb, -1);
283 anb = NULL;
284 }
285
286 // Clean up after success or error.
287 status_t err2 = native_window_api_disconnect(nativeWindow, NATIVE_WINDOW_API_CPU);
288 if (err2 != NO_ERROR) {
289 ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)", strerror(-err2), -err2);
290 if (err == NO_ERROR) {
291 err = err2;
292 }
293 }
294
295 err2 = nativeWindowConnect(nativeWindow, "pushBlankBuffersToNativeWindow(err2)");
296 if (err2 != NO_ERROR) {
297 ALOGE("error pushing blank frames: api_connect failed: %s (%d)", strerror(-err), -err);
298 if (err == NO_ERROR) {
299 err = err2;
300 }
301 }
302
303 return err;
304 }
305
nativeWindowConnect(ANativeWindow * surface,const char * reason)306 status_t nativeWindowConnect(ANativeWindow *surface, const char *reason) {
307 ALOGD("connecting to surface %p, reason %s", surface, reason);
308
309 status_t err = native_window_api_connect(surface, NATIVE_WINDOW_API_MEDIA);
310 ALOGE_IF(err != OK, "Failed to connect to surface %p, err %d", surface, err);
311
312 return err;
313 }
314
nativeWindowDisconnect(ANativeWindow * surface,const char * reason)315 status_t nativeWindowDisconnect(ANativeWindow *surface, const char *reason) {
316 ALOGD("disconnecting from surface %p, reason %s", surface, reason);
317
318 status_t err = native_window_api_disconnect(surface, NATIVE_WINDOW_API_MEDIA);
319 ALOGE_IF(err != OK, "Failed to disconnect from surface %p, err %d", surface, err);
320
321 return err;
322 }
323
disableLegacyBufferDropPostQ(const sp<Surface> & surface)324 status_t disableLegacyBufferDropPostQ(const sp<Surface> &surface) {
325 sp<IGraphicBufferProducer> igbp =
326 surface ? surface->getIGraphicBufferProducer() : nullptr;
327 if (igbp) {
328 int targetSdk = android_get_application_target_sdk_version();
329 // When the caller is not an app (e.g. MediaPlayer in mediaserver)
330 // targetSdk is __ANDROID_API_FUTURE__.
331 bool drop =
332 targetSdk < __ANDROID_API_Q__ ||
333 targetSdk == __ANDROID_API_FUTURE__;
334 if (!drop) {
335 status_t err = igbp->setLegacyBufferDrop(false);
336 if (err == NO_ERROR) {
337 ALOGD("legacy buffer drop disabled: target sdk (%d)",
338 targetSdk);
339 } else {
340 ALOGD("disabling legacy buffer drop failed: %d", err);
341 }
342 }
343 }
344 return NO_ERROR;
345 }
346 } // namespace android
347
348