OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/gl/gl_context_nsview.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #import <AppKit/NSOpenGL.h> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "ui/gfx/gl/gl_surface_nsview.h" |
| 13 |
| 14 namespace gfx { |
| 15 |
| 16 GLContextNSView::GLContextNSView(GLShareGroup* group) |
| 17 : GLContext(group) { |
| 18 } |
| 19 |
| 20 GLContextNSView::~GLContextNSView() { |
| 21 } |
| 22 |
| 23 bool GLContextNSView::Initialize(GLSurface* surface, |
| 24 GpuPreference gpu_preference) { |
| 25 DCHECK(!context_) << "NSGLContext was previously initialized."; |
| 26 gpu_preference_ = gpu_preference; |
| 27 |
| 28 std::vector<NSOpenGLPixelFormatAttribute> attributes; |
| 29 attributes.push_back(NSOpenGLPFAAccelerated); |
| 30 attributes.push_back(NSOpenGLPFADoubleBuffer); |
| 31 attributes.push_back(0); |
| 32 |
| 33 scoped_nsobject<NSOpenGLPixelFormat> pixel_format; |
| 34 pixel_format.reset([[NSOpenGLPixelFormat alloc] |
| 35 initWithAttributes:&attributes.front()]); |
| 36 if (!pixel_format) { |
| 37 LOG(ERROR) << "NSOpenGLPixelFormat initWithAttributes failed."; |
| 38 return false; |
| 39 } |
| 40 |
| 41 context_.reset([[NSOpenGLContext alloc] initWithFormat:pixel_format |
| 42 shareContext:nil]); |
| 43 if (!context_) { |
| 44 LOG(ERROR) << "NSOpenGLContext initWithFormat failed"; |
| 45 return false; |
| 46 } |
| 47 |
| 48 // Allow the surface to call back when in need of |FlushBuffer|. |
| 49 static_cast<GLSurfaceNSView*>(surface)->SetGLContext(this); |
| 50 |
| 51 return true; |
| 52 } |
| 53 |
| 54 void GLContextNSView::Destroy() { |
| 55 context_.reset(nil); |
| 56 } |
| 57 |
| 58 bool GLContextNSView::MakeCurrent(GLSurface* surface) { |
| 59 [context_ makeCurrentContext]; |
| 60 return true; |
| 61 } |
| 62 |
| 63 void GLContextNSView::ReleaseCurrent(GLSurface* surface) { |
| 64 [NSOpenGLContext clearCurrentContext]; |
| 65 } |
| 66 |
| 67 bool GLContextNSView::IsCurrent(GLSurface* surface) { |
| 68 return context_ == [NSOpenGLContext currentContext]; |
| 69 } |
| 70 |
| 71 void* GLContextNSView::GetHandle() { |
| 72 return context_; |
| 73 } |
| 74 |
| 75 void GLContextNSView::SetSwapInterval(int interval) { |
| 76 DCHECK(interval == 0 || interval == 1); |
| 77 GLint swap = interval; |
| 78 [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval]; |
| 79 } |
| 80 |
| 81 void GLContextNSView::FlushBuffer() { |
| 82 [context_ flushBuffer]; |
| 83 } |
| 84 |
| 85 } // namespace gfx |
OLD | NEW |