GLWindowContext_mac.cpp revision a3f3caccdfa6e5e044cc1c76e256e55b8a6004ad
1
2/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GLWindowContext_mac.h"
10
11//#include <GL/gl.h>
12
13#include "Window_mac.h"
14
15namespace sk_app {
16
17// platform-dependent create
18GLWindowContext* GLWindowContext::Create(void* platformData, const DisplayParams& params) {
19    GLWindowContext_mac* ctx = new GLWindowContext_mac(platformData, params);
20    if (!ctx->isValid()) {
21        delete ctx;
22        return nullptr;
23    }
24    return ctx;
25}
26
27GLWindowContext_mac::GLWindowContext_mac(void* platformData, const DisplayParams& params)
28    : GLWindowContext(platformData, params)
29#if 0
30    // TODO: init Mac-specific OpenGL objects
31    , fDisplay(nullptr)
32    , fWindow(0)
33    , fGLContext(0)
34#endif
35    {
36
37    // any config code here (particularly for msaa)?
38
39    this->initializeContext(platformData, params);
40}
41
42GLWindowContext_mac::~GLWindowContext_mac() {
43    this->destroyContext();
44}
45
46void GLWindowContext_mac::onInitializeContext(void* platformData, const DisplayParams& params) {
47#if 0
48    // TODO: Init for Mac
49    ContextPlatformData_mac* unixPlatformData =
50        reinterpret_cast<ContextPlatformData_mac*>(platformData);
51
52    if (unixPlatformData) {
53        fDisplay = unixPlatformData->fDisplay;
54        fWindow = unixPlatformData->fWindow;
55        fVisualInfo = unixPlatformData->fVisualInfo;
56    }
57    SkASSERT(fDisplay);
58
59    fGLContext = glXCreateContext(fDisplay, fVisualInfo, nullptr, GL_TRUE);
60    if (!fGLContext) {
61        return;
62    }
63
64    if (glXMakeCurrent(fDisplay, fWindow, fGLContext)) {
65        glClearStencil(0);
66        glClearColor(0, 0, 0, 0);
67        glStencilMask(0xffffffff);
68        glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
69
70        int redBits, greenBits, blueBits;
71        glXGetConfig(fDisplay, fVisualInfo, GLX_RED_SIZE, &redBits);
72        glXGetConfig(fDisplay, fVisualInfo, GLX_GREEN_SIZE, &greenBits);
73        glXGetConfig(fDisplay, fVisualInfo, GLX_BLUE_SIZE, &blueBits);
74        fColorBits = redBits + greenBits + blueBits;
75        glXGetConfig(fDisplay, fVisualInfo, GLX_STENCIL_SIZE, &fStencilBits);
76        glXGetConfig(fDisplay, fVisualInfo, GLX_SAMPLES_ARB, &fSampleCount);
77
78        XWindow root;
79        int x, y;
80        unsigned int border_width, depth;
81        XGetGeometry(fDisplay, fWindow, &root, &x, &y,
82                     (unsigned int*)&fWidth, (unsigned int*)&fHeight, &border_width, &depth);
83        glViewport(0, 0, fWidth, fHeight);
84    }
85#endif
86}
87
88void GLWindowContext_mac::onDestroyContext() {
89#if 0
90    // TODO: teardown for Mac
91    if (!fDisplay || !fGLContext) {
92        return;
93    }
94    glXMakeCurrent(fDisplay, None, nullptr);
95    glXDestroyContext(fDisplay, fGLContext);
96    fGLContext = nullptr;
97#endif
98}
99
100
101void GLWindowContext_mac::onSwapBuffers() {
102#if 0
103    // TODO: swap for Mac
104    if (fDisplay && fGLContext) {
105        glXSwapBuffers(fDisplay, fWindow);
106    }
107#endif
108}
109
110
111}   //namespace sk_app
112