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_gl_api_implementation.h"
10 #include "ui/gl/gl_surface.h"
11 #include "ui/gl/gpu_timing.h"
15 GLContextVirtual::GLContextVirtual(
16 gfx::GLShareGroup
* share_group
,
17 gfx::GLContext
* shared_context
,
18 base::WeakPtr
<gles2::GLES2Decoder
> decoder
)
19 : GLContext(share_group
),
20 shared_context_(shared_context
),
24 bool GLContextVirtual::Initialize(
25 gfx::GLSurface
* compatible_surface
, gfx::GpuPreference gpu_preference
) {
26 SetGLStateRestorer(new GLStateRestorerImpl(decoder_
));
28 // Virtual contexts obviously can't make a context that is compatible
29 // with the surface (the context already exists), but we do need to
30 // make a context current for SetupForVirtualization() below.
31 if (!IsCurrent(compatible_surface
)) {
32 if (!shared_context_
->MakeCurrent(compatible_surface
)) {
33 // This is likely an error. The real context should be made as
34 // compatible with all required surfaces when it was created.
35 LOG(ERROR
) << "Failed MakeCurrent(compatible_surface)";
40 shared_context_
->SetupForVirtualization();
41 shared_context_
->MakeVirtuallyCurrent(this, compatible_surface
);
45 void GLContextVirtual::Destroy() {
46 shared_context_
->OnReleaseVirtuallyCurrent(this);
47 shared_context_
= NULL
;
50 bool GLContextVirtual::MakeCurrent(gfx::GLSurface
* surface
) {
52 return shared_context_
->MakeVirtuallyCurrent(this, surface
);
54 LOG(ERROR
) << "Trying to make virtual context current without decoder.";
58 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface
* surface
) {
59 if (IsCurrent(surface
)) {
60 shared_context_
->OnReleaseVirtuallyCurrent(this);
61 shared_context_
->ReleaseCurrent(surface
);
65 bool GLContextVirtual::IsCurrent(gfx::GLSurface
* surface
) {
66 // If it's a real surface it needs to be current.
68 !surface
->IsOffscreen())
69 return shared_context_
->IsCurrent(surface
);
71 // Otherwise, only insure the context itself is current.
72 return shared_context_
->IsCurrent(NULL
);
75 void* GLContextVirtual::GetHandle() {
76 return shared_context_
->GetHandle();
79 scoped_refptr
<gfx::GPUTimingClient
> GLContextVirtual::CreateGPUTimingClient() {
80 return shared_context_
->CreateGPUTimingClient();
83 void GLContextVirtual::OnSetSwapInterval(int interval
) {
84 shared_context_
->SetSwapInterval(interval
);
87 std::string
GLContextVirtual::GetExtensions() {
88 return shared_context_
->GetExtensions();
91 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes
) {
92 return shared_context_
->GetTotalGpuMemory(bytes
);
95 void GLContextVirtual::SetSafeToForceGpuSwitch() {
96 // TODO(ccameron): This will not work if two contexts that disagree
97 // about whether or not forced gpu switching may be done both share
98 // the same underlying shared_context_.
99 return shared_context_
->SetSafeToForceGpuSwitch();
102 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
103 return shared_context_
->WasAllocatedUsingRobustnessExtension();
106 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
107 shared_context_
->SetUnbindFboOnMakeCurrent();
110 base::Closure
GLContextVirtual::GetStateWasDirtiedExternallyCallback() {
111 return shared_context_
->GetStateWasDirtiedExternallyCallback();
114 void GLContextVirtual::RestoreStateIfDirtiedExternally() {
115 // The dirty bit should only be cleared after the state has been restored,
116 // which should be done only when the context is current.
117 DCHECK(IsCurrent(NULL
));
118 if (!shared_context_
->GetStateWasDirtiedExternally())
120 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api
;
121 GetGLStateRestorer()->RestoreState(NULL
);
122 shared_context_
->SetStateWasDirtiedExternally(false);
125 GLContextVirtual::~GLContextVirtual() {