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 "gpu/command_buffer/service/gl_context_virtual.h"
6
7 #include "gpu/command_buffer/service/gl_state_restorer_impl.h"
8 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
9 #include "ui/gl/gl_surface.h"
10
11 namespace gpu {
12
GLContextVirtual(gfx::GLShareGroup * share_group,gfx::GLContext * shared_context,base::WeakPtr<gles2::GLES2Decoder> decoder)13 GLContextVirtual::GLContextVirtual(
14 gfx::GLShareGroup* share_group,
15 gfx::GLContext* shared_context,
16 base::WeakPtr<gles2::GLES2Decoder> decoder)
17 : GLContext(share_group),
18 shared_context_(shared_context),
19 display_(NULL),
20 decoder_(decoder) {
21 }
22
display()23 gfx::Display* GLContextVirtual::display() {
24 return display_;
25 }
26
Initialize(gfx::GLSurface * compatible_surface,gfx::GpuPreference gpu_preference)27 bool GLContextVirtual::Initialize(
28 gfx::GLSurface* compatible_surface, gfx::GpuPreference gpu_preference) {
29 SetGLStateRestorer(new GLStateRestorerImpl(decoder_));
30
31 display_ = static_cast<gfx::Display*>(compatible_surface->GetDisplay());
32
33 // Virtual contexts obviously can't make a context that is compatible
34 // with the surface (the context already exists), but we do need to
35 // make a context current for SetupForVirtualization() below.
36 if (!IsCurrent(compatible_surface)) {
37 if (!shared_context_->MakeCurrent(compatible_surface)) {
38 // This is likely an error. The real context should be made as
39 // compatible with all required surfaces when it was created.
40 LOG(ERROR) << "Failed MakeCurrent(compatible_surface)";
41 return false;
42 }
43 }
44
45 shared_context_->SetupForVirtualization();
46 shared_context_->MakeVirtuallyCurrent(this, compatible_surface);
47 return true;
48 }
49
Destroy()50 void GLContextVirtual::Destroy() {
51 shared_context_->OnReleaseVirtuallyCurrent(this);
52 shared_context_ = NULL;
53 display_ = NULL;
54 }
55
MakeCurrent(gfx::GLSurface * surface)56 bool GLContextVirtual::MakeCurrent(gfx::GLSurface* surface) {
57 if (decoder_.get())
58 return shared_context_->MakeVirtuallyCurrent(this, surface);
59
60 LOG(ERROR) << "Trying to make virtual context current without decoder.";
61 return false;
62 }
63
ReleaseCurrent(gfx::GLSurface * surface)64 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface* surface) {
65 if (IsCurrent(surface)) {
66 shared_context_->OnReleaseVirtuallyCurrent(this);
67 shared_context_->ReleaseCurrent(surface);
68 }
69 }
70
IsCurrent(gfx::GLSurface * surface)71 bool GLContextVirtual::IsCurrent(gfx::GLSurface* surface) {
72 // If it's a real surface it needs to be current.
73 if (surface &&
74 !surface->IsOffscreen())
75 return shared_context_->IsCurrent(surface);
76
77 // Otherwise, only insure the context itself is current.
78 return shared_context_->IsCurrent(NULL);
79 }
80
GetHandle()81 void* GLContextVirtual::GetHandle() {
82 return shared_context_->GetHandle();
83 }
84
SetSwapInterval(int interval)85 void GLContextVirtual::SetSwapInterval(int interval) {
86 shared_context_->SetSwapInterval(interval);
87 }
88
GetExtensions()89 std::string GLContextVirtual::GetExtensions() {
90 return shared_context_->GetExtensions();
91 }
92
GetTotalGpuMemory(size_t * bytes)93 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes) {
94 return shared_context_->GetTotalGpuMemory(bytes);
95 }
96
SetSafeToForceGpuSwitch()97 void GLContextVirtual::SetSafeToForceGpuSwitch() {
98 // TODO(ccameron): This will not work if two contexts that disagree
99 // about whether or not forced gpu switching may be done both share
100 // the same underlying shared_context_.
101 return shared_context_->SetSafeToForceGpuSwitch();
102 }
103
WasAllocatedUsingRobustnessExtension()104 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
105 return shared_context_->WasAllocatedUsingRobustnessExtension();
106 }
107
SetUnbindFboOnMakeCurrent()108 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
109 shared_context_->SetUnbindFboOnMakeCurrent();
110 }
111
~GLContextVirtual()112 GLContextVirtual::~GLContextVirtual() {
113 Destroy();
114 }
115
116 } // namespace gpu
117