📄 macvideo.cpp
字号:
/* Copyright 2005-2007 Intel Corporation. All Rights Reserved. This file is part of Threading Building Blocks. Threading Building Blocks is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. Threading Building Blocks is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Threading Building Blocks; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA As a special exception, you may use this file as part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License.*//* * Based on "OpenGL Image" example from http://developer.apple.com/samplecode/OpenGL_Image/ */#include "video.h"#include <sched.h>#include <sys/time.h>#include <stdio.h>#include <string.h>#include <pthread.h>#include <AvailabilityMacros.h>#undef DEPRECATED_ATTRIBUTE#define DEPRECATED_ATTRIBUTE#include <Carbon/Carbon.h>#include <AGL/agl.h>#include <OpenGL/gl.h> // for OpenGL API#include <OpenGL/glext.h> // for OpenGL extension support unsigned int * g_pImg = 0;int g_sizex, g_sizey;WindowRef g_window = 0;static video * g_video = 0;static int g_fps = 0;struct timeval g_time;static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;static OSStatus AppEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon );WindowRef HandleNew();static OSStatus WindowEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon );static IBNibRef sNibRef;//-------------------------------------------------------------------------------------------- // structure for creating a fullscreen contextstruct structGLInfo // storage for setup info{ SInt16 width; // input: width of drawable (screen width in full screen mode), return: actual width allocated SInt16 height; // input: height of drawable (screen height in full screen mode), return: actual height allocated UInt32 pixelDepth; // input: requested pixel depth Boolean fDepthMust; // input: pixel depth must be set (if false then current depth will be used if able) Boolean fAcceleratedMust; // input: must renderer be accelerated? GLint aglAttributes[64]; // input: pixel format attributes always required (reset to what was actually allocated) SInt32 VRAM; // input: minimum VRAM; output: actual (if successful otherwise input) SInt32 textureRAM; // input: amount of texture RAM required on card; output: same (used in allcoation to ensure enough texture AGLPixelFormat fmt; // input: none; output pixel format...};typedef struct structGLInfo structGLInfo;typedef struct structGLInfo * pstructGLInfo;// structure for creating a context from a windowstruct structGLWindowInfo // storage for setup info{ Boolean fAcceleratedMust; // input: must renderer be accelerated? GLint aglAttributes[64]; // input: pixel format attributes always required (reset to what was actually allocated) SInt32 VRAM; // input: minimum VRAM; output: actual (if successful otherwise input) SInt32 textureRAM; // input: amount of texture RAM required on card; output: same (used in allcoation to ensure enough texture AGLPixelFormat fmt; // input: none; output pixel format... Boolean fDraggable; // input: is window going to be dragable, // if so renderer check (accel, VRAM, textureRAM) will look at all renderers vice just the current one // if window is not dragable renderer check will either check the single device or short // circuit to software if window spans multiple devices // software renderer is consider to have unlimited VRAM, unlimited textureRAM and to not be accelerated};typedef struct structGLWindowInfo structGLWindowInfo;typedef struct structGLWindowInfo * pstructGLWindowInfo;//-------------------------------------------------------------------------------------------- struct recGLCap // structure to store minimum OpenGL capabilites across all displays and GPUs{ Boolean f_ext_texture_rectangle; // is texture rectangle extension supported Boolean f_ext_client_storage; // is client storage extension supported Boolean f_ext_packed_pixel; // is packed pixel extension supported Boolean f_ext_texture_edge_clamp; // is SGI texture edge clamp extension supported Boolean f_gl_texture_edge_clamp; // is OpenGL texture edge clamp support (1.2+) unsigned long edgeClampParam; // the param that is passed to the texturing parmeteres long maxTextureSize; // the minimum max texture size across all GPUs long maxNOPTDTextureSize; // the minimum max texture size across all GPUs that support non-power of two texture dimensions};typedef struct recGLCap recGLCap;typedef recGLCap * pRecGLCap;struct recImage // OpenGL and image information associated with each window{ // genric OpenGL stuff structGLWindowInfo glInfo; // gl info used with SetupGL to build context AGLContext aglContext; // the OpenGL context (read: state) GLuint fontList; // the display list storing the bitmap font created for the context to display info Boolean fAGPTexturing; // 10.1+ only: texture from AGP memory without loading to GPU // texture display stuff Boolean fNPOTTextures; // are we using Non-Power Of Two (NPOT) textures? Boolean fTileTextures; // are multiple tiled textures used to display image? Boolean fOverlapTextures; // do tiled textures overlapped to create correct filtering between tiles? (only applies if using tiled textures) Boolean fClientTextures; // 10.1+ only: texture from client memory unsigned char * pImageBuffer; // image buffer that contains data for image (disposed after loading into texture if not using client textures) long imageWidth; // height of orginal image long imageHeight; // width of orginal image float imageAspect; // width / height or aspect ratio of orginal image long imageDepth; // depth of image (after loading into gworld, will be either 32 or 16 bits) long textureX; // number of horizontal textures long textureY; // number of vertical textures long maxTextureSize; // max texture size for image GLuint * pTextureName; // array for texture names (# = textureX * textureY) long textureWidth; // total width of texels with cover image (including any border on image, but not internal texture overlaps) long textureHeight; // total height of texels with cover image (including any border on image, but not internal texture overlaps) float zoomX; // zoom from on texel = one pixel is 1.0 float zoomY; // zoom from on texel = one pixel is 1.0 };typedef struct recImage recImage; // typedef for easy declarationtypedef recImage * pRecImage; // pointer type// ==================================// public function declarations -------------------------------------// Destroys drawable and context// Ouputs: *paglDraw, *paglContext should be 0 on exit// destorys a context that was associated with an existing window, window is left intactedOSStatus DestroyGLFromWindow (AGLContext* paglContext, pstructGLWindowInfo pcontextInfo);short FindGDHandleFromWindow (WindowPtr pWindow, GDHandle * phgdOnThisDevice);// disposes OpenGL context, and associated texture listOSStatus DisposeGLForWindow (WindowRef window);// builds the GL context and associated state for the window// loads image into a texture or textures// disposes of GWorld and image buffer when finished loading texturesOSStatus BuildGLForWindow (WindowRef window);// Handle updating context for window moves and resizingOSStatus ResizeMoveGLWindow (WindowRef window);// main GL drawing routine, should be valid window passed in (will setupGL if require). Draw imagevoid DrawGL (WindowRef window);pRecGLCap gpOpenGLCaps; // prototypes (internal/private) --------------------------------------------static Boolean CheckRenderer (GDHandle hGD, long *VRAM, long *textureRAM, GLint* , Boolean fAccelMust);static Boolean CheckAllDeviceRenderers (long* pVRAM, long* pTextureRAM, GLint* pDepthSizeSupport, Boolean fAccelMust);static void DumpCurrent (AGLDrawable* paglDraw, AGLContext* paglContext, pstructGLInfo pcontextInfo);static OSStatus BuildGLonWindow (WindowPtr pWindow, AGLContext* paglContext, pstructGLWindowInfo pcontextInfo, AGLContext aglShareContext);static long GetNextTextureSize (long textureDimension, long maxTextureSize, Boolean textureRectangle);static long GetTextureNumFromTextureDim (long textureDimension, long maxTextureSize, Boolean texturesOverlap, Boolean textureRectangle);// ----------------------------------------------------------------------------------------// functions (internal/private) ---------------------------------------------#pragma mark -// --------------------------------------------------------------------------// central error reportingvoid ReportErrorNum (char * strError, long numError){ char errMsgPStr [257]; errMsgPStr[0] = (char)snprintf (errMsgPStr+1, 255, "%s %ld (0x%lx)\n", strError, numError, numError); // ensure we are faded in DebugStr ( (ConstStr255Param) errMsgPStr );}// --------------------------------------------------------------------------void ReportError (char * strError){ char errMsgPStr [257]; errMsgPStr[0] = (char)snprintf (errMsgPStr+1, 255, "%s\n", strError); // ensure we are faded in DebugStr ( (ConstStr255Param) errMsgPStr );}//-----------------------------------------------------------------------------------------------------------------------// if error dump agl errors to debugger string, return errorOSStatus aglReportError (void){ GLenum err = aglGetError(); if (AGL_NO_ERROR != err) ReportError ((char *)aglErrorString(err)); // ensure we are returning an OSStatus noErr if no error condition if (err == AGL_NO_ERROR) return noErr; else return (OSStatus) err;}//-----------------------------------------------------------------------------------------------------------------------// if error dump gl errors to debugger string, return errorOSStatus glReportError (void){ GLenum err = glGetError(); switch (err) { case GL_NO_ERROR: break; case GL_INVALID_ENUM: ReportError ("GL Error: Invalid enumeration"); break; case GL_INVALID_VALUE: ReportError ("GL Error: Invalid value"); break; case GL_INVALID_OPERATION: ReportError ("GL Error: Invalid operation"); break; case GL_STACK_OVERFLOW: ReportError ("GL Error: Stack overflow"); break; case GL_STACK_UNDERFLOW: ReportError ("GL Error: Stack underflow"); break; case GL_OUT_OF_MEMORY: ReportError ("GL Error: Out of memory"); break; } // ensure we are returning an OSStatus noErr if no error condition if (err == GL_NO_ERROR) return noErr; else return (OSStatus) err;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -