xref: /aosp_15_r20/external/skia/example/external_client/src/gl_context_helper.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#import "gl_context_helper.h"
8
9#import <AvailabilityMacros.h>
10#import <OpenGL/OpenGL.h>
11#import <dlfcn.h>
12#import <cstdio>
13
14    // cribbed from https://skia.googlesource.com/skia/+/78f0b8a7eda92e59943164caaaa00e01404643b9/tools/gpu/gl/mac/CreatePlatformGLTestContext_mac.cpp#46
15bool initialize_gl_mac() {
16    CGLPixelFormatAttribute attributes[] = {
17        // base parameters
18        kCGLPFAOpenGLProfile,
19        (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
20        kCGLPFADoubleBuffer,
21        (CGLPixelFormatAttribute)NULL
22    };
23
24    CGLPixelFormatObj pixFormat;
25    GLint npix;
26    CGLChoosePixelFormat(attributes, &pixFormat, &npix);
27    if (nullptr == pixFormat) {
28        printf("CGLChoosePixelFormat failed.");
29        return false;
30    }
31
32    CGLContextObj context;
33    CGLCreateContext(pixFormat, nullptr, &context);
34    CGLReleasePixelFormat(pixFormat);
35
36    if (!context) {
37        printf("CGLCreateContext failed.");
38        return false;
39    }
40
41    CGLSetCurrentContext(context);
42    return true;
43}
44