vulkan_renderengine: Check for support first

The vulkan_renderengine flag allows us to switch some users to using
Vulkan as part of the trunk stable process. But not all devices support
Vulkan.

Move canSupportSkiaVkRenderEngine into the RenderEngine header, so it
can be used by SurfaceFlinger. When the vulkan_renderengine flag is set,
check for support before choosing Vulkan. Do *not* log an error, since
this is specified broadly, so it is expected that the flag will be set
for devices that do not have support.

This introduces an extra call to initVulkanInterface, so log the amount
of time it takes. On my device, the first call is only ~17ms, so this
seems fine. The next call is much faster, ~4ms. The next call is not
interestingly different, possibly because the first call was already
made.

Bug: 293371537
Bug: 325619183
Test: atest librenderengine_test
Test: manual
Change-Id: Iba66e67347c69b95dc9c05ca838fa1391ef4ab81
diff --git a/libs/renderengine/skia/SkiaVkRenderEngine.cpp b/libs/renderengine/skia/SkiaVkRenderEngine.cpp
index bff12ce..b43ab6c 100644
--- a/libs/renderengine/skia/SkiaVkRenderEngine.cpp
+++ b/libs/renderengine/skia/SkiaVkRenderEngine.cpp
@@ -287,6 +287,7 @@
     CHECK_NONNULL(vk##F)
 
 VulkanInterface initVulkanInterface(bool protectedContent = false) {
+    const nsecs_t timeBefore = systemTime();
     VulkanInterface interface;
 
     VK_GET_PROC(EnumerateInstanceVersion);
@@ -598,7 +599,9 @@
     interface.isProtected = protectedContent;
     // funcs already initialized
 
-    ALOGD("%s: Success init Vulkan interface", __func__);
+    const nsecs_t timeAfter = systemTime();
+    const float initTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
+    ALOGD("%s: Success init Vulkan interface in %f ms", __func__, initTimeMs);
     return interface;
 }
 
@@ -654,17 +657,25 @@
     }
 }
 
+bool RenderEngine::canSupport(GraphicsApi graphicsApi) {
+    switch (graphicsApi) {
+        case GraphicsApi::GL:
+            return true;
+        case GraphicsApi::VK: {
+            if (!sVulkanInterface.initialized) {
+                sVulkanInterface = initVulkanInterface(false /* no protected content */);
+                ALOGD("%s: initialized == %s.", __func__,
+                      sVulkanInterface.initialized ? "true" : "false");
+            }
+            return sVulkanInterface.initialized;
+        }
+    }
+}
+
 namespace skia {
 
 using base::StringAppendF;
 
-bool SkiaVkRenderEngine::canSupportSkiaVkRenderEngine() {
-    VulkanInterface temp = initVulkanInterface(false /* no protected content */);
-    ALOGD("SkiaVkRenderEngine::canSupportSkiaVkRenderEngine(): initialized == %s.",
-          temp.initialized ? "true" : "false");
-    return temp.initialized;
-}
-
 std::unique_ptr<SkiaVkRenderEngine> SkiaVkRenderEngine::create(
         const RenderEngineCreationArgs& args) {
     std::unique_ptr<SkiaVkRenderEngine> engine(new SkiaVkRenderEngine(args));