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 #import <AppKit/NSView.h> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "ui/gfx/gl/gl_surface_nsview.h" |
| 14 |
| 15 namespace gfx { |
| 16 |
| 17 GLContextNSView::GLContextNSView(GLShareGroup* group) |
| 18 : GLContext(group) { |
| 19 } |
| 20 |
| 21 GLContextNSView::~GLContextNSView() { |
| 22 } |
| 23 |
| 24 bool GLContextNSView::Initialize(GLSurface* surface, |
| 25 GpuPreference gpu_preference) { |
| 26 DCHECK(!context_) << "NSGLContext was previously initialized."; |
| 27 gpu_preference_ = gpu_preference; |
| 28 |
| 29 std::vector<NSOpenGLPixelFormatAttribute> attributes; |
| 30 attributes.push_back(NSOpenGLPFAAccelerated); |
| 31 attributes.push_back(NSOpenGLPFADoubleBuffer); |
| 32 attributes.push_back(0); |
| 33 |
| 34 scoped_nsobject<NSOpenGLPixelFormat> pixel_format; |
| 35 pixel_format.reset([[NSOpenGLPixelFormat alloc] |
| 36 initWithAttributes:&attributes.front()]); |
| 37 if (!pixel_format) { |
| 38 LOG(ERROR) << "NSOpenGLPixelFormat initWithAttributes failed."; |
| 39 return false; |
| 40 } |
| 41 |
| 42 context_.reset([[NSOpenGLContext alloc] initWithFormat:pixel_format |
| 43 shareContext:nil]); |
| 44 if (!context_) { |
| 45 LOG(ERROR) << "NSOpenGLContext initWithFormat failed"; |
| 46 return false; |
| 47 } |
| 48 |
| 49 // Allow the surface to call back when in need of |FlushBuffer|. |
| 50 static_cast<GLSurfaceNSView*>(surface)->SetGLContext(this); |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 void GLContextNSView::Destroy() { |
| 56 context_.reset(nil); |
| 57 } |
| 58 |
| 59 bool GLContextNSView::MakeCurrent(GLSurface* surface) { |
| 60 PluginWindowHandle view = |
| 61 static_cast<PluginWindowHandle>(surface->GetHandle()); |
| 62 // Only set the context's view if the view is parented. |
| 63 // I.e. it is a valid drawable. |
| 64 if ([view window]) |
| 65 [context_ setView:view]; |
| 66 [context_ makeCurrentContext]; |
| 67 return true; |
| 68 } |
| 69 |
| 70 void GLContextNSView::ReleaseCurrent(GLSurface* surface) { |
| 71 [NSOpenGLContext clearCurrentContext]; |
| 72 } |
| 73 |
| 74 bool GLContextNSView::IsCurrent(GLSurface* surface) { |
| 75 return context_ == [NSOpenGLContext currentContext]; |
| 76 } |
| 77 |
| 78 void* GLContextNSView::GetHandle() { |
| 79 return context_; |
| 80 } |
| 81 |
| 82 void GLContextNSView::SetSwapInterval(int interval) { |
| 83 DCHECK(interval == 0 || interval == 1); |
| 84 GLint swap = interval; |
| 85 [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval]; |
| 86 } |
| 87 |
| 88 void GLContextNSView::FlushBuffer() { |
| 89 [context_ flushBuffer]; |
| 90 } |
| 91 |
| 92 } // namespace gfx |
OLD | NEW |