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 "base/callback.h"
8 #include "gpu/command_buffer/service/gl_state_restorer_impl.h"
9 #include "ui/gl/gl_gl_api_implementation.h"
10 #include "ui/gl/gl_surface.h"
11 #include "ui/gl/gpu_preference.h"
12 #include "ui/gl/gpu_timing.h"
16 GLContextVirtual::GLContextVirtual(
17 gfx::GLShareGroup
* share_group
,
18 gfx::GLContext
* shared_context
,
19 base::WeakPtr
<gles2::GLES2Decoder
> decoder
)
20 : GLContext(share_group
),
21 shared_context_(shared_context
),
25 bool GLContextVirtual::Initialize(
26 gfx::GLSurface
* compatible_surface
, gfx::GpuPreference gpu_preference
) {
27 SetGLStateRestorer(new GLStateRestorerImpl(decoder_
));
29 // Virtual contexts obviously can't make a context that is compatible
30 // with the surface (the context already exists), but we do need to
31 // make a context current for SetupForVirtualization() below.
32 if (!IsCurrent(compatible_surface
)) {
33 if (!shared_context_
->MakeCurrent(compatible_surface
)) {
34 // This is likely an error. The real context should be made as
35 // compatible with all required surfaces when it was created.
36 LOG(ERROR
) << "Failed MakeCurrent(compatible_surface)";
41 shared_context_
->SetupForVirtualization();
42 shared_context_
->MakeVirtuallyCurrent(this, compatible_surface
);
46 void GLContextVirtual::Destroy() {
47 shared_context_
->OnReleaseVirtuallyCurrent(this);
48 shared_context_
= NULL
;
51 bool GLContextVirtual::MakeCurrent(gfx::GLSurface
* surface
) {
53 return shared_context_
->MakeVirtuallyCurrent(this, surface
);
55 LOG(ERROR
) << "Trying to make virtual context current without decoder.";
59 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface
* surface
) {
60 if (IsCurrent(surface
)) {
61 shared_context_
->OnReleaseVirtuallyCurrent(this);
62 shared_context_
->ReleaseCurrent(surface
);
66 bool GLContextVirtual::IsCurrent(gfx::GLSurface
* surface
) {
67 // If it's a real surface it needs to be current.
69 !surface
->IsOffscreen())
70 return shared_context_
->IsCurrent(surface
);
72 // Otherwise, only insure the context itself is current.
73 return shared_context_
->IsCurrent(NULL
);
76 void* GLContextVirtual::GetHandle() {
77 return shared_context_
->GetHandle();
80 scoped_refptr
<gfx::GPUTimingClient
> GLContextVirtual::CreateGPUTimingClient() {
81 return shared_context_
->CreateGPUTimingClient();
84 void GLContextVirtual::OnSetSwapInterval(int interval
) {
85 shared_context_
->SetSwapInterval(interval
);
88 std::string
GLContextVirtual::GetExtensions() {
89 return shared_context_
->GetExtensions();
92 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes
) {
93 return shared_context_
->GetTotalGpuMemory(bytes
);
96 void GLContextVirtual::SetSafeToForceGpuSwitch() {
97 // TODO(ccameron): This will not work if two contexts that disagree
98 // about whether or not forced gpu switching may be done both share
99 // the same underlying shared_context_.
100 return shared_context_
->SetSafeToForceGpuSwitch();
103 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
104 return shared_context_
->WasAllocatedUsingRobustnessExtension();
107 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
108 shared_context_
->SetUnbindFboOnMakeCurrent();
111 base::Closure
GLContextVirtual::GetStateWasDirtiedExternallyCallback() {
112 return shared_context_
->GetStateWasDirtiedExternallyCallback();
115 void GLContextVirtual::RestoreStateIfDirtiedExternally() {
116 // The dirty bit should only be cleared after the state has been restored,
117 // which should be done only when the context is current.
118 DCHECK(IsCurrent(NULL
));
119 if (!shared_context_
->GetStateWasDirtiedExternally())
121 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api
;
122 GetGLStateRestorer()->RestoreState(NULL
);
123 shared_context_
->SetStateWasDirtiedExternally(false);
126 GLContextVirtual::~GLContextVirtual() {