xref: /aosp_15_r20/external/skia/example/external_client/src/graphite_native_vulkan.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSurface.h"
16 #include "include/gpu/graphite/Context.h"
17 #include "include/gpu/graphite/Recorder.h"
18 #include "include/gpu/graphite/ContextOptions.h"
19 #include "include/gpu/vk/VulkanBackendContext.h"
20 #include "include/gpu/graphite/Surface.h"
21 #include "include/gpu/vk/VulkanExtensions.h"
22 #include "include/gpu/graphite/vk/VulkanGraphiteUtils.h"
23 
24 #define WIDTH 200
25 #define HEIGHT 400
26 
main(int argc,char ** argv)27 int main(int argc, char** argv) {
28     // This will not run (since it's missing all the Vulkan setup code),
29     // but it should compile and link to test the build system.
30     skgpu::VulkanBackendContext backendContext;
31 
32     std::unique_ptr<skgpu::VulkanExtensions> extensions(new skgpu::VulkanExtensions());
33     backendContext.fInstance = VK_NULL_HANDLE;
34     backendContext.fDevice = VK_NULL_HANDLE;
35     skgpu::graphite::ContextOptions options;
36 
37     // This call will fail if run, due to the context not being set up.
38     std::unique_ptr<skgpu::graphite::Context> context =
39         skgpu::graphite::ContextFactory::MakeVulkan(backendContext, options);
40     if (!context) {
41         printf("Could not make Graphite Native Vulkan context\n");
42         return 1;
43     }
44     printf("Context made, now to make the surface\n");
45 
46     SkImageInfo imageInfo =
47             SkImageInfo::Make(WIDTH, HEIGHT, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
48 
49     std::unique_ptr<skgpu::graphite::Recorder> recorder = context->makeRecorder();
50     if (!recorder) {
51         printf("Could not make recorder\n");
52         return 1;
53     }
54     sk_sp<SkSurface> surface =
55             SkSurfaces::RenderTarget(recorder.get(), imageInfo);
56     if (!surface) {
57         printf("Could not make surface from Metal Recorder\n");
58         return 1;
59     }
60 
61     SkCanvas* canvas = surface->getCanvas();
62     canvas->clear(SK_ColorLTGRAY);
63     SkRRect rrect = SkRRect::MakeRectXY(SkRect::MakeLTRB(10, 20, 50, 70), 10, 10);
64 
65     SkPaint paint;
66     paint.setColor(SK_ColorBLUE);
67     paint.setAntiAlias(true);
68 
69     canvas->drawRRect(rrect, paint);
70 
71     // There would need to be vulkan cleanup here.
72     return 0;
73 }
74