| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #if defined (__APPLE__) && (!__LP64__) |
|---|
| 11 | |
|---|
| 12 | #include <osg/observer_ptr> |
|---|
| 13 | #include <osgViewer/api/Carbon/PixelBufferCarbon> |
|---|
| 14 | #include <osgViewer/api/Carbon/GraphicsWindowCarbon> |
|---|
| 15 | #include <Carbon/Carbon.h> |
|---|
| 16 | #include <OpenGL/OpenGL.h> |
|---|
| 17 | using namespace osgViewer; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | AGLPixelFormat PixelBufferCarbon::createPixelFormat(osg::GraphicsContext::Traits* traits) { |
|---|
| 24 | |
|---|
| 25 | std::vector<GLint> attributes; |
|---|
| 26 | |
|---|
| 27 | attributes.push_back(AGL_NO_RECOVERY); |
|---|
| 28 | attributes.push_back(AGL_RGBA); |
|---|
| 29 | if (!traits->pbuffer) |
|---|
| 30 | attributes.push_back(AGL_COMPLIANT); |
|---|
| 31 | else |
|---|
| 32 | attributes.push_back(AGL_CLOSEST_POLICY); |
|---|
| 33 | |
|---|
| 34 | if (traits->doubleBuffer) attributes.push_back(AGL_DOUBLEBUFFER); |
|---|
| 35 | if (traits->quadBufferStereo) attributes.push_back(AGL_STEREO); |
|---|
| 36 | |
|---|
| 37 | attributes.push_back(AGL_RED_SIZE); attributes.push_back(traits->red); |
|---|
| 38 | attributes.push_back(AGL_GREEN_SIZE); attributes.push_back(traits->green); |
|---|
| 39 | attributes.push_back(AGL_BLUE_SIZE); attributes.push_back(traits->blue); |
|---|
| 40 | attributes.push_back(AGL_DEPTH_SIZE); attributes.push_back(traits->depth); |
|---|
| 41 | |
|---|
| 42 | if (traits->alpha) { attributes.push_back(AGL_ALPHA_SIZE); attributes.push_back(traits->alpha); } |
|---|
| 43 | |
|---|
| 44 | if (traits->stencil) { attributes.push_back(AGL_STENCIL_SIZE); attributes.push_back(traits->stencil); } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | #if defined(AGL_SAMPLE_BUFFERS_ARB) && defined (AGL_SAMPLES_ARB) |
|---|
| 50 | |
|---|
| 51 | if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLE_BUFFERS_ARB); attributes.push_back(traits->sampleBuffers); } |
|---|
| 52 | if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLES_ARB); attributes.push_back(traits->samples); } |
|---|
| 53 | |
|---|
| 54 | #endif |
|---|
| 55 | attributes.push_back(AGL_NONE); |
|---|
| 56 | |
|---|
| 57 | return aglChoosePixelFormat(NULL, 0, &(attributes.front())); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | void PixelBufferCarbon::init() |
|---|
| 62 | { |
|---|
| 63 | _context = NULL; |
|---|
| 64 | _pixelformat = PixelBufferCarbon::createPixelFormat(_traits.get()); |
|---|
| 65 | if (!_pixelformat) |
|---|
| 66 | OSG_WARN << "PixelBufferCarbon::init could not create a valid pixelformat" << std::endl; |
|---|
| 67 | _valid = (_pixelformat != NULL); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | bool PixelBufferCarbon::realizeImplementation() |
|---|
| 75 | { |
|---|
| 76 | if (!_valid) { |
|---|
| 77 | OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 78 | return false; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | AGLContext sharedContext = NULL; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | GraphicsHandleCarbon* graphicsHandleCarbon = dynamic_cast<GraphicsHandleCarbon*>(_traits->sharedContext); |
|---|
| 85 | if (graphicsHandleCarbon) |
|---|
| 86 | { |
|---|
| 87 | sharedContext = graphicsHandleCarbon->getAGLContext(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | _context = aglCreateContext (_pixelformat, sharedContext); |
|---|
| 91 | |
|---|
| 92 | if (!_context) { |
|---|
| 93 | OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 94 | return false; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | _realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer)); |
|---|
| 100 | if (!_realized) { |
|---|
| 101 | OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | makeCurrentImplementation(); |
|---|
| 105 | |
|---|
| 106 | _realized = aglSetPBuffer(_context, _pbuffer, _traits->face, _traits->level, 0); |
|---|
| 107 | if (!_realized) { |
|---|
| 108 | OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglSetPBuffer failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 109 | } |
|---|
| 110 | return _realized; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void PixelBufferCarbon::closeImplementation() |
|---|
| 114 | { |
|---|
| 115 | if (_pbuffer) aglDestroyPBuffer(_pbuffer); |
|---|
| 116 | if (_context) aglDestroyContext(_context); |
|---|
| 117 | if (_pixelformat) aglDestroyPixelFormat(_pixelformat); |
|---|
| 118 | |
|---|
| 119 | _pbuffer = NULL; |
|---|
| 120 | _context = NULL; |
|---|
| 121 | _pixelformat = NULL; |
|---|
| 122 | |
|---|
| 123 | _valid = _realized = false; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | bool PixelBufferCarbon::makeCurrentImplementation() |
|---|
| 129 | { |
|---|
| 130 | return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | bool PixelBufferCarbon::makeContextCurrentImplementation(GraphicsContext* ) { |
|---|
| 136 | return makeCurrentImplementation(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | bool PixelBufferCarbon::releaseContextImplementation() |
|---|
| 141 | { |
|---|
| 142 | return (aglSetCurrentContext(NULL) == GL_TRUE); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | void PixelBufferCarbon::bindPBufferToTextureImplementation( GLenum buffer ){ |
|---|
| 149 | |
|---|
| 150 | OSG_NOTICE<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | void PixelBufferCarbon::swapBuffersImplementation() |
|---|
| 156 | { |
|---|
| 157 | aglSwapBuffers(_context); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | PixelBufferCarbon::~PixelBufferCarbon() |
|---|
| 162 | { |
|---|
| 163 | close(true); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | #endif |
|---|