Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "webkit/gpu/context_provider_in_process.h" | |
| 6 | |
| 7 #include "webkit/gpu/grcontext_for_webgraphicscontext3d.h" | |
| 8 #include "webkit/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h" | |
| 9 #include "webkit/gpu/webgraphicscontext3d_in_process_impl.h" | |
| 10 | |
| 11 namespace webkit { | |
| 12 namespace gpu { | |
| 13 | |
| 14 class ContextProviderInProcess::LostContextCallbackProxy | |
| 15 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback { | |
| 16 public: | |
| 17 LostContextCallbackProxy(ContextProviderInProcess* provider) | |
| 18 : provider_(provider) { | |
| 19 provider_->context3d_->setContextLostCallback(this); | |
| 20 } | |
| 21 | |
| 22 virtual void onContextLost() { | |
| 23 provider_->OnLostContext(); | |
| 24 } | |
| 25 | |
| 26 private: | |
| 27 ContextProviderInProcess* provider_; | |
| 28 }; | |
| 29 | |
| 30 class ContextProviderInProcess::MemoryAllocationCallbackProxy | |
| 31 : public WebKit::WebGraphicsContext3D:: | |
| 32 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM { | |
| 33 public: | |
| 34 MemoryAllocationCallbackProxy(ContextProviderInProcess* provider) | |
|
jamesr
2013/02/26 19:18:11
explicit
danakj
2013/02/26 19:23:02
Done.
| |
| 35 : provider_(provider) { | |
| 36 provider_->context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this); | |
| 37 } | |
| 38 | |
| 39 void onMemoryAllocationChanged(WebKit::WebGraphicsMemoryAllocation alloc) { | |
| 40 provider_->OnMemoryAllocationChanged(!!alloc.gpuResourceSizeInBytes); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 ContextProviderInProcess* provider_; | |
| 45 }; | |
| 46 | |
| 47 ContextProviderInProcess::ContextProviderInProcess(InProcessType type) | |
| 48 : type_(type), | |
| 49 destroyed_(false) { | |
| 50 } | |
| 51 | |
| 52 ContextProviderInProcess::~ContextProviderInProcess() {} | |
| 53 | |
| 54 bool ContextProviderInProcess::InitializeOnMainThread() { | |
| 55 if (destroyed_) | |
| 56 return false; | |
| 57 if (context3d_) | |
| 58 return true; | |
| 59 | |
| 60 context3d_ = CreateOffscreenContext3d().Pass(); | |
| 61 destroyed_ = !context3d_; | |
| 62 return !!context3d_; | |
| 63 } | |
| 64 | |
| 65 bool ContextProviderInProcess::BindToCurrentThread() { | |
| 66 if (lost_context_callback_proxy_) | |
| 67 return true; | |
| 68 | |
| 69 bool result = context3d_->makeContextCurrent(); | |
| 70 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
| 71 return result; | |
| 72 } | |
| 73 | |
| 74 WebKit::WebGraphicsContext3D* ContextProviderInProcess::Context3d() { | |
| 75 return context3d_.get(); | |
| 76 } | |
| 77 | |
| 78 class GrContext* ContextProviderInProcess::GrContext() { | |
| 79 if (gr_context_) | |
| 80 return gr_context_->get(); | |
| 81 | |
| 82 gr_context_.reset( | |
| 83 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_.get())); | |
| 84 memory_allocation_callback_proxy_.reset( | |
| 85 new MemoryAllocationCallbackProxy(this)); | |
| 86 return gr_context_->get(); | |
| 87 } | |
| 88 | |
| 89 void ContextProviderInProcess::VerifyContexts() { | |
| 90 if (!destroyed_ && context3d_->isContextLost()) | |
| 91 OnLostContext(); | |
| 92 } | |
| 93 | |
| 94 void ContextProviderInProcess::OnLostContext() { | |
| 95 base::AutoLock lock(destroyed_lock_); | |
| 96 destroyed_ = true; | |
| 97 } | |
| 98 | |
| 99 bool ContextProviderInProcess::DestroyedOnMainThread() { | |
| 100 base::AutoLock lock(destroyed_lock_); | |
| 101 return destroyed_; | |
| 102 } | |
| 103 | |
| 104 void ContextProviderInProcess::OnMemoryAllocationChanged( | |
| 105 bool nonzero_allocation) { | |
| 106 if (gr_context_) | |
| 107 gr_context_->SetMemoryLimit(nonzero_allocation); | |
| 108 } | |
| 109 | |
| 110 static scoped_ptr<WebKit::WebGraphicsContext3D> CreateInProcessImpl( | |
| 111 const WebKit::WebGraphicsContext3D::Attributes& attributes) { | |
| 112 return make_scoped_ptr( | |
| 113 webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWebView( | |
| 114 attributes, false)).PassAs<WebKit::WebGraphicsContext3D>(); | |
| 115 } | |
| 116 | |
| 117 static scoped_ptr<WebKit::WebGraphicsContext3D> CreateCommandBufferImpl( | |
| 118 const WebKit::WebGraphicsContext3D::Attributes& attributes) { | |
| 119 scoped_ptr<webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl> ctx( | |
| 120 new webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl()); | |
| 121 if (!ctx->Initialize(attributes, NULL)) | |
| 122 return scoped_ptr<WebKit::WebGraphicsContext3D>(); | |
| 123 return ctx.PassAs<WebKit::WebGraphicsContext3D>(); | |
| 124 } | |
| 125 | |
| 126 scoped_ptr<WebKit::WebGraphicsContext3D> | |
| 127 ContextProviderInProcess::CreateOffscreenContext3d() { | |
| 128 WebKit::WebGraphicsContext3D::Attributes attributes; | |
| 129 attributes.depth = false; | |
| 130 attributes.stencil = true; | |
| 131 attributes.antialias = false; | |
| 132 attributes.shareResources = true; | |
| 133 attributes.noAutomaticFlushes = true; | |
| 134 | |
| 135 switch (type_) { | |
| 136 case IN_PROCESS: | |
| 137 return CreateInProcessImpl(attributes).Pass(); | |
| 138 case IN_PROCESS_COMMAND_BUFFER: | |
| 139 return CreateCommandBufferImpl(attributes).Pass(); | |
| 140 } | |
| 141 NOTREACHED(); | |
| 142 } | |
| 143 | |
| 144 } // namespace gpu | |
| 145 } // namespace webkit | |
| OLD | NEW |