Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_context_nsview.mm
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   if (!InitializeDynamicBindings()) {
70     ReleaseCurrent(surface);
71     return false;
72   }
73
74   if (!surface->OnMakeCurrent(this)) {
75     LOG(ERROR) << "Unable to make gl context current.";
76     return false;
77   }
78
79   return true;
80 }
81
82 void GLContextNSView::ReleaseCurrent(GLSurface* surface) {
83   [NSOpenGLContext clearCurrentContext];
84 }
85
86 bool GLContextNSView::IsCurrent(GLSurface* surface) {
87   return context_ == [NSOpenGLContext currentContext];
88 }
89
90 void* GLContextNSView::GetHandle() {
91   return context_;
92 }
93
94 void GLContextNSView::SetSwapInterval(int interval) {
95   DCHECK(interval == 0 || interval == 1);
96   GLint swap = interval;
97   [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval];
98 }
99
100 void GLContextNSView::FlushBuffer() {
101   [context_ flushBuffer];
102 }
103
104 }  // namespace gfx