Path: blob/21.2-virgl/src/glx/apple/apple_visual.c
4560 views
/*1Copyright (c) 2008, 2009 Apple Inc.23Permission is hereby granted, free of charge, to any person4obtaining a copy of this software and associated documentation files5(the "Software"), to deal in the Software without restriction,6including without limitation the rights to use, copy, modify, merge,7publish, distribute, sublicense, and/or sell copies of the Software,8and to permit persons to whom the Software is furnished to do so,9subject to the following conditions:1011The above copyright notice and this permission notice shall be12included in all copies or substantial portions of the Software.1314THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT18HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,19WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21DEALINGS IN THE SOFTWARE.2223Except as contained in this notice, the name(s) of the above24copyright holders shall not be used in advertising or otherwise to25promote the sale, use or other dealings in this Software without26prior written authorization.27*/2829#include <stdbool.h>30#include <stdio.h>31#include <stdlib.h>32#include <assert.h>33#include <GL/gl.h>34#include <util/debug.h>3536/* <rdar://problem/6953344> */37#define glTexImage1D glTexImage1D_OSX38#define glTexImage2D glTexImage2D_OSX39#define glTexImage3D glTexImage3D_OSX40#include <OpenGL/OpenGL.h>41#include <OpenGL/CGLContext.h>42#include <OpenGL/CGLRenderers.h>43#include <OpenGL/CGLTypes.h>44#undef glTexImage1D45#undef glTexImage2D46#undef glTexImage3D4748#ifndef kCGLPFAOpenGLProfile49#define kCGLPFAOpenGLProfile 9950#endif5152#ifndef kCGLOGLPVersion_3_2_Core53#define kCGLOGLPVersion_3_2_Core 0x320054#endif5556#include "apple_cgl.h"57#include "apple_visual.h"58#include "apple_glx.h"59#include "glxconfig.h"6061enum62{63MAX_ATTR = 6064};6566static char __crashreporter_info_buff__[4096] = { 0 };67static const char *__crashreporter_info__ __attribute__((__used__)) =68&__crashreporter_info_buff__[0];69#if MAC_OS_X_VERSION_MIN_REQUIRED >= 105070// This is actually a toolchain requirement, but I'm not sure the correct check,71// but it should be fine to just only include it for Leopard and later. This line72// just tells the linker to never strip this symbol (such as for space optimization)73__asm__ (".desc ___crashreporter_info__, 0x10");74#endif7576void77apple_visual_create_pfobj(CGLPixelFormatObj * pfobj, const struct glx_config * mode,78bool * double_buffered, bool * uses_stereo,79bool offscreen)80{81CGLPixelFormatAttribute attr[MAX_ATTR];82int numattr = 0;83GLint vsref = 0;84CGLError error = 0;85bool use_core_profile = env_var_as_boolean("LIBGL_PROFILE_CORE", false);8687if (offscreen) {88apple_glx_diagnostic89("offscreen rendering enabled. Using kCGLPFAOffScreen\n");9091attr[numattr++] = kCGLPFAOffScreen;92}93else if (env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false)) {94apple_glx_diagnostic95("Software rendering requested. Using kCGLRendererGenericFloatID.\n");96attr[numattr++] = kCGLPFARendererID;97attr[numattr++] = kCGLRendererGenericFloatID;98}99else if (env_var_as_boolean("LIBGL_ALLOW_SOFTWARE", false)) {100apple_glx_diagnostic101("Software rendering is not being excluded. Not using kCGLPFAAccelerated.\n");102}103else {104attr[numattr++] = kCGLPFAAccelerated;105}106107/*108* The program chose a config based on the fbconfigs or visuals.109* Those are based on the attributes from CGL, so we probably110* do want the closest match for the color, depth, and accum.111*/112attr[numattr++] = kCGLPFAClosestPolicy;113114if (mode->stereoMode) {115attr[numattr++] = kCGLPFAStereo;116*uses_stereo = true;117}118else {119*uses_stereo = false;120}121122if (!offscreen && mode->doubleBufferMode) {123attr[numattr++] = kCGLPFADoubleBuffer;124*double_buffered = true;125}126else {127*double_buffered = false;128}129130attr[numattr++] = kCGLPFAColorSize;131attr[numattr++] = mode->redBits + mode->greenBits + mode->blueBits;132attr[numattr++] = kCGLPFAAlphaSize;133attr[numattr++] = mode->alphaBits;134135if ((mode->accumRedBits + mode->accumGreenBits + mode->accumBlueBits) > 0) {136attr[numattr++] = kCGLPFAAccumSize;137attr[numattr++] = mode->accumRedBits + mode->accumGreenBits +138mode->accumBlueBits + mode->accumAlphaBits;139}140141if (mode->depthBits > 0) {142attr[numattr++] = kCGLPFADepthSize;143attr[numattr++] = mode->depthBits;144}145146if (mode->stencilBits > 0) {147attr[numattr++] = kCGLPFAStencilSize;148attr[numattr++] = mode->stencilBits;149}150151if (mode->sampleBuffers > 0) {152attr[numattr++] = kCGLPFAMultisample;153attr[numattr++] = kCGLPFASampleBuffers;154attr[numattr++] = mode->sampleBuffers;155attr[numattr++] = kCGLPFASamples;156attr[numattr++] = mode->samples;157}158159/* Debugging support for Core profiles to support newer versions of OpenGL */160if (use_core_profile) {161attr[numattr++] = kCGLPFAOpenGLProfile;162attr[numattr++] = kCGLOGLPVersion_3_2_Core;163}164165attr[numattr++] = 0;166167assert(numattr < MAX_ATTR);168169error = apple_cgl.choose_pixel_format(attr, pfobj, &vsref);170171if ((error == kCGLBadAttribute || vsref == 0) && use_core_profile) {172apple_glx_diagnostic173("Trying again without CoreProfile: error=%s, vsref=%d\n", apple_cgl.error_string(error), vsref);174175if (!error)176apple_cgl.destroy_pixel_format(*pfobj);177178numattr -= 3;179attr[numattr++] = 0;180181error = apple_cgl.choose_pixel_format(attr, pfobj, &vsref);182}183184if (error) {185snprintf(__crashreporter_info_buff__, sizeof(__crashreporter_info_buff__),186"CGLChoosePixelFormat error: %s\n", apple_cgl.error_string(error));187fprintf(stderr, "%s", __crashreporter_info_buff__);188abort();189}190191if (!*pfobj) {192snprintf(__crashreporter_info_buff__, sizeof(__crashreporter_info_buff__),193"No matching pixelformats found, perhaps try setting LIBGL_ALLOW_SOFTWARE=true\n");194fprintf(stderr, "%s", __crashreporter_info_buff__);195abort();196}197}198199200