This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathtest_gl_surface.cc
264 lines (210 loc) · 8.05 KB
/
test_gl_surface.cc
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/test_gl_surface.h"
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <memory>
#include <string>
#include "flutter/fml/logging.h"
#include "flutter/testing/test_gl_utils.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkColorType.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLAssembleInterface.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLTypes.h"
namespace flutter::testing {
TestGLOnscreenOnlySurface::TestGLOnscreenOnlySurface(
std::shared_ptr<TestEGLContext> context,
SkISize size)
: surface_size_(size), egl_context_(std::move(context)) {
const EGLint attributes[] = {
EGL_WIDTH, size.width(), //
EGL_HEIGHT, size.height(), //
EGL_NONE,
};
onscreen_surface_ =
::eglCreatePbufferSurface(egl_context_->display, // display connection
egl_context_->config, // config
attributes // surface attributes
);
FML_CHECK(onscreen_surface_ != EGL_NO_SURFACE) << GetEGLError();
}
TestGLOnscreenOnlySurface::~TestGLOnscreenOnlySurface() {
skia_context_ = nullptr;
auto result = ::eglDestroySurface(egl_context_->display, onscreen_surface_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
}
const SkISize& TestGLOnscreenOnlySurface::GetSurfaceSize() const {
return surface_size_;
}
bool TestGLOnscreenOnlySurface::MakeCurrent() {
auto result =
::eglMakeCurrent(egl_context_->display, onscreen_surface_,
onscreen_surface_, egl_context_->onscreen_context);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not make the context current. " << GetEGLError();
}
return result == EGL_TRUE;
}
bool TestGLOnscreenOnlySurface::ClearCurrent() {
auto result = ::eglMakeCurrent(egl_context_->display, EGL_NO_SURFACE,
EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not clear the current context. " << GetEGLError();
}
return result == EGL_TRUE;
}
bool TestGLOnscreenOnlySurface::Present() {
auto result = ::eglSwapBuffers(egl_context_->display, onscreen_surface_);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not swap buffers. " << GetEGLError();
}
return result == EGL_TRUE;
}
uint32_t TestGLOnscreenOnlySurface::GetFramebuffer(uint32_t width,
uint32_t height) const {
return GetWindowFBOId();
}
void* TestGLOnscreenOnlySurface::GetProcAddress(const char* name) const {
if (name == nullptr) {
return nullptr;
}
auto symbol = ::eglGetProcAddress(name);
if (symbol == NULL) {
FML_LOG(ERROR) << "Could not fetch symbol for name: " << name;
}
return reinterpret_cast<void*>(symbol);
}
sk_sp<GrDirectContext> TestGLOnscreenOnlySurface::GetGrContext() {
if (skia_context_) {
return skia_context_;
}
return CreateGrContext();
}
sk_sp<GrDirectContext> TestGLOnscreenOnlySurface::CreateGrContext() {
if (!MakeCurrent()) {
return nullptr;
}
auto get_string =
reinterpret_cast<PFNGLGETSTRINGPROC>(GetProcAddress("glGetString"));
if (!get_string) {
return nullptr;
}
auto c_version = reinterpret_cast<const char*>(get_string(GL_VERSION));
if (c_version == NULL) {
return nullptr;
}
GrGLGetProc get_proc = [](void* context, const char name[]) -> GrGLFuncPtr {
return reinterpret_cast<GrGLFuncPtr>(
reinterpret_cast<TestGLOnscreenOnlySurface*>(context)->GetProcAddress(
name));
};
std::string version(c_version);
auto interface = version.find("OpenGL ES") == std::string::npos
? GrGLMakeAssembledGLInterface(this, get_proc)
: GrGLMakeAssembledGLESInterface(this, get_proc);
if (!interface) {
return nullptr;
}
skia_context_ = GrDirectContexts::MakeGL(interface);
return skia_context_;
}
sk_sp<SkSurface> TestGLOnscreenOnlySurface::GetOnscreenSurface() {
FML_CHECK(::eglGetCurrentContext() != EGL_NO_CONTEXT);
GrGLFramebufferInfo framebuffer_info = {};
const uint32_t width = surface_size_.width();
const uint32_t height = surface_size_.height();
framebuffer_info.fFBOID = GetFramebuffer(width, height);
#if FML_OS_MACOSX
framebuffer_info.fFormat = 0x8058; // GL_RGBA8
#else
framebuffer_info.fFormat = 0x93A1; // GL_BGRA8;
#endif
auto backend_render_target =
GrBackendRenderTargets::MakeGL(width, // width
height, // height
1, // sample count
8, // stencil bits
framebuffer_info // framebuffer info
);
SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
auto surface = SkSurfaces::WrapBackendRenderTarget(
GetGrContext().get(), // context
backend_render_target, // backend render target
kBottomLeft_GrSurfaceOrigin, // surface origin
kN32_SkColorType, // color type
SkColorSpace::MakeSRGB(), // color space
&surface_properties, // surface properties
nullptr, // release proc
nullptr // release context
);
if (!surface) {
FML_LOG(ERROR) << "Could not wrap the surface while attempting to "
"snapshot the GL surface.";
return nullptr;
}
return surface;
}
sk_sp<SkImage> TestGLOnscreenOnlySurface::GetRasterSurfaceSnapshot() {
auto surface = GetOnscreenSurface();
if (!surface) {
FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface "
"acquisition failure.";
return nullptr;
}
auto device_snapshot = surface->makeImageSnapshot();
if (!device_snapshot) {
FML_LOG(ERROR) << "Could not create the device snapshot while attempting "
"to snapshot the GL surface.";
return nullptr;
}
auto host_snapshot = device_snapshot->makeRasterImage();
if (!host_snapshot) {
FML_LOG(ERROR) << "Could not create the host snapshot while attempting to "
"snapshot the GL surface.";
return nullptr;
}
return host_snapshot;
}
uint32_t TestGLOnscreenOnlySurface::GetWindowFBOId() const {
return 0u;
}
TestGLSurface::TestGLSurface(SkISize surface_size)
: TestGLSurface(std::make_shared<TestEGLContext>(), surface_size) {}
TestGLSurface::TestGLSurface(std::shared_ptr<TestEGLContext> egl_context,
SkISize surface_size)
: TestGLOnscreenOnlySurface(std::move(egl_context), surface_size) {
{
const EGLint offscreen_surface_attributes[] = {
EGL_WIDTH, 1, //
EGL_HEIGHT, 1, //
EGL_NONE,
};
offscreen_surface_ = ::eglCreatePbufferSurface(
egl_context_->display, // display connection
egl_context_->config, // config
offscreen_surface_attributes // surface attributes
);
FML_CHECK(offscreen_surface_ != EGL_NO_SURFACE) << GetEGLError();
}
}
TestGLSurface::~TestGLSurface() {
auto result = ::eglDestroySurface(egl_context_->display, offscreen_surface_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
}
bool TestGLSurface::MakeResourceCurrent() {
auto result =
::eglMakeCurrent(egl_context_->display, offscreen_surface_,
offscreen_surface_, egl_context_->offscreen_context);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not make the resource context current. "
<< GetEGLError();
}
return result == EGL_TRUE;
}
} // namespace flutter::testing