Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format all c++ files with clang-format. #307

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Format all c++ files with clang-format.
null77 committed Jan 13, 2025
commit 58672297d1f43ab2429a7bd63d47e731032b4ec8
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ColumnLimit: 100
83 changes: 37 additions & 46 deletions src/native/SharedLibrary.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <string>
#include <array>
#include <string>

#ifdef _WIN32
#include <windows.h>
@@ -8,62 +8,53 @@
#endif

#ifdef _WIN32
std::string Narrow(const std::wstring_view &utf16)
{
if (utf16.empty())
{
return {};
}
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()),
nullptr, 0, nullptr, nullptr);
std::string utf8(requiredSize, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0],
requiredSize, nullptr, nullptr);
return utf8;
std::string Narrow(const std::wstring_view &utf16) {
if (utf16.empty()) {
return {};
}
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()),
nullptr, 0, nullptr, nullptr);
std::string utf8(requiredSize, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0],
requiredSize, nullptr, nullptr);
return utf8;
}

std::string GetPath(HMODULE module)
{
std::array<wchar_t, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(),
static_cast<DWORD>(executableFileBuf.size()));
return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L"");
std::string GetPath(HMODULE module) {
std::array<wchar_t, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(),
static_cast<DWORD>(executableFileBuf.size()));
return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L"");
}

std::string GetModulePath(void *moduleOrSymbol)
{
HMODULE module = nullptr;
if (GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module))
{
return GetPath(module);
}
std::string GetModulePath(void *moduleOrSymbol) {
HMODULE module = nullptr;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module)) {
return GetPath(module);
}

return "";
return "";
}
#else
std::string GetModulePath(void *moduleOrSymbol)
{
Dl_info dlInfo;
if (dladdr(moduleOrSymbol, &dlInfo) == 0)
{
return "";
}
std::string GetModulePath(void *moduleOrSymbol) {
Dl_info dlInfo;
if (dladdr(moduleOrSymbol, &dlInfo) == 0) {
return "";
}

return dlInfo.dli_fname;
return dlInfo.dli_fname;
}
#endif

std::string StripFilenameFromPath(const std::string &path)
{
size_t lastPathSepLoc = path.find_last_of("\\/");
return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : "";
std::string StripFilenameFromPath(const std::string &path) {
size_t lastPathSepLoc = path.find_last_of("\\/");
return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : "";
}

std::string GetModuleDirectory()
{
static int placeholderSymbol = 0;
std::string moduleName = GetModulePath(&placeholderSymbol);
return StripFilenameFromPath(moduleName);
std::string GetModuleDirectory() {
static int placeholderSymbol = 0;
std::string moduleName = GetModulePath(&placeholderSymbol);
return StripFilenameFromPath(moduleName);
}
45 changes: 23 additions & 22 deletions src/native/SharedLibrary.h
Original file line number Diff line number Diff line change
@@ -12,11 +12,11 @@

inline std::string GetSharedLibraryExtension() {
#ifdef _WIN32
return ".dll"; // Windows
return ".dll"; // Windows
#elif __APPLE__
return ".dylib"; // OSX
return ".dylib"; // OSX
#elif __linux__
return ".so"; // Linux
return ".so"; // Linux
#else
#error Unsupported platform
#endif
@@ -26,38 +26,39 @@ std::string GetModuleDirectory();

class SharedLibrary {
public:
SharedLibrary() {}
SharedLibrary() {}

bool open(const std::string& libraryPath) {
const std::string libraryPathWithExt = GetModuleDirectory() + "/" + libraryPath + GetSharedLibraryExtension();
bool open(const std::string &libraryPath) {
const std::string libraryPathWithExt =
GetModuleDirectory() + "/" + libraryPath + GetSharedLibraryExtension();
#ifdef _WIN32
handle = LoadLibraryA(libraryPathWithExt.c_str());
handle = LoadLibraryA(libraryPathWithExt.c_str());
#else
handle = dlopen(libraryPathWithExt.c_str(), RTLD_LAZY);
handle = dlopen(libraryPathWithExt.c_str(), RTLD_LAZY);
#endif
return handle != nullptr;
}
return handle != nullptr;
}

~SharedLibrary() {
if (handle) {
~SharedLibrary() {
if (handle) {
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle));
FreeLibrary(static_cast<HMODULE>(handle));
#else
dlclose(handle);
dlclose(handle);
#endif
}
}
}

template <typename Func>
Func getFunction(const std::string& functionName) {
template <typename Func> Func getFunction(const std::string &functionName) {
#ifdef _WIN32
auto func = reinterpret_cast<Func>(GetProcAddress(static_cast<HMODULE>(handle), functionName.c_str()));
auto func =
reinterpret_cast<Func>(GetProcAddress(static_cast<HMODULE>(handle), functionName.c_str()));
#else
auto func = reinterpret_cast<Func>(dlsym(handle, functionName.c_str()));
auto func = reinterpret_cast<Func>(dlsym(handle, functionName.c_str()));
#endif
return func;
}
return func;
}

private:
void* handle = nullptr;
void *handle = nullptr;
};
51 changes: 17 additions & 34 deletions src/native/bindings.cc
Original file line number Diff line number Diff line change
@@ -5,33 +5,25 @@
* Author: ngk437
*/

#include <cstdlib>
#include "webgl.h"
#include <cstdlib>

Nan::Persistent<v8::FunctionTemplate> WEBGL_TEMPLATE;

#define JS_GL_METHOD(webgl_name, method_name) \
Nan::SetPrototypeTemplate(\
webgl_template \
, webgl_name\
, Nan::New<v8::FunctionTemplate>(\
WebGLRenderingContext:: method_name))
#define JS_GL_METHOD(webgl_name, method_name) \
Nan::SetPrototypeTemplate(webgl_template, webgl_name, \
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::method_name))

#define JS_CONSTANT(x, v) \
Nan::SetPrototypeTemplate( \
webgl_template \
, #x \
, Nan::New<v8::Integer>(v))
#define JS_CONSTANT(x, v) Nan::SetPrototypeTemplate(webgl_template, #x, Nan::New<v8::Integer>(v))

#define JS_GL_CONSTANT(name) JS_CONSTANT(name, GL_ ## name)
#define JS_GL_CONSTANT(name) JS_CONSTANT(name, GL_##name)

NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> webgl_template =
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::New);
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::New);

webgl_template->InstanceTemplate()->SetInternalFieldCount(1);
webgl_template->SetClassName(
Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked());
webgl_template->SetClassName(Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked());

/* WebGL methods */
JS_GL_METHOD("_drawArraysInstancedANGLE", DrawArraysInstancedANGLE);
@@ -167,25 +159,18 @@ NAN_MODULE_INIT(Init) {
JS_GL_METHOD("bindVertexArrayOES", BindVertexArrayOES);

// Windows defines a macro called NO_ERROR which messes this up
Nan::SetPrototypeTemplate(
webgl_template,
"NO_ERROR",
Nan::New<v8::Integer>(GL_NO_ERROR));
Nan::SetPrototypeTemplate(webgl_template, "NO_ERROR", Nan::New<v8::Integer>(GL_NO_ERROR));
JS_GL_CONSTANT(INVALID_ENUM);
JS_GL_CONSTANT(INVALID_VALUE);
JS_GL_CONSTANT(INVALID_OPERATION);
JS_GL_CONSTANT(OUT_OF_MEMORY);

// OpenGL ES 2.1 constants
Nan::SetPrototypeTemplate(
webgl_template
, "DEPTH_STENCIL"
, Nan::New<v8::Integer>(GL_DEPTH_STENCIL_OES));
Nan::SetPrototypeTemplate(webgl_template, "DEPTH_STENCIL",
Nan::New<v8::Integer>(GL_DEPTH_STENCIL_OES));

Nan::SetPrototypeTemplate(
webgl_template
, "DEPTH_STENCIL_ATTACHMENT"
, Nan::New<v8::Integer>(0x821A));
Nan::SetPrototypeTemplate(webgl_template, "DEPTH_STENCIL_ATTACHMENT",
Nan::New<v8::Integer>(0x821A));

JS_GL_CONSTANT(MAX_VERTEX_UNIFORM_VECTORS);
JS_GL_CONSTANT(MAX_VARYING_VECTORS);
@@ -480,14 +465,12 @@ NAN_MODULE_INIT(Init) {
JS_CONSTANT(IMPLEMENTATION_COLOR_READ_TYPE, 0x8B9A);
JS_CONSTANT(IMPLEMENTATION_COLOR_READ_FORMAT, 0x8B9B);

//Export template
// Export template
WEBGL_TEMPLATE.Reset(webgl_template);
Nan::Set(
target
, Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked()
, Nan::GetFunction(webgl_template).ToLocalChecked());
Nan::Set(target, Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked(),
Nan::GetFunction(webgl_template).ToLocalChecked());

//Export helper methods for clean up and error handling
// Export helper methods for clean up and error handling
Nan::Export(target, "cleanup", WebGLRenderingContext::DisposeAll);
Nan::Export(target, "setError", WebGLRenderingContext::SetError);
}
1,498 changes: 664 additions & 834 deletions src/native/webgl.cc

Large diffs are not rendered by default.

80 changes: 32 additions & 48 deletions src/native/webgl.h
Original file line number Diff line number Diff line change
@@ -2,21 +2,21 @@
#define WEBGL_H_

#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <vector>

#include <node.h>
#include "nan.h"
#include <node.h>
#include <v8.h>

#define EGL_EGL_PROTOTYPES 0
#define GL_GLES_PROTOTYPES 0

#include "SharedLibrary.h"
#include "angle-loader/egl_loader.h"
#include "angle-loader/gles_loader.h"
#include "SharedLibrary.h"

enum GLObjectType {
GLOBJECT_TYPE_BUFFER,
@@ -39,96 +39,80 @@ typedef std::pair<GLuint, GLObjectType> GLObjectReference;

struct WebGLRenderingContext : public node::ObjectWrap {

//The underlying OpenGL context
static bool HAS_DISPLAY;
// The underlying OpenGL context
static bool HAS_DISPLAY;
static EGLDisplay DISPLAY;

SharedLibrary eglLibrary;
EGLContext context;
EGLConfig config;
EGLConfig config;
EGLSurface surface;
GLContextState state;
GLContextState state;

//Pixel storage flags
bool unpack_flip_y;
bool unpack_premultiply_alpha;
// Pixel storage flags
bool unpack_flip_y;
bool unpack_premultiply_alpha;
GLint unpack_colorspace_conversion;
GLint unpack_alignment;

std::set<std::string> requestableExtensions;
std::set<std::string> enabledExtensions;
std::map<std::string, std::vector<std::string>> webGLToANGLEExtensions;

//A list of object references, need do destroy them at program exit
std::map< std::pair<GLuint, GLObjectType>, bool > objects;
void registerGLObj(GLObjectType type, GLuint obj) {
objects[std::make_pair(obj, type)] = true;
}
void unregisterGLObj(GLObjectType type, GLuint obj) {
objects.erase(std::make_pair(obj, type));
}
// A list of object references, need do destroy them at program exit
std::map<std::pair<GLuint, GLObjectType>, bool> objects;
void registerGLObj(GLObjectType type, GLuint obj) { objects[std::make_pair(obj, type)] = true; }
void unregisterGLObj(GLObjectType type, GLuint obj) { objects.erase(std::make_pair(obj, type)); }

//Context list
// Context list
WebGLRenderingContext *next, *prev;
static WebGLRenderingContext* CONTEXT_LIST_HEAD;
static WebGLRenderingContext *CONTEXT_LIST_HEAD;
void registerContext() {
if(CONTEXT_LIST_HEAD) {
if (CONTEXT_LIST_HEAD) {
CONTEXT_LIST_HEAD->prev = this;
}
next = CONTEXT_LIST_HEAD;
prev = NULL;
CONTEXT_LIST_HEAD = this;
}
void unregisterContext() {
if(next) {
if (next) {
next->prev = this->prev;
}
if(prev) {
if (prev) {
prev->next = this->next;
}
if(CONTEXT_LIST_HEAD == this) {
if (CONTEXT_LIST_HEAD == this) {
CONTEXT_LIST_HEAD = this->next;
}
next = prev = NULL;
}

//Constructor
WebGLRenderingContext(
int width,
int height,
bool alpha,
bool depth,
bool stencil,
bool antialias,
bool premultipliedAlpha,
bool preserveDrawingBuffer,
bool preferLowPowerToHighPerformance,
bool failIfMajorPerformanceCaveat);
// Constructor
WebGLRenderingContext(int width, int height, bool alpha, bool depth, bool stencil, bool antialias,
bool premultipliedAlpha, bool preserveDrawingBuffer,
bool preferLowPowerToHighPerformance, bool failIfMajorPerformanceCaveat);
virtual ~WebGLRenderingContext();

//Context validation
static WebGLRenderingContext* ACTIVE;
// Context validation
static WebGLRenderingContext *ACTIVE;
bool setActive();

//Unpacks a buffer full of pixels into memory
unsigned char* unpackPixels(
GLenum type,
GLenum format,
GLint width,
GLint height,
unsigned char* pixels);
// Unpacks a buffer full of pixels into memory
unsigned char *unpackPixels(GLenum type, GLenum format, GLint width, GLint height,
unsigned char *pixels);

//Error handling
// Error handling
GLenum lastError;
void setError(GLenum error);
GLenum getError();
static NAN_METHOD(SetError);
static NAN_METHOD(GetError);

//Preferred depth format
// Preferred depth format
GLenum preferredDepth;

//Destructors
// Destructors
void dispose();

static NAN_METHOD(DisposeAll);