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
),
25 gfx::Display
* GLContextVirtual::display() {
29 bool GLContextVirtual::Initialize(
30 gfx::GLSurface
* compatible_surface
, gfx::GpuPreference gpu_preference
) {
31 SetGLStateRestorer(new GLStateRestorerImpl(decoder_
));
33 display_
= static_cast<gfx::Display
*>(compatible_surface
->GetDisplay());
35 // Virtual contexts obviously can't make a context that is compatible
36 // with the surface (the context already exists), but we do need to
37 // make a context current for SetupForVirtualization() below.
38 if (!IsCurrent(compatible_surface
)) {
39 if (!shared_context_
->MakeCurrent(compatible_surface
)) {
40 // This is likely an error. The real context should be made as
41 // compatible with all required surfaces when it was created.
42 LOG(ERROR
) << "Failed MakeCurrent(compatible_surface)";
47 shared_context_
->SetupForVirtualization();
48 shared_context_
->MakeVirtuallyCurrent(this, compatible_surface
);
52 void GLContextVirtual::Destroy() {
53 shared_context_
->OnReleaseVirtuallyCurrent(this);
54 shared_context_
= NULL
;
58 bool GLContextVirtual::MakeCurrent(gfx::GLSurface
* surface
) {
60 return shared_context_
->MakeVirtuallyCurrent(this, surface
);
62 LOG(ERROR
) << "Trying to make virtual context current without decoder.";
66 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface
* surface
) {
67 if (IsCurrent(surface
)) {
68 shared_context_
->OnReleaseVirtuallyCurrent(this);
69 shared_context_
->ReleaseCurrent(surface
);
73 bool GLContextVirtual::IsCurrent(gfx::GLSurface
* surface
) {
74 // If it's a real surface it needs to be current.
76 !surface
->IsOffscreen())
77 return shared_context_
->IsCurrent(surface
);
79 // Otherwise, only insure the context itself is current.
80 return shared_context_
->IsCurrent(NULL
);
83 void* GLContextVirtual::GetHandle() {
84 return shared_context_
->GetHandle();
87 scoped_refptr
<gfx::GPUTimingClient
> GLContextVirtual::CreateGPUTimingClient() {
88 return shared_context_
->CreateGPUTimingClient();
91 void GLContextVirtual::OnSetSwapInterval(int interval
) {
92 shared_context_
->SetSwapInterval(interval
);
95 std::string
GLContextVirtual::GetExtensions() {
96 return shared_context_
->GetExtensions();
99 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes
) {
100 return shared_context_
->GetTotalGpuMemory(bytes
);
103 void GLContextVirtual::SetSafeToForceGpuSwitch() {
104 // TODO(ccameron): This will not work if two contexts that disagree
105 // about whether or not forced gpu switching may be done both share
106 // the same underlying shared_context_.
107 return shared_context_
->SetSafeToForceGpuSwitch();
110 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
111 return shared_context_
->WasAllocatedUsingRobustnessExtension();
114 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
115 shared_context_
->SetUnbindFboOnMakeCurrent();
118 base::Closure
GLContextVirtual::GetStateWasDirtiedExternallyCallback() {
119 return shared_context_
->GetStateWasDirtiedExternallyCallback();
122 void GLContextVirtual::RestoreStateIfDirtiedExternally() {
123 // The dirty bit should only be cleared after the state has been restored,
124 // which should be done only when the context is current.
125 DCHECK(IsCurrent(NULL
));
126 if (!shared_context_
->GetStateWasDirtiedExternally())
128 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api
;
129 GetGLStateRestorer()->RestoreState(NULL
);
130 shared_context_
->SetStateWasDirtiedExternally(false);
133 GLContextVirtual::~GLContextVirtual() {