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.
5 #include "gpu/command_buffer/service/gl_context_virtual.h"
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"
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
),
23 gfx::Display
* GLContextVirtual::display() {
27 bool GLContextVirtual::Initialize(
28 gfx::GLSurface
* compatible_surface
, gfx::GpuPreference gpu_preference
) {
29 SetGLStateRestorer(new GLStateRestorerImpl(decoder_
));
31 display_
= static_cast<gfx::Display
*>(compatible_surface
->GetDisplay());
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)";
45 shared_context_
->SetupForVirtualization();
46 shared_context_
->MakeVirtuallyCurrent(this, compatible_surface
);
50 void GLContextVirtual::Destroy() {
51 shared_context_
->OnReleaseVirtuallyCurrent(this);
52 shared_context_
= NULL
;
56 bool GLContextVirtual::MakeCurrent(gfx::GLSurface
* surface
) {
58 return shared_context_
->MakeVirtuallyCurrent(this, surface
);
60 LOG(ERROR
) << "Trying to make virtual context current without decoder.";
64 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface
* surface
) {
65 if (IsCurrent(surface
)) {
66 shared_context_
->OnReleaseVirtuallyCurrent(this);
67 shared_context_
->ReleaseCurrent(surface
);
71 bool GLContextVirtual::IsCurrent(gfx::GLSurface
* surface
) {
72 // If it's a real surface it needs to be current.
74 !surface
->IsOffscreen())
75 return shared_context_
->IsCurrent(surface
);
77 // Otherwise, only insure the context itself is current.
78 return shared_context_
->IsCurrent(NULL
);
81 void* GLContextVirtual::GetHandle() {
82 return shared_context_
->GetHandle();
85 void GLContextVirtual::OnSetSwapInterval(int interval
) {
86 shared_context_
->SetSwapInterval(interval
);
89 std::string
GLContextVirtual::GetExtensions() {
90 return shared_context_
->GetExtensions();
93 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes
) {
94 return shared_context_
->GetTotalGpuMemory(bytes
);
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();
104 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
105 return shared_context_
->WasAllocatedUsingRobustnessExtension();
108 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
109 shared_context_
->SetUnbindFboOnMakeCurrent();
112 GLContextVirtual::~GLContextVirtual() {