1 // Copyright (c) 2012 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.
5 #include "ui/gl/gl_context_nsview.h"
9 #import <AppKit/NSOpenGL.h>
10 #import <AppKit/NSView.h>
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "ui/gl/gl_surface_nsview.h"
18 GLContextNSView::GLContextNSView(GLShareGroup* group)
22 GLContextNSView::~GLContextNSView() {
25 bool GLContextNSView::Initialize(GLSurface* surface,
26 GpuPreference gpu_preference) {
27 DCHECK(!context_) << "NSGLContext was previously initialized.";
28 gpu_preference_ = gpu_preference;
30 std::vector<NSOpenGLPixelFormatAttribute> attributes;
31 attributes.push_back(NSOpenGLPFAAccelerated);
32 attributes.push_back(NSOpenGLPFADoubleBuffer);
33 attributes.push_back(0);
35 scoped_nsobject<NSOpenGLPixelFormat> pixel_format;
36 pixel_format.reset([[NSOpenGLPixelFormat alloc]
37 initWithAttributes:&attributes.front()]);
39 LOG(ERROR) << "NSOpenGLPixelFormat initWithAttributes failed.";
43 context_.reset([[NSOpenGLContext alloc] initWithFormat:pixel_format
46 LOG(ERROR) << "NSOpenGLContext initWithFormat failed";
53 void GLContextNSView::Destroy() {
57 bool GLContextNSView::MakeCurrent(GLSurface* surface) {
58 TRACE_EVENT0("gpu", "GLContextNSView::MakeCurrent");
59 AcceleratedWidget view =
60 static_cast<AcceleratedWidget>(surface->GetHandle());
61 // Only set the context's view if the view is parented.
62 // I.e. it is a valid drawable.
64 [context_ setView:view];
65 [context_ makeCurrentContext];
67 SetCurrent(this, surface);
69 if (!surface->OnMakeCurrent(this)) {
70 LOG(ERROR) << "Unable to make gl context current.";
77 void GLContextNSView::ReleaseCurrent(GLSurface* surface) {
78 [NSOpenGLContext clearCurrentContext];
81 bool GLContextNSView::IsCurrent(GLSurface* surface) {
82 return context_ == [NSOpenGLContext currentContext];
85 void* GLContextNSView::GetHandle() {
89 void GLContextNSView::SetSwapInterval(int interval) {
90 DCHECK(interval == 0 || interval == 1);
91 GLint swap = interval;
92 [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval];
95 void GLContextNSView::FlushBuffer() {
96 [context_ flushBuffer];