Elim cr-checkbox
[chromium-blink-merge.git] / gpu / command_buffer / service / gl_context_virtual.cc
blob563e6018757960ad6e890dae5114bb99e734e4c8
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 "gpu/command_buffer/service/gles2_cmd_decoder.h"
10 #include "ui/gl/gl_gl_api_implementation.h"
11 #include "ui/gl/gl_surface.h"
12 #include "ui/gl/gpu_preference.h"
13 #include "ui/gl/gpu_timing.h"
14 #include "ui/gl/scoped_api.h"
16 namespace gpu {
18 GLContextVirtual::GLContextVirtual(
19 gfx::GLShareGroup* share_group,
20 gfx::GLContext* shared_context,
21 base::WeakPtr<gles2::GLES2Decoder> decoder)
22 : GLContext(share_group),
23 shared_context_(shared_context),
24 decoder_(decoder) {
27 bool GLContextVirtual::Initialize(
28 gfx::GLSurface* compatible_surface, gfx::GpuPreference gpu_preference) {
29 SetGLStateRestorer(new GLStateRestorerImpl(decoder_));
31 // Virtual contexts obviously can't make a context that is compatible
32 // with the surface (the context already exists), but we do need to
33 // make a context current for SetupForVirtualization() below.
34 if (!IsCurrent(compatible_surface)) {
35 if (!shared_context_->MakeCurrent(compatible_surface)) {
36 // This is likely an error. The real context should be made as
37 // compatible with all required surfaces when it was created.
38 LOG(ERROR) << "Failed MakeCurrent(compatible_surface)";
39 return false;
43 shared_context_->SetupForVirtualization();
44 shared_context_->MakeVirtuallyCurrent(this, compatible_surface);
45 return true;
48 void GLContextVirtual::Destroy() {
49 shared_context_->OnReleaseVirtuallyCurrent(this);
50 shared_context_ = NULL;
53 bool GLContextVirtual::MakeCurrent(gfx::GLSurface* surface) {
54 if (decoder_.get())
55 return shared_context_->MakeVirtuallyCurrent(this, surface);
57 LOG(ERROR) << "Trying to make virtual context current without decoder.";
58 return false;
61 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface* surface) {
62 if (IsCurrent(surface)) {
63 shared_context_->OnReleaseVirtuallyCurrent(this);
64 shared_context_->ReleaseCurrent(surface);
68 bool GLContextVirtual::IsCurrent(gfx::GLSurface* surface) {
69 // If it's a real surface it needs to be current.
70 if (surface &&
71 !surface->IsOffscreen())
72 return shared_context_->IsCurrent(surface);
74 // Otherwise, only insure the context itself is current.
75 return shared_context_->IsCurrent(NULL);
78 void* GLContextVirtual::GetHandle() {
79 return shared_context_->GetHandle();
82 scoped_refptr<gfx::GPUTimingClient> GLContextVirtual::CreateGPUTimingClient() {
83 return shared_context_->CreateGPUTimingClient();
86 void GLContextVirtual::OnSetSwapInterval(int interval) {
87 shared_context_->SetSwapInterval(interval);
90 std::string GLContextVirtual::GetExtensions() {
91 return shared_context_->GetExtensions();
94 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes) {
95 return shared_context_->GetTotalGpuMemory(bytes);
98 void GLContextVirtual::SetSafeToForceGpuSwitch() {
99 // TODO(ccameron): This will not work if two contexts that disagree
100 // about whether or not forced gpu switching may be done both share
101 // the same underlying shared_context_.
102 return shared_context_->SetSafeToForceGpuSwitch();
105 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
106 return shared_context_->WasAllocatedUsingRobustnessExtension();
109 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
110 shared_context_->SetUnbindFboOnMakeCurrent();
113 base::Closure GLContextVirtual::GetStateWasDirtiedExternallyCallback() {
114 return shared_context_->GetStateWasDirtiedExternallyCallback();
117 void GLContextVirtual::RestoreStateIfDirtiedExternally() {
118 // The dirty bit should only be cleared after the state has been restored,
119 // which should be done only when the context is current.
120 DCHECK(IsCurrent(NULL));
121 if (!shared_context_->GetStateWasDirtiedExternally())
122 return;
123 gfx::ScopedSetGLToRealGLApi scoped_set_gl_api;
124 GetGLStateRestorer()->RestoreState(NULL);
125 shared_context_->SetStateWasDirtiedExternally(false);
128 GLContextVirtual::~GLContextVirtual() {
129 Destroy();
132 } // namespace gpu