Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / gpu / command_buffer / service / gl_context_virtual.cc
blob7747407cf6dfba7887bd043afb45bd445fb1afba
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"
10 #include "ui/gl/gpu_timing.h"
12 namespace gpu {
14 GLContextVirtual::GLContextVirtual(
15 gfx::GLShareGroup* share_group,
16 gfx::GLContext* shared_context,
17 base::WeakPtr<gles2::GLES2Decoder> decoder)
18 : GLContext(share_group),
19 shared_context_(shared_context),
20 display_(NULL),
21 decoder_(decoder) {
24 gfx::Display* GLContextVirtual::display() {
25 return display_;
28 bool GLContextVirtual::Initialize(
29 gfx::GLSurface* compatible_surface, gfx::GpuPreference gpu_preference) {
30 SetGLStateRestorer(new GLStateRestorerImpl(decoder_));
32 display_ = static_cast<gfx::Display*>(compatible_surface->GetDisplay());
34 // Virtual contexts obviously can't make a context that is compatible
35 // with the surface (the context already exists), but we do need to
36 // make a context current for SetupForVirtualization() below.
37 if (!IsCurrent(compatible_surface)) {
38 if (!shared_context_->MakeCurrent(compatible_surface)) {
39 // This is likely an error. The real context should be made as
40 // compatible with all required surfaces when it was created.
41 LOG(ERROR) << "Failed MakeCurrent(compatible_surface)";
42 return false;
46 shared_context_->SetupForVirtualization();
47 shared_context_->MakeVirtuallyCurrent(this, compatible_surface);
48 return true;
51 void GLContextVirtual::Destroy() {
52 shared_context_->OnReleaseVirtuallyCurrent(this);
53 shared_context_ = NULL;
54 display_ = NULL;
57 bool GLContextVirtual::MakeCurrent(gfx::GLSurface* surface) {
58 if (decoder_.get())
59 return shared_context_->MakeVirtuallyCurrent(this, surface);
61 LOG(ERROR) << "Trying to make virtual context current without decoder.";
62 return false;
65 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface* surface) {
66 if (IsCurrent(surface)) {
67 shared_context_->OnReleaseVirtuallyCurrent(this);
68 shared_context_->ReleaseCurrent(surface);
72 bool GLContextVirtual::IsCurrent(gfx::GLSurface* surface) {
73 // If it's a real surface it needs to be current.
74 if (surface &&
75 !surface->IsOffscreen())
76 return shared_context_->IsCurrent(surface);
78 // Otherwise, only insure the context itself is current.
79 return shared_context_->IsCurrent(NULL);
82 void* GLContextVirtual::GetHandle() {
83 return shared_context_->GetHandle();
86 scoped_refptr<gfx::GPUTimingClient> GLContextVirtual::CreateGPUTimingClient() {
87 return shared_context_->CreateGPUTimingClient();
90 void GLContextVirtual::OnSetSwapInterval(int interval) {
91 shared_context_->SetSwapInterval(interval);
94 std::string GLContextVirtual::GetExtensions() {
95 return shared_context_->GetExtensions();
98 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes) {
99 return shared_context_->GetTotalGpuMemory(bytes);
102 void GLContextVirtual::SetSafeToForceGpuSwitch() {
103 // TODO(ccameron): This will not work if two contexts that disagree
104 // about whether or not forced gpu switching may be done both share
105 // the same underlying shared_context_.
106 return shared_context_->SetSafeToForceGpuSwitch();
109 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() {
110 return shared_context_->WasAllocatedUsingRobustnessExtension();
113 void GLContextVirtual::SetUnbindFboOnMakeCurrent() {
114 shared_context_->SetUnbindFboOnMakeCurrent();
117 GLContextVirtual::~GLContextVirtual() {
118 Destroy();
121 } // namespace gpu