Debian Codegraph @

example/external_client/src/graphite_metal_capture.cpp

raw
1 /*
2 * Copyright 2025 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 <cstdio>
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkRRect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkStream.h"
17 #include "include/core/SkSurface.h"
18 #include "include/encode/SkJpegEncoder.h"
19 #include "include/gpu/graphite/Context.h"
20 #include "include/gpu/graphite/ContextOptions.h"
21 #include "include/gpu/graphite/GraphiteTypes.h"
22 #include "include/gpu/graphite/Recorder.h"
23 #include "include/gpu/graphite/Surface.h"
24 #include "include/gpu/graphite/mtl/MtlBackendContext.h"
25 #include "include/gpu/graphite/mtl/MtlGraphiteUtils.h"
26
27 #include "graphite_metal_context_helper.h"
28 #include "src/capture/SkCapture.h"
29
30 #define WIDTH 200
31 #define HEIGHT 400
32
33 int main(int argc, char *argv[]) {
34
35 if (argc != 2) {
36 printf("Specify a single write location.\n");
37 return 1;
38 }
39
40 SkFILEWStream output(argv[1]);
41 if (!output.isValid()) {
42 printf("Cannot open output file %s\n", argv[1]);
43 return 1;
44 }
45
46 /* SET UP CONTEXT AND RECORDERS FOR DRAWING AND CAPTURE */
47
48 skgpu::graphite::MtlBackendContext backendContext = GetMetalContext();
49 skgpu::graphite::ContextOptions options;
50
51 options.fEnableCapture = true;
52
53 std::unique_ptr<skgpu::graphite::Context> context =
54 skgpu::graphite::ContextFactory::MakeMetal(backendContext, options);
55 if (!context) {
56 printf("Could not make Graphite Native Metal context\n");
57 return 1;
58 }
59 printf("Context made, now to make the surface\n");
60
61 SkImageInfo imageInfo =
62 SkImageInfo::Make(WIDTH, HEIGHT, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
63
64 std::unique_ptr<skgpu::graphite::Recorder> recorder = context->makeRecorder();
65 if (!recorder) {
66 printf("Could not make recorder\n");
67 return 1;
68 }
69 sk_sp<SkSurface> surface =
70 SkSurfaces::RenderTarget(recorder.get(), imageInfo);
71 if (!surface) {
72 printf("Could not make surface from Metal Recorder\n");
73 return 1;
74 }
75
76 sk_sp<SkSurface> surfaceB =
77 SkSurfaces::RenderTarget(recorder.get(), imageInfo);
78 if (!surfaceB) {
79 printf("Could not make second surface from Metal Recorder\n");
80 return 1;
81 }
82
83 /* DRAWING */
84 context->startCapture();
85 printf("Capture started, now to draw\n");
86
87 SkCanvas* canvas = surface->getCanvas();
88 SkCanvas* canvasB = surfaceB->getCanvas();
89
90 // Canvas A
91 canvas->clear(SK_ColorCYAN);
92 SkRRect rrect = SkRRect::MakeRectXY(SkRect::MakeLTRB(10, 20, 50, 70), 10, 10);
93
94 SkPaint paint;
95 paint.setColor(SK_ColorRED);
96 paint.setAntiAlias(true);
97
98 canvas->drawRRect(rrect, paint);
99
100 // Triggers a breakpoint in the capture
101 auto contentImg = surface->makeImageSnapshot();
102 canvas->drawCircle(50, 50, 30, paint);
103
104 // Canvas B
105 canvasB->clear(SK_ColorMAGENTA);
106 paint.setColor(SK_ColorBLACK);
107 canvasB->drawImage(contentImg, 0, 0);
108 canvasB->drawCircle(10, 10, 5, paint);
109
110 printf("ready to snap the GPU calls\n");
111 // Now to send the draws to the GPU
112 std::unique_ptr<skgpu::graphite::Recording> recording = recorder->snap();
113 if (!recording) {
114 printf("Could not create a recording\n");
115 return 1;
116 }
117 skgpu::graphite::InsertRecordingInfo info;
118 info.fRecording = recording.get();
119 if (!context->insertRecording(info)) {
120 printf("Context::insertRecording failed\n");
121 return 1;
122 }
123
124 sk_sp<SkImage> img;
125 auto callback = [](SkImage::ReadPixelsContext ctx,
126 std::unique_ptr<const SkImage::AsyncReadResult> result) {
127 if (result->count() != 1) {
128 printf("Didn't load exactly one plane\n");
129 return;
130 }
131 auto ii = SkImageInfo::Make(WIDTH, HEIGHT, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
132 sk_sp<SkImage>* output = reinterpret_cast<sk_sp<SkImage>*>(ctx);
133
134 SkPixmap pm(ii, result->data(0), result->rowBytes(0));
135 *output = SkImages::RasterFromPixmapCopy(pm);
136 };
137 printf("GPU operations modifying surface have been inserted in the command buffer,"
138 "scheduling pixel readback\n");
139 context->asyncRescaleAndReadPixels(surface.get(), imageInfo, SkIRect::MakeSize({WIDTH, HEIGHT}),
140 SkImage::RescaleGamma::kSrc,
141 SkImage::RescaleMode::kRepeatedCubic,
142 callback, &img);
143
144 printf("Submitting work to GPU and waiting for it to be done\n");
145 // Note this doesn't work on all backend types, e.g. Dawn.
146 context->submit(skgpu::graphite::SyncToCpu::kYes);
147 if (context->hasUnfinishedGpuWork()) {
148 printf("Sync with GPU completion failed\n");
149 return 1;
150 }
151
152 auto capture = context->endCapture();
153 auto serializedCapture = capture->serializeCapture();
154
155 /* WRITE DATA TO FILE LOCATION */
156 output.write(serializedCapture->data(), serializedCapture->size());
157 output.fsync();
158
159 /* DESERIALIZE CAPTURE AND INSPECT CONTENTS */
160 if (serializedCapture) {
161 auto deserializedCapture = SkCapture::MakeFromData(serializedCapture);
162 } else {
163 printf("No capture to inspect.");
164 return 1;
165 }
166
167 printf("done\n");
168 return 0;
169 }