Skip to content

Commit 50bb065

Browse files
Merge pull request #230 from CarterLi/master
OpenGL / OpenCL: add macOS support
2 parents b815464 + a8b0e77 commit 50bb065

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

CMakeLists.txt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ cmake_dependent_option(ENABLE_ZLIB "Enable zlib" ON "ENABLE_IMAGEMAGICK6 OR ENAB
4343
cmake_dependent_option(ENABLE_EGL "Enable egl" ON "LINUX" OFF)
4444
cmake_dependent_option(ENABLE_GLX "Enable glx" ON "LINUX" OFF)
4545
cmake_dependent_option(ENABLE_OSMESA "Enable osmesa" ON "LINUX" OFF)
46-
cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX" OFF)
46+
cmake_dependent_option(ENABLE_APPLECGL "Enable apple-cgl" ON "APPLE" OFF)
47+
cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR APPLE" OFF)
4748

4849
option(BUILD_TESTS "Build tests" OFF) # Also create test executables
4950
option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by github actions for release builds
@@ -309,10 +310,34 @@ ff_check_lib(IMAGEMAGICK7 MagickCore-7.Q16HDRI MagickCore-7.Q16 /usr/lib/imagema
309310
ff_check_lib(IMAGEMAGICK6 MagickCore-6.Q16HDRI MagickCore-6.Q16 /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16HDRI.pc /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16.pc)
310311
ff_check_lib(ZLIB zlib)
311312
ff_check_lib(CHAFA chafa>=1.10)
312-
ff_check_lib(EGL egl)
313-
ff_check_lib(GLX glx)
314-
ff_check_lib(OSMESA osmesa)
315-
ff_check_lib(OPENCL OpenCL)
313+
314+
if(APPLE)
315+
# Apple framework bundles can't be found by pkg-config
316+
if(ENABLE_APPLECGL)
317+
find_package(OpenGL)
318+
if(OpenGL_FOUND)
319+
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_APPLECGL=1)
320+
target_link_libraries(libfastfetch PRIVATE ${OPENGL_LIBRARIES})
321+
else()
322+
message(WARNING "Package Apple-CGL not found, building without support.")
323+
endif()
324+
endif()
325+
326+
if(ENABLE_OPENCL)
327+
find_package(OpenCL)
328+
if(OpenCL_FOUND)
329+
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_OPENCL=1)
330+
target_link_libraries(libfastfetch PRIVATE ${OpenCL_LIBRARY})
331+
else()
332+
message(WARNING "Package OpenCL not found, building without support.")
333+
endif()
334+
endif()
335+
else()
336+
ff_check_lib(EGL egl)
337+
ff_check_lib(GLX glx)
338+
ff_check_lib(OSMESA osmesa)
339+
ff_check_lib(OPENCL OpenCL)
340+
endif()
316341

317342
target_include_directories(libfastfetch
318343
PUBLIC ${PROJECT_BINARY_DIR}

src/common/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ void ffListFeatures()
430430
#ifdef FF_HAVE_OSMESA
431431
"osmesa\n"
432432
#endif
433+
#ifdef FF_HAVE_APPLECGL
434+
"apple-cgl\n"
435+
#endif
433436
#ifdef FF_HAVE_OPENCL
434437
"opencl\n"
435438
#endif

src/fastfetch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
12581258
"egl", FF_GL_TYPE_EGL,
12591259
"glx", FF_GL_TYPE_GLX,
12601260
"osmesa", FF_GL_TYPE_OSMESA,
1261+
"apple-cgl", FF_GL_TYPE_APPLECGL,
12611262
NULL
12621263
);
12631264
}

src/fastfetch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ typedef enum FFGLType
5050
FF_GL_TYPE_AUTO,
5151
FF_GL_TYPE_EGL,
5252
FF_GL_TYPE_GLX,
53-
FF_GL_TYPE_OSMESA
53+
FF_GL_TYPE_OSMESA,
54+
FF_GL_TYPE_APPLECGL
5455
} FFGLType;
5556

5657
typedef struct FFModuleArgs

src/modules/opencl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
#include <string.h>
1111

1212
#define CL_TARGET_OPENCL_VERSION 100
13+
#ifdef __APPLE__
14+
#include <OpenCL/cl.h>
15+
#else
1316
#include <CL/cl.h>
17+
#endif
1418

1519
typedef struct OpenCLData
1620
{
@@ -75,13 +79,21 @@ static const char* printOpenCL(FFinstance* instance)
7579
{
7680
OpenCLData data;
7781

82+
#ifdef __APPLE__
83+
data.ffclGetPlatformIDs = clGetPlatformIDs;
84+
data.ffclGetDeviceIDs = clGetDeviceIDs;
85+
data.ffclGetDeviceInfo = clGetDeviceInfo;
86+
#else
7887
FF_LIBRARY_LOAD(opencl, instance->config.libOpenCL, "dlopen libOpenCL.so failed", "libOpenCL.so", 1);
7988
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformIDs);
8089
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs);
8190
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo);
91+
#endif
8292

8393
const char* error = openCLHandelData(instance, &data);
94+
#ifndef __APPLE__
8495
dlclose(opencl);
96+
#endif
8597
return error;
8698
}
8799
#endif

src/modules/opengl.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define FF_OPENGL_MODULE_NAME "OpenGL"
88
#define FF_OPENGL_NUM_FORMAT_ARGS 4
99

10-
#if defined(FF_HAVE_EGL) || defined(FF_HAVE_GLX) || defined(FF_HAVE_OSMESA)
10+
#if defined(FF_HAVE_EGL) || defined(FF_HAVE_GLX) || defined(FF_HAVE_OSMESA) || defined(FF_HAVE_APPLECGL)
1111
#define FF_HAVE_GL 1
1212
#include "common/library.h"
1313
#ifdef __APPLE__
@@ -337,6 +337,45 @@ static const char* osMesaPrint(FFinstance* instance)
337337

338338
#endif //FF_HAVE_OSMESA
339339

340+
#ifdef FF_HAVE_APPLECGL
341+
342+
#include <OpenGL/OpenGL.h>
343+
344+
static const char* appleCglPrint(FFinstance* instance)
345+
{
346+
CGLPixelFormatAttribute attrs[] = {
347+
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
348+
kCGLPFAAccelerated,
349+
0
350+
};
351+
CGLPixelFormatObj pix;
352+
GLint num;
353+
if (CGLChoosePixelFormat(attrs, &pix, &num) != kCGLNoError) {
354+
return "CGLChoosePixelFormat() failed";
355+
}
356+
357+
CGLContextObj ctx;
358+
if (CGLCreateContext(pix, NULL, &ctx) != kCGLNoError) {
359+
return "CGLCreateContext() failed";
360+
}
361+
if (CGLSetCurrentContext(ctx) != kCGLNoError) {
362+
return "CGLSetCurrentContext() failed";
363+
}
364+
365+
GLData glData = {
366+
.ffglGetString = glGetString
367+
};
368+
369+
const char* error = glHandlePrint(instance, &glData);
370+
371+
CGLDestroyContext(ctx);
372+
CGLDestroyPixelFormat(pix);
373+
374+
return error;
375+
}
376+
377+
#endif //FF_HAVE_APPLECGL
378+
340379
static const char* glPrint(FFinstance* instance)
341380
{
342381
if(instance->config.glType == FF_GL_TYPE_GLX)
@@ -366,6 +405,15 @@ static const char* glPrint(FFinstance* instance)
366405
#endif
367406
}
368407

408+
if(instance->config.glType == FF_GL_TYPE_APPLECGL)
409+
{
410+
#ifdef FF_HAVE_APPLECGL
411+
return appleCglPrint(instance);
412+
#else
413+
return "fastfetch was compiled without apple-cgl support";
414+
#endif
415+
}
416+
369417
const char* error = ""; // not NULL dummy value
370418

371419
#ifdef FF_HAVE_EGL
@@ -377,6 +425,11 @@ static const char* glPrint(FFinstance* instance)
377425
error = glxPrint(instance);
378426
#endif
379427

428+
#ifdef FF_HAVE_APPLECGL
429+
if(error != NULL)
430+
error = appleCglPrint(instance);
431+
#endif
432+
380433
//We don't use osmesa in auto mode here, because it is a software implementation,
381434
//that doesn't reflect the opengl supported by the hardware
382435

0 commit comments

Comments
 (0)