Home | History | Annotate | Download | only in gl
      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.
      4 
      5 #include "ui/gl/gl_context_nsview.h"
      6 
      7 #include <vector>
      8 
      9 #import <AppKit/NSOpenGL.h>
     10 #import <AppKit/NSView.h>
     11 
     12 #include "base/debug/trace_event.h"
     13 #include "base/logging.h"
     14 #include "ui/gl/gl_surface_nsview.h"
     15 
     16 namespace gfx {
     17 
     18 GLContextNSView::GLContextNSView(GLShareGroup* group)
     19     : GLContextReal(group) {
     20 }
     21 
     22 GLContextNSView::~GLContextNSView() {
     23 }
     24 
     25 bool GLContextNSView::Initialize(GLSurface* surface,
     26                                  GpuPreference gpu_preference) {
     27   DCHECK(!context_) << "NSGLContext was previously initialized.";
     28   gpu_preference_ = gpu_preference;
     29 
     30   std::vector<NSOpenGLPixelFormatAttribute> attributes;
     31   attributes.push_back(NSOpenGLPFAAccelerated);
     32   attributes.push_back(NSOpenGLPFADoubleBuffer);
     33   attributes.push_back(0);
     34 
     35   base::scoped_nsobject<NSOpenGLPixelFormat> pixel_format;
     36   pixel_format.reset([[NSOpenGLPixelFormat alloc]
     37                          initWithAttributes:&attributes.front()]);
     38   if (!pixel_format) {
     39     LOG(ERROR) << "NSOpenGLPixelFormat initWithAttributes failed.";
     40     return false;
     41   }
     42 
     43   context_.reset([[NSOpenGLContext alloc] initWithFormat:pixel_format
     44                                             shareContext:nil]);
     45   if (!context_) {
     46     LOG(ERROR) << "NSOpenGLContext initWithFormat failed";
     47     return false;
     48   }
     49 
     50   return true;
     51 }
     52 
     53 void GLContextNSView::Destroy() {
     54   context_.reset(nil);
     55 }
     56 
     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.
     63   if ([view window])
     64     [context_ setView:view];
     65   [context_ makeCurrentContext];
     66 
     67   SetRealGLApi();
     68   SetCurrent(surface);
     69 
     70   if (!surface->OnMakeCurrent(this)) {
     71     LOG(ERROR) << "Unable to make gl context current.";
     72     return false;
     73   }
     74 
     75   return true;
     76 }
     77 
     78 void GLContextNSView::ReleaseCurrent(GLSurface* surface) {
     79   [NSOpenGLContext clearCurrentContext];
     80 }
     81 
     82 bool GLContextNSView::IsCurrent(GLSurface* surface) {
     83   return context_ == [NSOpenGLContext currentContext];
     84 }
     85 
     86 void* GLContextNSView::GetHandle() {
     87   return context_;
     88 }
     89 
     90 void GLContextNSView::SetSwapInterval(int interval) {
     91   DCHECK(interval == 0 || interval == 1);
     92   GLint swap = interval;
     93   [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval];
     94 }
     95 
     96 void GLContextNSView::FlushBuffer() {
     97   [context_ flushBuffer];
     98 }
     99 
    100 }  // namespace gfx
    101