OLD | NEW |
| (Empty) |
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 ScopedReleaseCurrent release_current; | |
59 TRACE_EVENT0("gpu", "GLContextNSView::MakeCurrent"); | |
60 AcceleratedWidget view = | |
61 static_cast<AcceleratedWidget>(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 | |
68 SetRealGLApi(); | |
69 SetCurrent(surface); | |
70 if (!InitializeDynamicBindings()) { | |
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 release_current.Cancel(); | |
80 return true; | |
81 } | |
82 | |
83 void GLContextNSView::ReleaseCurrent(GLSurface* surface) { | |
84 [NSOpenGLContext clearCurrentContext]; | |
85 } | |
86 | |
87 bool GLContextNSView::IsCurrent(GLSurface* surface) { | |
88 return context_ == [NSOpenGLContext currentContext]; | |
89 } | |
90 | |
91 void* GLContextNSView::GetHandle() { | |
92 return context_; | |
93 } | |
94 | |
95 void GLContextNSView::SetSwapInterval(int interval) { | |
96 DCHECK(interval == 0 || interval == 1); | |
97 GLint swap = interval; | |
98 [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval]; | |
99 } | |
100 | |
101 void GLContextNSView::FlushBuffer() { | |
102 [context_ flushBuffer]; | |
103 } | |
104 | |
105 } // namespace gfx | |
OLD | NEW |