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/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/debug/trace_event.h" | |
13 #include "base/logging.h" | |
14 #include "ui/gfx/gl/gl_surface_nsview.h" | |
15 | |
16 namespace gfx { | |
17 | |
18 GLContextNSView::GLContextNSView(GLShareGroup* group) | |
19 : GLContext(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 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 SetCurrent(this, surface); | |
68 | |
69 if (!surface->OnMakeCurrent(this)) { | |
70 LOG(ERROR) << "Unable to make gl context current."; | |
71 return false; | |
72 } | |
73 | |
74 return true; | |
75 } | |
76 | |
77 void GLContextNSView::ReleaseCurrent(GLSurface* surface) { | |
78 [NSOpenGLContext clearCurrentContext]; | |
79 } | |
80 | |
81 bool GLContextNSView::IsCurrent(GLSurface* surface) { | |
82 return context_ == [NSOpenGLContext currentContext]; | |
83 } | |
84 | |
85 void* GLContextNSView::GetHandle() { | |
86 return context_; | |
87 } | |
88 | |
89 void GLContextNSView::SetSwapInterval(int interval) { | |
90 DCHECK(interval == 0 || interval == 1); | |
91 GLint swap = interval; | |
92 [context_ setValues:&swap forParameter:NSOpenGLCPSwapInterval]; | |
93 } | |
94 | |
95 void GLContextNSView::FlushBuffer() { | |
96 [context_ flushBuffer]; | |
97 } | |
98 | |
99 } // namespace gfx | |
OLD | NEW |