-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkiaSDLExample.cpp
More file actions
347 lines (300 loc) · 12 KB
/
Copy pathSkiaSDLExample.cpp
File metadata and controls
347 lines (300 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
*/
/*
* Known to work with SDL2 2.30.11 and mis-behave with sdl2 compat 2.32.54 (on SDL3 3.2.10). HTL 2025-04-29
*
* Changes from chrome/m92:example/SkiaSDLExample.cpp - HTL 2024:
* - Update to current skia (m132+)
* - Default typeface has no visible glyph from m120/m121 onwards; must be initialized
* - Fix element-positioning bug due to window resizing:
* - The (*bottom*-left corner of) text is drawn 100,100 from *top*-left
* - Gnome-Shell forces the SDL window to resize to fit, as it tries to expand to fill full-screen
* - Therefore element positioning was off by about 105 pixels, which means the text is invisible,
* and the rects are drawn by a (variable) vertical offset from the mouse clicks.
*
* The python / GLFW c++ version goes further cosmetically: the (*top*-left of) text is exactly at 0,0 *top*-left,
* and also centers the rotating star. This c++ code is quite inconsistent in drawing
* the star fixed relative to the *bottom*-left corner, and the (*bottom*-left of) text relative to the
* *top*-left.
*/
#include "include/gpu/ganesh/GrBackendSurface.h"
#include "include/gpu/ganesh/GrDirectContext.h"
#include "SDL.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkSurface.h"
#include "src/base/SkRandom.h"
#include "include/private/base/SkTArray.h"
#include "include/gpu/ganesh/gl/GrGLInterface.h"
#include "src/gpu/ganesh/gl/GrGLUtil.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/gpu/ganesh/gl/GrGLDirectContext.h"
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "include/core/SkColorSpace.h"
#if defined(SK_BUILD_FOR_ANDROID)
#include <GLES/gl.h>
#elif defined(SK_BUILD_FOR_UNIX)
#include <GL/gl.h>
#elif defined(SK_BUILD_FOR_MAC)
#include <OpenGL/gl.h>
#elif defined(SK_BUILD_FOR_IOS)
#include <OpenGLES/ES2/gl.h>
#endif
#include "include/core/SkFontMgr.h"
#ifdef __linux__
#include "include/ports/SkFontConfigInterface.h"
#include "include/ports/SkFontMgr_FontConfigInterface.h"
#endif
#ifdef __APPLE__
#include "include/ports/SkFontMgr_mac_ct.h"
#endif
/*
* This application is a simple example of how to combine SDL and Skia it demonstrates:
* how to setup gpu rendering to the main window
* how to perform cpu-side rendering and draw the result to the gpu-backed screen
* draw simple primitives (rectangles)
* draw more complex primitives (star)
*/
struct ApplicationState {
ApplicationState() : fQuit(false) {}
// Storage for the user created rectangles. The last one may still be being edited.
skia_private::TArray<SkRect> fRects;
int window_width;
int window_height;
bool fQuit;
};
static void handle_error() {
const char* error = SDL_GetError();
SkDebugf("SDL Error: %s\n", error);
SDL_ClearError();
}
static void handle_events(ApplicationState* state, SkCanvas* canvas) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEMOTION:
if (event.motion.state == SDL_PRESSED) {
SkRect& rect = state->fRects.back();
rect.fRight = event.motion.x;
rect.fBottom = event.motion.y;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.state == SDL_PRESSED) {
state->fRects.push_back() = SkRect::MakeLTRB(SkIntToScalar(event.button.x),
SkIntToScalar(event.button.y),
SkIntToScalar(event.button.x),
SkIntToScalar(event.button.y));
}
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
event.window.event == SDL_WINDOWEVENT_RESIZED) {
state->window_width = event.window.data1;
state->window_height = event.window.data2;
}
break;
case SDL_KEYDOWN: {
SDL_Keycode key = event.key.keysym.sym;
if (key == SDLK_ESCAPE) {
state->fQuit = true;
}
break;
}
case SDL_QUIT:
state->fQuit = true;
break;
default:
break;
}
}
}
// Creates a star type shape using a SkPath
static SkPath create_star() {
static const int kNumPoints = 5;
SkPath concavePath;
SkPoint points[kNumPoints] = {{0, SkIntToScalar(-50)} };
SkMatrix rot;
rot.setRotate(SkIntToScalar(360) / kNumPoints);
for (int i = 1; i < kNumPoints; ++i) {
rot.mapPoints(points + i, points + i - 1, 1);
}
concavePath.moveTo(points[0]);
for (int i = 0; i < kNumPoints; ++i) {
concavePath.lineTo(points[(2 * i) % kNumPoints]);
}
concavePath.setFillType(SkPathFillType::kEvenOdd);
SkASSERT(!concavePath.isConvex());
concavePath.close();
return concavePath;
}
#if defined(SK_BUILD_FOR_ANDROID)
int SDL_main(int argc, char** argv) {
#else
int main(int argc, char** argv) {
#endif
uint32_t windowFlags = 0;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GLContext glContext = nullptr;
#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
// For Android/iOS we need to set up for OpenGL ES and we make the window hi res & full screen
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |
SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP |
SDL_WINDOW_ALLOW_HIGHDPI;
#else
// For all other clients we use the core profile and operate in a window
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
#endif
static const int kStencilBits = 8; // Skia needs 8 stencil bits
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, kStencilBits);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
// If you want multisampling, uncomment the below lines and set a sample count
static const int kMsaaSampleCount = 0; //4;
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, kMsaaSampleCount);
/*
* In a real application you might want to initialize more subsystems
*/
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
handle_error();
return 1;
}
// Setup window
// This code will create a window with the same resolution as the user's desktop.
SDL_DisplayMode dm;
if (SDL_GetDesktopDisplayMode(0, &dm) != 0) {
handle_error();
return 1;
}
SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, dm.w, dm.h, windowFlags);
if (!window) {
handle_error();
return 1;
}
// To go fullscreen
// SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
// try and setup a GL context
glContext = SDL_GL_CreateContext(window);
if (!glContext) {
handle_error();
return 1;
}
int success = SDL_GL_MakeCurrent(window, glContext);
if (success != 0) {
handle_error();
return success;
}
uint32_t windowFormat = SDL_GetWindowPixelFormat(window);
int contextType;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &contextType);
int dw, dh;
SDL_GL_GetDrawableSize(window, &dw, &dh);
glViewport(0, 0, dw, dh);
glClearColor(1, 1, 1, 1);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// setup GrContext
auto interface = GrGLMakeNativeInterface();
// setup contexts
sk_sp<GrDirectContext> grContext(GrDirectContexts::MakeGL(interface));
SkASSERT(grContext);
// Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
// render to it
GrGLint buffer;
GR_GL_GetIntegerv(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
GrGLFramebufferInfo info;
info.fFBOID = (GrGLuint) buffer;
SkColorType colorType;
//SkDebugf("%s", SDL_GetPixelFormatName(windowFormat));
// TODO: the windowFormat is never any of these?
if (SDL_PIXELFORMAT_RGBA8888 == windowFormat) {
info.fFormat = GR_GL_RGBA8;
colorType = kRGBA_8888_SkColorType;
} else {
colorType = kBGRA_8888_SkColorType;
if (SDL_GL_CONTEXT_PROFILE_ES == contextType) {
info.fFormat = GR_GL_BGRA8;
} else {
// We assume the internal format is RGBA8 on desktop GL
info.fFormat = GR_GL_RGBA8;
}
}
GrBackendRenderTarget target = GrBackendRenderTargets::MakeGL(dw, dh, kMsaaSampleCount, kStencilBits, info);
// setup SkSurface
// To use distance field text, use commented out SkSurfaceProps instead
// SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
// SkSurfaceProps::kUnknown_SkPixelGeometry);
SkSurfaceProps props;
sk_sp<SkSurface> surface(SkSurfaces::WrapBackendRenderTarget(grContext.get(), target,
kBottomLeft_GrSurfaceOrigin,
colorType, nullptr, &props));
SkCanvas* canvas = surface->getCanvas();
canvas->scale((float)dw/dm.w, (float)dh/dm.h);
ApplicationState state;
state.window_width = dw;
state.window_height = dh;
const char* helpMessage = "Click and drag to create rects. Press esc to quit.";
SkPaint paint;
// create a surface for CPU rasterization
sk_sp<SkSurface> cpuSurface(SkSurfaces::Raster(canvas->imageInfo()));
SkCanvas* offscreen = cpuSurface->getCanvas();
offscreen->save();
offscreen->translate(50.0f, 50.0f);
offscreen->drawPath(create_star(), paint);
offscreen->restore();
sk_sp<SkImage> image = cpuSurface->makeImageSnapshot();
int rotation = 0;
#ifdef __linux__
sk_sp<SkFontConfigInterface> fc(SkFontConfigInterface::RefGlobal());
sk_sp<SkTypeface> typeface(SkFontMgr_New_FCI(std::move(fc))->legacyMakeTypeface("",SkFontStyle()));
#endif
#ifdef __APPLE__
sk_sp<SkTypeface> typeface(SkFontMgr_New_CoreText(nullptr)->legacyMakeTypeface("",SkFontStyle()));
#endif
SkFont font(typeface);
while (!state.fQuit) { // Our application loop
SkRandom rand;
canvas->clear(SK_ColorWHITE);
handle_events(&state, canvas);
paint.setColor(SK_ColorBLACK);
canvas->translate(0, dh - state.window_height);
canvas->drawString(helpMessage, 100.0f, 100.0f, font, paint);
for (int i = 0; i < state.fRects.size(); i++) {
paint.setColor(rand.nextU() | 0x44808080);
canvas->drawRect(state.fRects[i], paint);
}
canvas->translate(0, -(dh - state.window_height));
// draw offscreen canvas
canvas->save();
canvas->translate(dm.w / 2.0, dm.h / 2.0);
canvas->rotate(rotation++);
canvas->drawImage(image, -50.0f, -50.0f);
canvas->restore();
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
dContext->flushAndSubmit();
}
SDL_GL_SwapWindow(window);
}
if (glContext) {
SDL_GL_DeleteContext(glContext);
}
//Destroy window
SDL_DestroyWindow(window);
//Quit SDL subsystems
SDL_Quit();
return 0;
}