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. 4 5 #include "gpu/command_buffer/service/gl_context_virtual.h" 6 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 11 namespace gpu { 12 13 GLContextVirtual::GLContextVirtual( 14 gfx::GLShareGroup* share_group, 15 gfx::GLContext* shared_context, 16 base::WeakPtr<gles2::GLES2Decoder> decoder) 17 : GLContext(share_group), 18 shared_context_(shared_context), 19 display_(NULL), 20 decoder_(decoder) { 21 } 22 23 gfx::Display* GLContextVirtual::display() { 24 return display_; 25 } 26 27 bool GLContextVirtual::Initialize( 28 gfx::GLSurface* compatible_surface, gfx::GpuPreference gpu_preference) { 29 SetGLStateRestorer(new GLStateRestorerImpl(decoder_)); 30 31 display_ = static_cast<gfx::Display*>(compatible_surface->GetDisplay()); 32 33 // Virtual contexts obviously can't make a context that is compatible 34 // with the surface (the context already exists), but we do need to 35 // make a context current for SetupForVirtualization() below. 36 if (!IsCurrent(compatible_surface)) { 37 if (!shared_context_->MakeCurrent(compatible_surface)) { 38 // This is likely an error. The real context should be made as 39 // compatible with all required surfaces when it was created. 40 LOG(ERROR) << "Failed MakeCurrent(compatible_surface)"; 41 return false; 42 } 43 } 44 45 shared_context_->SetupForVirtualization(); 46 shared_context_->MakeVirtuallyCurrent(this, compatible_surface); 47 return true; 48 } 49 50 void GLContextVirtual::Destroy() { 51 shared_context_->OnReleaseVirtuallyCurrent(this); 52 shared_context_ = NULL; 53 display_ = NULL; 54 } 55 56 bool GLContextVirtual::MakeCurrent(gfx::GLSurface* surface) { 57 if (decoder_.get()) 58 return shared_context_->MakeVirtuallyCurrent(this, surface); 59 60 LOG(ERROR) << "Trying to make virtual context current without decoder."; 61 return false; 62 } 63 64 void GLContextVirtual::ReleaseCurrent(gfx::GLSurface* surface) { 65 if (IsCurrent(surface)) { 66 shared_context_->OnReleaseVirtuallyCurrent(this); 67 shared_context_->ReleaseCurrent(surface); 68 } 69 } 70 71 bool GLContextVirtual::IsCurrent(gfx::GLSurface* surface) { 72 // If it's a real surface it needs to be current. 73 if (surface && 74 !surface->IsOffscreen() && 75 !surface->GetBackingFrameBufferObject()) 76 return shared_context_->IsCurrent(surface); 77 78 // Otherwise, only insure the context itself is current. 79 return shared_context_->IsCurrent(NULL); 80 } 81 82 void* GLContextVirtual::GetHandle() { 83 return shared_context_->GetHandle(); 84 } 85 86 void GLContextVirtual::SetSwapInterval(int interval) { 87 shared_context_->SetSwapInterval(interval); 88 } 89 90 std::string GLContextVirtual::GetExtensions() { 91 return shared_context_->GetExtensions(); 92 } 93 94 bool GLContextVirtual::GetTotalGpuMemory(size_t* bytes) { 95 return shared_context_->GetTotalGpuMemory(bytes); 96 } 97 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(); 103 } 104 105 bool GLContextVirtual::WasAllocatedUsingRobustnessExtension() { 106 return shared_context_->WasAllocatedUsingRobustnessExtension(); 107 } 108 109 void GLContextVirtual::SetUnbindFboOnMakeCurrent() { 110 shared_context_->SetUnbindFboOnMakeCurrent(); 111 } 112 113 GLContextVirtual::~GLContextVirtual() { 114 Destroy(); 115 } 116 117 } // namespace gpu 118