File: egl_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 6,065 bytes parent folder | download | duplicates (3)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/common/egl_util.h"

#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "ui/gl/buildflags.h"
#include "ui/gl/egl_util.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_implementation.h"

#if BUILDFLAG(USE_OPENGL_APITRACE)
#include <stdlib.h>
#endif

#if BUILDFLAG(USE_STATIC_ANGLE)
extern "C" {
// The ANGLE internal eglGetProcAddress
EGLAPI __eglMustCastToProperFunctionPointerType EGLAPIENTRY
EGL_GetProcAddress(const char* procname);
}
#endif  // BUILDFLAG(USE_STATIC_ANGLE)

namespace ui {
namespace {

#if BUILDFLAG(IS_FUCHSIA)
const base::FilePath::CharType kDefaultEglSoname[] =
    FILE_PATH_LITERAL("libEGL.so");
const base::FilePath::CharType kDefaultGlesSoname[] =
    FILE_PATH_LITERAL("libGLESv2.so");
#else  // BUILDFLAG(IS_FUCHSIA)
const base::FilePath::CharType kDefaultEglSoname[] =
    FILE_PATH_LITERAL("libEGL.so.1");
const base::FilePath::CharType kDefaultGlesSoname[] =
    FILE_PATH_LITERAL("libGLESv2.so.2");
#endif
#if !BUILDFLAG(USE_STATIC_ANGLE)
const base::FilePath::CharType kAngleEglSoname[] =
    FILE_PATH_LITERAL("libEGL.so");
const base::FilePath::CharType kAngleGlesSoname[] =
    FILE_PATH_LITERAL("libGLESv2.so");
#endif  // !BUILDFLAG(USE_STATIC_ANGLE)

bool LoadEGLGLES2Bindings(const base::FilePath& egl_library_path,
                          const base::FilePath& gles_library_path) {
  base::NativeLibraryLoadError error;
  base::NativeLibrary gles_library;
  {
    TRACE_EVENT("gpu,startup", "Load gles_library");
    gles_library = base::LoadNativeLibrary(gles_library_path, &error);
  }
  if (!gles_library) {
    LOG(ERROR) << "Failed to load GLES library: " << gles_library_path << ": "
               << error.ToString();
    return false;
  }

  base::NativeLibrary egl_library;
  {
    TRACE_EVENT("gpu,startup", "Load egl_library");
    egl_library =
        base::LoadNativeLibrary(base::FilePath(egl_library_path), &error);
  }
  if (!egl_library) {
    LOG(ERROR) << "Failed to load EGL library: " << egl_library_path << ": "
               << error.ToString();
    base::UnloadNativeLibrary(gles_library);
    return false;
  }

  gl::GLGetProcAddressProc get_proc_address =
      reinterpret_cast<gl::GLGetProcAddressProc>(
          base::GetFunctionPointerFromNativeLibrary(egl_library,
                                                    "eglGetProcAddress"));
  if (!get_proc_address) {
    LOG(ERROR) << "eglGetProcAddress not found.";
    base::UnloadNativeLibrary(egl_library);
    base::UnloadNativeLibrary(gles_library);
    return false;
  }

  gl::SetGLGetProcAddressProc(get_proc_address);

#if BUILDFLAG(USE_OPENGL_APITRACE)
  constexpr char kTraceLibegl[] = "TRACE_LIBEGL";
  constexpr char kTraceLibglesv2[] = "TRACE_LIBGLESV2";
  constexpr char kTraceFile[] = "TRACE_FILE";

  if (egl_library_path.BaseName().value() != kDefaultEglSoname) {
    LOG(ERROR) << "Unsupported EGL library '"
               << egl_library_path.BaseName().value()
               << "'. egltrace may not work.";
  }
  if (gles_library_path.BaseName().value() != kDefaultGlesSoname) {
    LOG(ERROR) << "Unsupported GLESv2 library '"
               << gles_library_path.BaseName().value()
               << "'. egltrace may not work.";
  }

  // Send correct library names to egttrace.
  setenv(kTraceLibegl, egl_library_path.BaseName().value().c_str(),
         /*overwrite=*/0);
  setenv(kTraceLibglesv2, gles_library_path.BaseName().value().c_str(),
         /*overwrite=*/0);
#if BUILDFLAG(IS_CHROMEOS)
  setenv(kTraceFile, "/tmp/gltrace.dat", /*overwrite=*/0);
#else
  if (!getenv(kTraceFile)) {
    LOG(ERROR) << "egltrace requires valid TRACE_FILE environment variable but "
                  "none were found. Chrome will probably crash.";
  }
#endif

  LOG(WARNING) << "Loading egltrace.so with TRACE_LIBEGL="
               << getenv(kTraceLibegl)
               << " TRACE_LIBGLESV2=" << getenv(kTraceLibglesv2)
               << " TRACE_FILE=" << getenv(kTraceFile);
  const base::FilePath::CharType kDefaultTraceSoname[] =
      FILE_PATH_LITERAL("egltrace.so");
  base::NativeLibrary trace_library =
      base::LoadNativeLibrary(base::FilePath(kDefaultTraceSoname), &error);
  if (!trace_library)
    LOG(ERROR) << error.ToString();
  gl::AddGLNativeLibrary(trace_library);
#endif

  gl::AddGLNativeLibrary(egl_library);
  gl::AddGLNativeLibrary(gles_library);

  return true;
}

}  // namespace

bool LoadDefaultEGLGLES2Bindings(
    const gl::GLImplementationParts& implementation) {
  base::FilePath glesv2_path;
  base::FilePath egl_path;

  if (implementation.gl == gl::kGLImplementationEGLANGLE) {
#if BUILDFLAG(USE_STATIC_ANGLE)
    gl::SetGLGetProcAddressProc(&EGL_GetProcAddress);
    return true;
#else
    base::FilePath module_path;
#if !BUILDFLAG(IS_FUCHSIA)
    if (!base::PathService::Get(base::DIR_MODULE, &module_path))
      return false;
#endif

    glesv2_path = module_path.Append(kAngleGlesSoname);
    egl_path = module_path.Append(kAngleEglSoname);
#endif  // BUILDFLAG(USE_STATIC_ANGLE)
  } else {
    glesv2_path = base::FilePath(kDefaultGlesSoname);
    egl_path = base::FilePath(kDefaultEglSoname);
  }

  return LoadEGLGLES2Bindings(egl_path, glesv2_path);
}

EGLConfig ChooseEGLConfig(EGLDisplay display, const int32_t* attributes) {
  int32_t num_configs;
  if (!eglChooseConfig(display, attributes, nullptr, 0, &num_configs)) {
    LOG(ERROR) << "eglChooseConfig failed with error "
               << GetLastEGLErrorString();
    return nullptr;
  }

  if (num_configs == 0) {
    LOG(ERROR) << "No suitable EGL configs found.";
    return nullptr;
  }

  EGLConfig config;
  if (!eglChooseConfig(display, attributes, &config, 1, &num_configs)) {
    LOG(ERROR) << "eglChooseConfig failed with error "
               << GetLastEGLErrorString();
    return nullptr;
  }
  return config;
}

}  // namespace ui