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 "wm/gpu/foreign_window_texture_factory.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/observer_list.h" |
| 9 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
| 10 #include "content/common/gpu/client/gl_helper.h" |
| 11 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" |
| 12 #include "third_party/khronos/GLES2/gl2.h" |
| 13 #include "ui/compositor/compositor_setup.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 class DefaultGpuChannelHostFactory : public content::GpuChannelHostFactory { |
| 18 public: |
| 19 DefaultGpuChannelHostFactory() {} |
| 20 |
| 21 virtual bool IsMainThread() OVERRIDE { |
| 22 return false; |
| 23 } |
| 24 virtual bool IsIOThread() OVERRIDE { |
| 25 return false; |
| 26 } |
| 27 virtual MessageLoop* GetMainLoop() OVERRIDE { |
| 28 return NULL; |
| 29 } |
| 30 virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() OVERRIDE { |
| 31 return NULL; |
| 32 } |
| 33 virtual base::WaitableEvent* GetShutDownEvent() OVERRIDE { return NULL; } |
| 34 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory( |
| 35 size_t size) OVERRIDE { |
| 36 return scoped_ptr<base::SharedMemory>(); |
| 37 } |
| 38 virtual int32 CreateViewCommandBuffer( |
| 39 int32 surface_id, |
| 40 const GPUCreateCommandBufferConfig& init_params) OVERRIDE { |
| 41 return 0; |
| 42 } |
| 43 virtual content::GpuChannelHost* EstablishGpuChannelSync( |
| 44 content::CauseForGpuLaunch) OVERRIDE { |
| 45 return NULL; |
| 46 } |
| 47 virtual void CreateImage( |
| 48 gfx::PluginWindowHandle window_handle, |
| 49 int32 image_id, |
| 50 const CreateImageCallback& callback) OVERRIDE { |
| 51 callback.Run(gfx::Size()); |
| 52 } |
| 53 virtual void DeleteImage(int32 image_id, int32 sync_point) OVERRIDE {} |
| 54 }; |
| 55 |
| 56 base::LazyInstance<DefaultGpuChannelHostFactory> g_default_factory = |
| 57 LAZY_INSTANCE_INITIALIZER; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 namespace wm { |
| 62 |
| 63 // static |
| 64 ForeignWindowTextureFactory* ForeignWindowTextureFactory::instance_ = NULL; |
| 65 |
| 66 ForeignWindowTexture::ForeignWindowTexture( |
| 67 content::GpuChannelHostFactory* factory, |
| 68 WebKit::WebGraphicsContext3D* host_context, |
| 69 bool flipped, |
| 70 const gfx::Size& size, |
| 71 float device_scale_factor, |
| 72 int32 image_id) |
| 73 : ui::Texture(flipped, size, device_scale_factor), |
| 74 factory_(factory), |
| 75 host_context_(host_context), |
| 76 image_id_(image_id), |
| 77 texture_id_(0), |
| 78 contents_changed_(true) { |
| 79 ForeignWindowTextureFactory::GetInstance()->AddObserver(this); |
| 80 } |
| 81 |
| 82 ForeignWindowTexture::~ForeignWindowTexture() { |
| 83 ForeignWindowTextureFactory::GetInstance()->RemoveObserver(this); |
| 84 if (texture_id_) |
| 85 host_context_->deleteTexture(texture_id_); |
| 86 if (image_id_ && !host_context_->isContextLost()) { |
| 87 factory_->DeleteImage(image_id_, |
| 88 texture_id_ ? host_context_->insertSyncPoint() : 0); |
| 89 } |
| 90 } |
| 91 |
| 92 unsigned int ForeignWindowTexture::PrepareTexture() { |
| 93 if (contents_changed_) { |
| 94 unsigned texture_id = texture_id_; |
| 95 |
| 96 if (!texture_id) |
| 97 texture_id = host_context_->createTexture(); |
| 98 |
| 99 host_context_->bindTexture(GL_TEXTURE_2D, texture_id); |
| 100 if (texture_id_) |
| 101 host_context_->releaseTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id_); |
| 102 host_context_->bindTexImage2DCHROMIUM(GL_TEXTURE_2D, image_id_); |
| 103 host_context_->bindTexture(GL_TEXTURE_2D, 0); |
| 104 host_context_->flush(); |
| 105 |
| 106 texture_id_ = texture_id; |
| 107 contents_changed_ = false; |
| 108 } |
| 109 |
| 110 return texture_id_; |
| 111 } |
| 112 |
| 113 WebKit::WebGraphicsContext3D* ForeignWindowTexture::HostContext3D() { |
| 114 DCHECK(host_context_); // This should never be called after lost context. |
| 115 return host_context_; |
| 116 } |
| 117 |
| 118 void ForeignWindowTexture::OnLostResources() { |
| 119 if (texture_id_) { |
| 120 host_context_->deleteTexture(texture_id_); |
| 121 texture_id_ = 0; |
| 122 } |
| 123 contents_changed_ = false; |
| 124 image_id_ = 0; |
| 125 host_context_ = NULL; |
| 126 } |
| 127 |
| 128 void ForeignWindowTexture::OnContentsChanged() { |
| 129 if (image_id_) |
| 130 contents_changed_ = true; |
| 131 } |
| 132 |
| 133 ForeignWindowTextureFactory::ForeignWindowTextureFactory( |
| 134 content::GpuChannelHostFactory* factory) : factory_(factory) { |
| 135 DCHECK(factory); |
| 136 DCHECK(content::ImageTransportFactory::GetInstance()); |
| 137 content::ImageTransportFactory::GetInstance()->AddObserver(this); |
| 138 } |
| 139 |
| 140 ForeignWindowTextureFactory::~ForeignWindowTextureFactory() { |
| 141 content::ImageTransportFactory::GetInstance()->RemoveObserver(this); |
| 142 } |
| 143 |
| 144 void ForeignWindowTextureFactory::OnLostResources() { |
| 145 content::ImageTransportFactory* transport_factory = |
| 146 content::ImageTransportFactory::GetInstance(); |
| 147 // Make sure a new gpu channel is established before any calls to |
| 148 // CreateTextureForForeignWindow() are made. |
| 149 WebKit::WebGraphicsContext3D* context = |
| 150 transport_factory->GetGLHelper()->context(); |
| 151 CHECK(context); |
| 152 |
| 153 FOR_EACH_OBSERVER(ForeignWindowTextureFactoryObserver, |
| 154 observer_list_, |
| 155 OnLostResources()); |
| 156 } |
| 157 |
| 158 void ForeignWindowTextureFactory::CreateTextureForForeignWindow( |
| 159 gfx::PluginWindowHandle window_handle, |
| 160 bool flipped, |
| 161 float device_scale_factor, |
| 162 const CreateTextureCallback& callback) { |
| 163 DCHECK(window_handle); |
| 164 |
| 165 int32 image_id = GenerateImageID(); |
| 166 factory_->CreateImage( |
| 167 window_handle, |
| 168 image_id, |
| 169 base::Bind(&ForeignWindowTextureFactory::OnImageCreated, |
| 170 base::Unretained(this), |
| 171 callback, |
| 172 flipped, |
| 173 device_scale_factor, |
| 174 image_id)); |
| 175 } |
| 176 |
| 177 void ForeignWindowTextureFactory::AddObserver( |
| 178 ForeignWindowTextureFactoryObserver* observer) { |
| 179 observer_list_.AddObserver(observer); |
| 180 } |
| 181 |
| 182 void ForeignWindowTextureFactory::RemoveObserver( |
| 183 ForeignWindowTextureFactoryObserver* observer) { |
| 184 observer_list_.RemoveObserver(observer); |
| 185 } |
| 186 |
| 187 int ForeignWindowTextureFactory::GenerateImageID() { |
| 188 static unsigned int image_id = 0; |
| 189 return ++image_id; |
| 190 } |
| 191 |
| 192 void ForeignWindowTextureFactory::OnImageCreated( |
| 193 const CreateTextureCallback& callback, |
| 194 bool flipped, |
| 195 float device_scale_factor, |
| 196 int32 image_id, |
| 197 const gfx::Size size) { |
| 198 if (size.IsEmpty()) { |
| 199 callback.Run(NULL); |
| 200 return; |
| 201 } |
| 202 |
| 203 content::ImageTransportFactory* transport_factory = |
| 204 content::ImageTransportFactory::GetInstance(); |
| 205 scoped_refptr<ForeignWindowTexture> texture( |
| 206 new ForeignWindowTexture(factory_, |
| 207 transport_factory->GetGLHelper()->context(), |
| 208 flipped, |
| 209 size, |
| 210 device_scale_factor, |
| 211 image_id)); |
| 212 |
| 213 callback.Run(texture); |
| 214 } |
| 215 |
| 216 // static |
| 217 void ForeignWindowTextureFactory::Initialize() { |
| 218 CHECK(!instance_); |
| 219 if (ui::IsTestCompositorEnabled()) { |
| 220 instance_ = new ForeignWindowTextureFactory(&g_default_factory.Get()); |
| 221 } else { |
| 222 instance_ = new ForeignWindowTextureFactory( |
| 223 content::BrowserGpuChannelHostFactory::instance()); |
| 224 } |
| 225 } |
| 226 |
| 227 // static |
| 228 void ForeignWindowTextureFactory::Terminate() { |
| 229 CHECK(instance_); |
| 230 delete instance_; |
| 231 instance_ = NULL; |
| 232 } |
| 233 |
| 234 // static |
| 235 ForeignWindowTextureFactory* ForeignWindowTextureFactory::GetInstance() { |
| 236 DCHECK(instance_); |
| 237 return instance_; |
| 238 } |
| 239 |
| 240 } // namespace wm |
OLD | NEW |