| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #ifdef __APPLE__ |
|---|
| 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::notify(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::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 78 | return false; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | AGLContext sharedContext = NULL; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | GraphicsWindowCarbon* graphicsWindowCarbon = dynamic_cast<GraphicsWindowCarbon*>(_traits->sharedContext); |
|---|
| 85 | if (graphicsWindowCarbon) |
|---|
| 86 | { |
|---|
| 87 | sharedContext = graphicsWindowCarbon->getAGLContext(); |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | { |
|---|
| 91 | PixelBufferCarbon* pixelBufferCarbon = dynamic_cast<PixelBufferCarbon*>(_traits->sharedContext); |
|---|
| 92 | if (pixelBufferCarbon) |
|---|
| 93 | { |
|---|
| 94 | sharedContext = pixelBufferCarbon->getAGLContext(); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | _context = aglCreateContext (_pixelformat, sharedContext); |
|---|
| 99 | |
|---|
| 100 | if (!_context) { |
|---|
| 101 | osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | _realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer)); |
|---|
| 108 | if (!_realized) { |
|---|
| 109 | osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | makeCurrentImplementation(); |
|---|
| 113 | |
|---|
| 114 | _realized = aglSetPBuffer(_context, _pbuffer, _traits->face, _traits->level, 0); |
|---|
| 115 | if (!_realized) { |
|---|
| 116 | osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglSetPBuffer failed! " << aglErrorString(aglGetError()) << std::endl; |
|---|
| 117 | } |
|---|
| 118 | return _realized; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void PixelBufferCarbon::closeImplementation() |
|---|
| 122 | { |
|---|
| 123 | if (_pbuffer) aglDestroyPBuffer(_pbuffer); |
|---|
| 124 | if (_context) aglDestroyContext(_context); |
|---|
| 125 | if (_pixelformat) aglDestroyPixelFormat(_pixelformat); |
|---|
| 126 | _valid = _realized = false; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | bool PixelBufferCarbon::makeCurrentImplementation() |
|---|
| 132 | { |
|---|
| 133 | return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | bool PixelBufferCarbon::makeContextCurrentImplementation(GraphicsContext* ) { |
|---|
| 139 | return makeCurrentImplementation(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | bool PixelBufferCarbon::releaseContextImplementation() |
|---|
| 144 | { |
|---|
| 145 | return (aglSetCurrentContext(NULL) == GL_TRUE); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | void PixelBufferCarbon::bindPBufferToTextureImplementation( GLenum buffer ){ |
|---|
| 152 | |
|---|
| 153 | osg::notify(osg::NOTICE)<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | void PixelBufferCarbon::swapBuffersImplementation() |
|---|
| 159 | { |
|---|
| 160 | aglSwapBuffers(_context); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | PixelBufferCarbon::~PixelBufferCarbon() |
|---|
| 165 | { |
|---|
| 166 | close(true); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | #endif |
|---|