Debian Codegraph @

libs/hwui/renderthread/VulkanSurface.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 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 "VulkanSurface.h"
18
19 #include <GrDirectContext.h>
20 #include <SkSurface.h>
21 #include <algorithm>
22
23 #include <gui/TraceUtils.h>
24 #include "VulkanManager.h"
25 #include "utils/Color.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "VulkanSurface"
29
30 namespace android {
31 namespace uirenderer {
32 namespace renderthread {
33
34 static int InvertTransform(int transform) {
35 switch (transform) {
36 case ANATIVEWINDOW_TRANSFORM_ROTATE_90:
37 return ANATIVEWINDOW_TRANSFORM_ROTATE_270;
38 case ANATIVEWINDOW_TRANSFORM_ROTATE_180:
39 return ANATIVEWINDOW_TRANSFORM_ROTATE_180;
40 case ANATIVEWINDOW_TRANSFORM_ROTATE_270:
41 return ANATIVEWINDOW_TRANSFORM_ROTATE_90;
42 default:
43 return 0;
44 }
45 }
46
47 static SkMatrix GetPreTransformMatrix(SkISize windowSize, int transform) {
48 const int width = windowSize.width();
49 const int height = windowSize.height();
50
51 switch (transform) {
52 case 0:
53 return SkMatrix::I();
54 case ANATIVEWINDOW_TRANSFORM_ROTATE_90:
55 return SkMatrix::MakeAll(0, -1, height, 1, 0, 0, 0, 0, 1);
56 case ANATIVEWINDOW_TRANSFORM_ROTATE_180:
57 return SkMatrix::MakeAll(-1, 0, width, 0, -1, height, 0, 0, 1);
58 case ANATIVEWINDOW_TRANSFORM_ROTATE_270:
59 return SkMatrix::MakeAll(0, 1, 0, -1, 0, width, 0, 0, 1);
60 default:
61 LOG_ALWAYS_FATAL("Unsupported Window Transform (%d)", transform);
62 }
63 return SkMatrix::I();
64 }
65
66 static bool ConnectAndSetWindowDefaults(ANativeWindow* window) {
67 ATRACE_CALL();
68
69 int err = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
70 if (err != 0) {
71 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-err), err);
72 return false;
73 }
74
75 // this will match what we do on GL so pick that here.
76 err = window->setSwapInterval(window, 1);
77 if (err != 0) {
78 ALOGE("native_window->setSwapInterval(1) failed: %s (%d)", strerror(-err), err);
79 return false;
80 }
81
82 err = native_window_set_shared_buffer_mode(window, false);
83 if (err != 0) {
84 ALOGE("native_window_set_shared_buffer_mode(false) failed: %s (%d)", strerror(-err), err);
85 return false;
86 }
87
88 err = native_window_set_auto_refresh(window, false);
89 if (err != 0) {
90 ALOGE("native_window_set_auto_refresh(false) failed: %s (%d)", strerror(-err), err);
91 return false;
92 }
93
94 err = native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_FREEZE);
95 if (err != 0) {
96 ALOGE("native_window_set_scaling_mode(NATIVE_WINDOW_SCALING_MODE_FREEZE) failed: %s (%d)",
97 strerror(-err), err);
98 return false;
99 }
100
101 // Let consumer drive the size of the buffers.
102 err = native_window_set_buffers_dimensions(window, 0, 0);
103 if (err != 0) {
104 ALOGE("native_window_set_buffers_dimensions(0,0) failed: %s (%d)", strerror(-err), err);
105 return false;
106 }
107
108 // Enable auto prerotation, so when buffer size is driven by the consumer
109 // and the transform hint specifies a 90 or 270 degree rotation, the width
110 // and height used for buffer pre-allocation and dequeueBuffer will be
111 // additionally swapped.
112 err = native_window_set_auto_prerotation(window, true);
113 if (err != 0) {
114 ALOGE("VulkanSurface::UpdateWindow() native_window_set_auto_prerotation failed: %s (%d)",
115 strerror(-err), err);
116 return false;
117 }
118
119 return true;
120 }
121
122 VulkanSurface* VulkanSurface::Create(ANativeWindow* window, ColorMode colorMode,
123 SkColorType colorType, sk_sp<SkColorSpace> colorSpace,
124 GrDirectContext* grContext, const VulkanManager& vkManager,
125 uint32_t extraBuffers) {
126 // Connect and set native window to default configurations.
127 if (!ConnectAndSetWindowDefaults(window)) {
128 return nullptr;
129 }
130
131 // Initialize WindowInfo struct.
132 WindowInfo windowInfo;
133 if (!InitializeWindowInfoStruct(window, colorMode, colorType, colorSpace, vkManager,
134 extraBuffers, &windowInfo)) {
135 return nullptr;
136 }
137
138 // Now we attempt to modify the window.
139 if (!UpdateWindow(window, windowInfo)) {
140 return nullptr;
141 }
142
143 return new VulkanSurface(window, windowInfo, grContext);
144 }
145
146 bool VulkanSurface::InitializeWindowInfoStruct(ANativeWindow* window, ColorMode colorMode,
147 SkColorType colorType,
148 sk_sp<SkColorSpace> colorSpace,
149 const VulkanManager& vkManager,
150 uint32_t extraBuffers, WindowInfo* outWindowInfo) {
151 ATRACE_CALL();
152
153 int width, height;
154 int err = window->query(window, NATIVE_WINDOW_DEFAULT_WIDTH, &width);
155 if (err != 0 || width < 0) {
156 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, width);
157 return false;
158 }
159 err = window->query(window, NATIVE_WINDOW_DEFAULT_HEIGHT, &height);
160 if (err != 0 || height < 0) {
161 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, height);
162 return false;
163 }
164 outWindowInfo->size = SkISize::Make(width, height);
165
166 int query_value;
167 err = window->query(window, NATIVE_WINDOW_TRANSFORM_HINT, &query_value);
168 if (err != 0 || query_value < 0) {
169 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
170 return false;
171 }
172 outWindowInfo->transform = query_value;
173
174 outWindowInfo->actualSize = outWindowInfo->size;
175 if (outWindowInfo->transform & ANATIVEWINDOW_TRANSFORM_ROTATE_90) {
176 outWindowInfo->actualSize.set(outWindowInfo->size.height(), outWindowInfo->size.width());
177 }
178
179 outWindowInfo->preTransform =
180 GetPreTransformMatrix(outWindowInfo->size, outWindowInfo->transform);
181
182 err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
183 if (err != 0 || query_value < 0) {
184 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
185 return false;
186 }
187 outWindowInfo->bufferCount =
188 static_cast<uint32_t>(query_value) + sTargetBufferCount + extraBuffers;
189
190 err = window->query(window, NATIVE_WINDOW_MAX_BUFFER_COUNT, &query_value);
191 if (err != 0 || query_value < 0) {
192 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
193 return false;
194 }
195 if (outWindowInfo->bufferCount > static_cast<uint32_t>(query_value)) {
196 // Application must settle for fewer images than desired:
197 outWindowInfo->bufferCount = static_cast<uint32_t>(query_value);
198 }
199
200 outWindowInfo->bufferFormat = ColorTypeToBufferFormat(colorType);
201 outWindowInfo->colorspace = colorSpace;
202 outWindowInfo->dataspace = ColorSpaceToADataSpace(colorSpace.get(), colorType);
203 LOG_ALWAYS_FATAL_IF(
204 outWindowInfo->dataspace == HAL_DATASPACE_UNKNOWN && colorType != kAlpha_8_SkColorType,
205 "Unsupported colorspace");
206
207 VkFormat vkPixelFormat;
208 switch (colorType) {
209 case kRGBA_8888_SkColorType:
210 vkPixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
211 break;
212 case kRGBA_F16_SkColorType:
213 vkPixelFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
214 break;
215 case kRGBA_1010102_SkColorType:
216 vkPixelFormat = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
217 break;
218 case kAlpha_8_SkColorType:
219 vkPixelFormat = VK_FORMAT_R8_UNORM;
220 break;
221 default:
222 LOG_ALWAYS_FATAL("Unsupported colorType: %d", (int)colorType);
223 }
224
225 LOG_ALWAYS_FATAL_IF(nullptr == vkManager.mGetPhysicalDeviceImageFormatProperties2,
226 "vkGetPhysicalDeviceImageFormatProperties2 is missing");
227 VkPhysicalDeviceExternalImageFormatInfo externalImageFormatInfo;
228 externalImageFormatInfo.sType =
229 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO;
230 externalImageFormatInfo.pNext = nullptr;
231 externalImageFormatInfo.handleType =
232 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
233
234 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo;
235 imageFormatInfo.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
236 imageFormatInfo.pNext = &externalImageFormatInfo;
237 imageFormatInfo.format = vkPixelFormat;
238 imageFormatInfo.type = VK_IMAGE_TYPE_2D;
239 imageFormatInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
240 // Currently Skia requires the images to be color attachments and support all transfer
241 // operations.
242 imageFormatInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
243 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
244 imageFormatInfo.flags = 0;
245
246 VkAndroidHardwareBufferUsageANDROID hwbUsage;
247 hwbUsage.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID;
248 hwbUsage.pNext = nullptr;
249
250 VkImageFormatProperties2 imgFormProps;
251 imgFormProps.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
252 imgFormProps.pNext = &hwbUsage;
253
254 VkResult res = vkManager.mGetPhysicalDeviceImageFormatProperties2(
255 vkManager.mPhysicalDevice, &imageFormatInfo, &imgFormProps);
256 if (VK_SUCCESS != res) {
257 ALOGE("Failed to query GetPhysicalDeviceImageFormatProperties2");
258 return false;
259 }
260
261 uint64_t consumerUsage;
262 err = native_window_get_consumer_usage(window, &consumerUsage);
263 if (err != 0) {
264 ALOGE("native_window_get_consumer_usage failed: %s (%d)", strerror(-err), err);
265 return false;
266 }
267 outWindowInfo->windowUsageFlags = consumerUsage | hwbUsage.androidHardwareBufferUsage;
268
269 return true;
270 }
271
272 bool VulkanSurface::UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo) {
273 ATRACE_CALL();
274
275 int err = native_window_set_buffers_format(window, windowInfo.bufferFormat);
276 if (err != 0) {
277 ALOGE("VulkanSurface::UpdateWindow() native_window_set_buffers_format(%d) failed: %s (%d)",
278 windowInfo.bufferFormat, strerror(-err), err);
279 return false;
280 }
281
282 err = native_window_set_buffers_data_space(window, windowInfo.dataspace);
283 if (err != 0) {
284 ALOGE("VulkanSurface::UpdateWindow() native_window_set_buffers_data_space(%d) "
285 "failed: %s (%d)",
286 windowInfo.dataspace, strerror(-err), err);
287 return false;
288 }
289
290 // native_window_set_buffers_transform() expects the transform the app is requesting that
291 // the compositor perform during composition. With native windows, pre-transform works by
292 // rendering with the same transform the compositor is applying (as in Vulkan), but
293 // then requesting the inverse transform, so that when the compositor does
294 // it's job the two transforms cancel each other out and the compositor ends
295 // up applying an identity transform to the app's buffer.
296 err = native_window_set_buffers_transform(window, InvertTransform(windowInfo.transform));
297 if (err != 0) {
298 ALOGE("VulkanSurface::UpdateWindow() native_window_set_buffers_transform(%d) "
299 "failed: %s (%d)",
300 windowInfo.transform, strerror(-err), err);
301 return false;
302 }
303
304 err = native_window_set_buffer_count(window, windowInfo.bufferCount);
305 if (err != 0) {
306 ALOGE("VulkanSurface::UpdateWindow() native_window_set_buffer_count(%zu) failed: %s (%d)",
307 windowInfo.bufferCount, strerror(-err), err);
308 return false;
309 }
310
311 err = native_window_set_usage(window, windowInfo.windowUsageFlags);
312 if (err != 0) {
313 ALOGE("VulkanSurface::UpdateWindow() native_window_set_usage failed: %s (%d)",
314 strerror(-err), err);
315 return false;
316 }
317
318 return true;
319 }
320
321 VulkanSurface::VulkanSurface(ANativeWindow* window, const WindowInfo& windowInfo,
322 GrDirectContext* grContext)
323 : mNativeWindow(window), mWindowInfo(windowInfo), mGrContext(grContext) {}
324
325 VulkanSurface::~VulkanSurface() {
326 releaseBuffers();
327
328 // release the native window to be available for use by other clients
329 int err = native_window_api_disconnect(mNativeWindow.get(), NATIVE_WINDOW_API_EGL);
330 ALOGW_IF(err != 0, "native_window_api_disconnect failed: %s (%d)", strerror(-err), err);
331 }
332
333 void VulkanSurface::releaseBuffers() {
334 for (uint32_t i = 0; i < mWindowInfo.bufferCount; i++) {
335 VulkanSurface::NativeBufferInfo& bufferInfo = mNativeBuffers[i];
336
337 if (bufferInfo.buffer.get() != nullptr && bufferInfo.dequeued) {
338 int err = mNativeWindow->cancelBuffer(mNativeWindow.get(), bufferInfo.buffer.get(),
339 bufferInfo.dequeue_fence.release());
340 if (err != 0) {
341 ALOGE("cancelBuffer[%u] failed during destroy: %s (%d)", i, strerror(-err), err);
342 }
343 bufferInfo.dequeued = false;
344 bufferInfo.dequeue_fence.reset();
345 }
346
347 LOG_ALWAYS_FATAL_IF(bufferInfo.dequeued);
348 LOG_ALWAYS_FATAL_IF(bufferInfo.dequeue_fence.ok());
349
350 bufferInfo.skSurface.reset();
351 bufferInfo.buffer.clear();
352 bufferInfo.hasValidContents = false;
353 bufferInfo.lastPresentedCount = 0;
354 }
355 }
356
357 VulkanSurface::NativeBufferInfo* VulkanSurface::dequeueNativeBuffer() {
358 // Set the mCurrentBufferInfo to invalid in case of error and only reset it to the correct
359 // value at the end of the function if everything dequeued correctly.
360 mCurrentBufferInfo = nullptr;
361
362 // Query the transform hint synced from the initial Surface connect or last queueBuffer. The
363 // auto prerotation on the buffer is based on the same transform hint in use by the producer.
364 int transformHint = 0;
365 int err =
366 mNativeWindow->query(mNativeWindow.get(), NATIVE_WINDOW_TRANSFORM_HINT, &transformHint);
367
368 // Since auto pre-rotation is enabled, dequeueBuffer to get the consumer driven buffer size
369 // from ANativeWindowBuffer.
370 ANativeWindowBuffer* buffer;
371 base::unique_fd fence_fd;
372 {
373 int rawFd = -1;
374 err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buffer, &rawFd);
375 fence_fd.reset(rawFd);
376 }
377 if (err != 0) {
378 ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), err);
379 return nullptr;
380 }
381
382 SkISize actualSize = SkISize::Make(buffer->width, buffer->height);
383 if (actualSize != mWindowInfo.actualSize || transformHint != mWindowInfo.transform) {
384 if (actualSize != mWindowInfo.actualSize) {
385 // reset the NativeBufferInfo (including SkSurface) associated with the old buffers. The
386 // new NativeBufferInfo storage will be populated lazily as we dequeue each new buffer.
387 mWindowInfo.actualSize = actualSize;
388 releaseBuffers();
389 }
390
391 if (transformHint != mWindowInfo.transform) {
392 err = native_window_set_buffers_transform(mNativeWindow.get(),
393 InvertTransform(transformHint));
394 if (err != 0) {
395 ALOGE("native_window_set_buffers_transform(%d) failed: %s (%d)", transformHint,
396 strerror(-err), err);
397 mNativeWindow->cancelBuffer(mNativeWindow.get(), buffer, fence_fd.release());
398 return nullptr;
399 }
400 mWindowInfo.transform = transformHint;
401 }
402
403 mWindowInfo.size = actualSize;
404 if (mWindowInfo.transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
405 mWindowInfo.size.set(actualSize.height(), actualSize.width());
406 }
407
408 mWindowInfo.preTransform = GetPreTransformMatrix(mWindowInfo.size, mWindowInfo.transform);
409 }
410
411 uint32_t idx;
412 for (idx = 0; idx < mWindowInfo.bufferCount; idx++) {
413 if (mNativeBuffers[idx].buffer.get() == buffer) {
414 mNativeBuffers[idx].dequeued = true;
415 mNativeBuffers[idx].dequeue_fence = std::move(fence_fd);
416 break;
417 } else if (mNativeBuffers[idx].buffer.get() == nullptr) {
418 // increasing the number of buffers we have allocated
419 mNativeBuffers[idx].buffer = buffer;
420 mNativeBuffers[idx].dequeued = true;
421 mNativeBuffers[idx].dequeue_fence = std::move(fence_fd);
422 break;
423 }
424 }
425 if (idx == mWindowInfo.bufferCount) {
426 ALOGE("dequeueBuffer returned unrecognized buffer");
427 mNativeWindow->cancelBuffer(mNativeWindow.get(), buffer, fence_fd.release());
428 return nullptr;
429 }
430
431 VulkanSurface::NativeBufferInfo* bufferInfo = &mNativeBuffers[idx];
432
433 if (bufferInfo->skSurface.get() == nullptr) {
434 bufferInfo->skSurface = SkSurface::MakeFromAHardwareBuffer(
435 mGrContext, ANativeWindowBuffer_getHardwareBuffer(bufferInfo->buffer.get()),
436 kTopLeft_GrSurfaceOrigin, mWindowInfo.colorspace, nullptr, /*from_window=*/true);
437 if (bufferInfo->skSurface.get() == nullptr) {
438 ALOGE("SkSurface::MakeFromAHardwareBuffer failed");
439 mNativeWindow->cancelBuffer(mNativeWindow.get(), buffer,
440 mNativeBuffers[idx].dequeue_fence.release());
441 mNativeBuffers[idx].dequeued = false;
442 return nullptr;
443 }
444 }
445
446 mCurrentBufferInfo = bufferInfo;
447 return bufferInfo;
448 }
449
450 bool VulkanSurface::presentCurrentBuffer(const SkRect& dirtyRect, int semaphoreFd) {
451 if (!dirtyRect.isEmpty()) {
452
453 // native_window_set_surface_damage takes a rectangle in prerotated space
454 // with a bottom-left origin. That is, top > bottom.
455 // The dirtyRect is also in prerotated space, so we just need to switch it to
456 // a bottom-left origin space.
457
458 SkIRect irect;
459 dirtyRect.roundOut(&irect);
460 android_native_rect_t aRect;
461 aRect.left = irect.left();
462 aRect.top = logicalHeight() - irect.top();
463 aRect.right = irect.right();
464 aRect.bottom = logicalHeight() - irect.bottom();
465
466 int err = native_window_set_surface_damage(mNativeWindow.get(), &aRect, 1);
467 ALOGE_IF(err != 0, "native_window_set_surface_damage failed: %s (%d)", strerror(-err), err);
468 }
469
470 LOG_ALWAYS_FATAL_IF(!mCurrentBufferInfo);
471 VulkanSurface::NativeBufferInfo& currentBuffer = *mCurrentBufferInfo;
472 // queueBuffer always closes fence, even on error
473 int queuedFd = (semaphoreFd != -1) ? semaphoreFd : currentBuffer.dequeue_fence.release();
474 int err = mNativeWindow->queueBuffer(mNativeWindow.get(), currentBuffer.buffer.get(), queuedFd);
475
476 currentBuffer.dequeued = false;
477 if (err != 0) {
478 ALOGE("queueBuffer failed: %s (%d)", strerror(-err), err);
479 // cancelBuffer takes ownership of the fence
480 mNativeWindow->cancelBuffer(mNativeWindow.get(), currentBuffer.buffer.get(),
481 currentBuffer.dequeue_fence.release());
482 } else {
483 currentBuffer.hasValidContents = true;
484 currentBuffer.lastPresentedCount = mPresentCount;
485 mPresentCount++;
486 }
487
488 currentBuffer.dequeue_fence.reset();
489
490 return err == 0;
491 }
492
493 int VulkanSurface::getCurrentBuffersAge() {
494 LOG_ALWAYS_FATAL_IF(!mCurrentBufferInfo);
495 VulkanSurface::NativeBufferInfo& currentBuffer = *mCurrentBufferInfo;
496 return currentBuffer.hasValidContents ? (mPresentCount - currentBuffer.lastPresentedCount) : 0;
497 }
498
499 } /* namespace renderthread */
500 } /* namespace uirenderer */
501 } /* namespace android */