1cb93a386Sopenharmony_ci
2cb93a386Sopenharmony_ci---
3cb93a386Sopenharmony_cititle: "Vulkan"
4cb93a386Sopenharmony_cilinkTitle: "Vulkan"
5cb93a386Sopenharmony_ci
6cb93a386Sopenharmony_ci---
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ciSkia has a Vulkan implementation of its GPU backend. The Vulkan backend can be
10cb93a386Sopenharmony_cibuilt alongside the OpenGL backend. The client can select between the OpenGL
11cb93a386Sopenharmony_ciand Vulkan implementation at runtime. The Vulkan backend has reached feature
12cb93a386Sopenharmony_ciparity with the OpenGL backend. At this time we find that many Vulkan drivers
13cb93a386Sopenharmony_cihave bugs that Skia triggers for which we have no workaround. We are reporting
14cb93a386Sopenharmony_cibugs to vendors as we find them.
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ciWindows and Linux
17cb93a386Sopenharmony_ci-----------------
18cb93a386Sopenharmony_ciTo build the Vulkan backend, set `skia_use_vulkan=true` in `args.gn`.
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ciAndroid
21cb93a386Sopenharmony_ci-------
22cb93a386Sopenharmony_ciThe Vulkan backend can run on any device with Vulkan drivers, including all Android N+ devices.
23cb93a386Sopenharmony_ciTo build the Vulkan backend, set `ndk_api = 24` in `args.gn` to target Android N.
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ciUsing the Vulkan Backend
26cb93a386Sopenharmony_ci------------------------
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ciTo create a GrContext that is backed by Vulkan the client creates a Vulkan device and queue, initializes a GrVkBackendContext to describe the context, and then calls GrContext::MakeVulkan:
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci<!--?prettify lang=c++?-->
31cb93a386Sopenharmony_ci    sk_sp<GrVkBackendContext> vkContext = new GrVkBackendContext;
32cb93a386Sopenharmony_ci    vkBackendContext.fInstance = vkInstance;
33cb93a386Sopenharmony_ci    vkBackendContext.fPhysicalDevice = vkPhysDevice;
34cb93a386Sopenharmony_ci    ...
35cb93a386Sopenharmony_ci    vkBackendContext.fInterface.reset(GrVkCreateInterface(instance, vkPhysDevice, extensionFlags);
36cb93a386Sopenharmony_ci    ...
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    sk_sp<GrContext> context = GrContext::MakeVulkan(vkBackendContext);
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ciWhen using the Vulkan backend, GrVkImageInfo is used to construct GrBackendTexture
41cb93a386Sopenharmony_ciand GrBackendRenderTarget objects that in turn are used to create SkSurface and SkImage
42cb93a386Sopenharmony_ciobjects that refer to VkImages created by the Skia client.
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ciThe GrBackendObject returned by SkImage::getTextureHandle(),
45cb93a386Sopenharmony_ciSkSurface::getTextureHandle(), and SkSurface::getRenderTargetHandle() should be
46cb93a386Sopenharmony_ciinterpreted as a GrVkImageInfo*. This allows a client to get the backing VkImage
47cb93a386Sopenharmony_ciof a SkImage or SkSurface.
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ciGrVkImageInfo specifies a VkImage and associated state (tiling, layout, format, etc).
50cb93a386Sopenharmony_ciAfter getting a GrVkImageInfo* via getTextureHandle() or
51cb93a386Sopenharmony_cigetRenderTargetHandle(), the client should check the fImageLayout field to know
52cb93a386Sopenharmony_ciwhat layout Skia left the VkImage in before using the VkImage. If the client
53cb93a386Sopenharmony_cichanges the layout of the VkImage,
54cb93a386Sopenharmony_ciGrVkImageInfo::updateImageLayout(VkImageLayout layout) should be called before
55cb93a386Sopenharmony_ciresuming Skia rendering.
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ciThe client is responsible for any synchronization or barriers needed before
58cb93a386Sopenharmony_ciSkia performs I/O on a VkImage imported into Skia via GrVkImageInfo.  Skia will
59cb93a386Sopenharmony_ciassume it can start issuing commands referencing the VkImage without the need
60cb93a386Sopenharmony_cifor additional synchronization.
61cb93a386Sopenharmony_ci
62