OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_surface.h" | 5 #include "ui/gl/gl_surface.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "ui/gfx/native_widget_types.h" | 9 #include "ui/gfx/native_widget_types.h" |
10 #include "ui/gl/gl_context.h" | 10 #include "ui/gl/gl_context.h" |
11 #include "ui/gl/gl_image.h" | 11 #include "ui/gl/gl_image.h" |
12 #include "ui/gl/gl_implementation.h" | 12 #include "ui/gl/gl_implementation.h" |
13 #include "ui/gl/gl_surface_egl.h" | 13 #include "ui/gl/gl_surface_egl.h" |
14 #include "ui/gl/gl_surface_osmesa.h" | 14 #include "ui/gl/gl_surface_osmesa.h" |
15 #include "ui/gl/gl_surface_stub.h" | 15 #include "ui/gl/gl_surface_stub.h" |
16 #include "ui/gl/scoped_make_current.h" | 16 #include "ui/gl/scoped_make_current.h" |
| 17 #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h" |
| 18 #include "ui/ozone/public/native_pixmap.h" |
17 #include "ui/ozone/public/surface_factory_ozone.h" | 19 #include "ui/ozone/public/surface_factory_ozone.h" |
18 #include "ui/ozone/public/surface_ozone_egl.h" | 20 #include "ui/ozone/public/surface_ozone_egl.h" |
19 | 21 |
20 namespace gfx { | 22 namespace gfx { |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow | 26 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow |
25 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { | 27 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { |
26 public: | 28 public: |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 return ozone_surface_->OnSwapBuffersAsync(callback); | 152 return ozone_surface_->OnSwapBuffersAsync(callback); |
151 } | 153 } |
152 bool PostSubBufferAsync(int x, | 154 bool PostSubBufferAsync(int x, |
153 int y, | 155 int y, |
154 int width, | 156 int width, |
155 int height, | 157 int height, |
156 const SwapCompletionCallback& callback) override { | 158 const SwapCompletionCallback& callback) override { |
157 return SwapBuffersAsync(callback); | 159 return SwapBuffersAsync(callback); |
158 } | 160 } |
159 | 161 |
160 private: | 162 protected: |
161 ~GLSurfaceOzoneSurfaceless() override { | 163 ~GLSurfaceOzoneSurfaceless() override { |
162 Destroy(); // EGL surface must be destroyed before SurfaceOzone | 164 Destroy(); // EGL surface must be destroyed before SurfaceOzone |
163 } | 165 } |
164 | 166 |
165 bool Flush() { | 167 bool Flush() { |
166 glFlush(); | 168 glFlush(); |
167 // TODO: the following should be replaced by a per surface flush as it gets | 169 // TODO: the following should be replaced by a per surface flush as it gets |
168 // implemented in GL drivers. | 170 // implemented in GL drivers. |
169 if (has_implicit_external_sync_) { | 171 if (has_implicit_external_sync_) { |
170 const EGLint attrib_list[] = { | 172 const EGLint attrib_list[] = { |
(...skipping 16 matching lines...) Expand all Loading... |
187 } | 189 } |
188 | 190 |
189 // The native surface. Deleting this is allowed to free the EGLNativeWindow. | 191 // The native surface. Deleting this is allowed to free the EGLNativeWindow. |
190 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; | 192 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; |
191 AcceleratedWidget widget_; | 193 AcceleratedWidget widget_; |
192 scoped_ptr<VSyncProvider> vsync_provider_; | 194 scoped_ptr<VSyncProvider> vsync_provider_; |
193 bool has_implicit_external_sync_; | 195 bool has_implicit_external_sync_; |
194 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); | 196 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); |
195 }; | 197 }; |
196 | 198 |
| 199 // This provides surface-like semantics implemented through surfaceless. |
| 200 // A framebuffer is bound automatically and re-bound at each swap. |
| 201 class GL_EXPORT GLSurfaceOzoneSurfacelessSurfaceImpl |
| 202 : public GLSurfaceOzoneSurfaceless { |
| 203 public: |
| 204 GLSurfaceOzoneSurfacelessSurfaceImpl( |
| 205 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
| 206 AcceleratedWidget widget) |
| 207 : GLSurfaceOzoneSurfaceless(ozone_surface.Pass(), widget), |
| 208 fbo_(0), |
| 209 current_surface_(0) { |
| 210 textures_[0] = 0; |
| 211 textures_[1] = 0; |
| 212 } |
| 213 |
| 214 unsigned int GetBackingFrameBufferObject() override { return fbo_; } |
| 215 |
| 216 bool OnMakeCurrent(GLContext* context) override { |
| 217 if (!fbo_) { |
| 218 glGenFramebuffersEXT(1, &fbo_); |
| 219 if (!fbo_) |
| 220 return false; |
| 221 glGenTextures(2, textures_); |
| 222 // Create and bind our pixmaps. |
| 223 ResizePixmaps(); |
| 224 } |
| 225 BindFramebuffer(); |
| 226 return SurfacelessEGL::OnMakeCurrent(context); |
| 227 } |
| 228 |
| 229 bool Resize(const gfx::Size& size) override { |
| 230 return GLSurfaceOzoneSurfaceless::Resize(size) && ResizePixmaps(); |
| 231 } |
| 232 |
| 233 bool SupportsPostSubBuffer() override { return false; } |
| 234 |
| 235 bool SwapBuffers() override { |
| 236 bool ret = images_[current_surface_]->ScheduleOverlayPlane( |
| 237 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE, |
| 238 gfx::Rect(GetSize()), gfx::RectF(1, 1)) && |
| 239 GLSurfaceOzoneSurfaceless::SwapBuffers(); |
| 240 current_surface_ ^= 1; |
| 241 BindFramebuffer(); |
| 242 return ret; |
| 243 } |
| 244 |
| 245 private: |
| 246 ~GLSurfaceOzoneSurfacelessSurfaceImpl() override { |
| 247 glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 248 glDeleteTextures(2, textures_); |
| 249 glDeleteFramebuffers(1, &fbo_); |
| 250 } |
| 251 |
| 252 void BindFramebuffer() { |
| 253 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_); |
| 254 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 255 GL_TEXTURE_2D, textures_[current_surface_], 0); |
| 256 } |
| 257 |
| 258 bool ResizePixmaps() { |
| 259 if (!fbo_) |
| 260 return true; |
| 261 for (int i = 0; i < 2; i++) { |
| 262 scoped_refptr<ui::NativePixmap> pixmap = |
| 263 ui::SurfaceFactoryOzone::GetInstance()->CreateNativePixmap( |
| 264 widget_, GetSize(), ui::SurfaceFactoryOzone::RGBA_8888, |
| 265 ui::SurfaceFactoryOzone::SCANOUT); |
| 266 if (!pixmap) |
| 267 return false; |
| 268 images_[i] = |
| 269 ui::GpuMemoryBufferFactoryOzoneNativeBuffer::CreateImageForPixmap( |
| 270 pixmap, GetSize(), GpuMemoryBuffer::Format::RGBA_8888, GL_RGBA); |
| 271 // Bind image to texture |
| 272 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures_[i]); |
| 273 if (!images_[i]->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) |
| 274 return false; |
| 275 } |
| 276 return true; |
| 277 } |
| 278 |
| 279 GLuint fbo_; |
| 280 GLuint textures_[2]; |
| 281 scoped_refptr<GLImage> images_[2]; |
| 282 int current_surface_; |
| 283 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl); |
| 284 }; |
| 285 |
197 } // namespace | 286 } // namespace |
198 | 287 |
199 // static | 288 // static |
200 bool GLSurface::InitializeOneOffInternal() { | 289 bool GLSurface::InitializeOneOffInternal() { |
201 switch (GetGLImplementation()) { | 290 switch (GetGLImplementation()) { |
202 case kGLImplementationEGLGLES2: | 291 case kGLImplementationEGLGLES2: |
203 if (!GLSurfaceEGL::InitializeOneOff()) { | 292 if (!GLSurfaceEGL::InitializeOneOff()) { |
204 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | 293 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; |
205 return false; | 294 return false; |
206 } | 295 } |
207 | 296 |
208 return true; | 297 return true; |
209 case kGLImplementationOSMesaGL: | 298 case kGLImplementationOSMesaGL: |
210 case kGLImplementationMockGL: | 299 case kGLImplementationMockGL: |
211 return true; | 300 return true; |
212 default: | 301 default: |
213 return false; | 302 return false; |
214 } | 303 } |
215 } | 304 } |
216 | 305 |
217 // static | 306 // static |
| 307 scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface( |
| 308 gfx::AcceleratedWidget window) { |
| 309 if (GetGLImplementation() == kGLImplementationEGLGLES2 && |
| 310 window != kNullAcceleratedWidget && |
| 311 GLSurfaceEGL::IsEGLSurfacelessContextSupported() && |
| 312 ui::SurfaceFactoryOzone::GetInstance()->CanShowPrimaryPlaneAsOverlay()) { |
| 313 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
| 314 ui::SurfaceFactoryOzone::GetInstance() |
| 315 ->CreateSurfacelessEGLSurfaceForWidget(window); |
| 316 if (!surface_ozone) |
| 317 return NULL; |
| 318 scoped_refptr<GLSurface> surface; |
| 319 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); |
| 320 if (surface->Initialize()) |
| 321 return surface; |
| 322 } |
| 323 |
| 324 return nullptr; |
| 325 } |
| 326 |
| 327 // static |
218 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( | 328 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( |
219 gfx::AcceleratedWidget window) { | 329 gfx::AcceleratedWidget window) { |
220 if (GetGLImplementation() == kGLImplementationOSMesaGL) { | 330 if (GetGLImplementation() == kGLImplementationOSMesaGL) { |
221 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); | 331 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); |
222 if (!surface->Initialize()) | 332 if (!surface->Initialize()) |
223 return NULL; | 333 return NULL; |
224 return surface; | 334 return surface; |
225 } | 335 } |
226 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); | 336 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); |
227 if (window != kNullAcceleratedWidget) { | 337 if (window != kNullAcceleratedWidget) { |
228 scoped_refptr<GLSurface> surface; | 338 scoped_refptr<GLSurface> surface; |
229 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && | 339 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && |
230 ui::SurfaceFactoryOzone::GetInstance() | 340 ui::SurfaceFactoryOzone::GetInstance() |
231 ->CanShowPrimaryPlaneAsOverlay()) { | 341 ->CanShowPrimaryPlaneAsOverlay()) { |
232 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 342 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
233 ui::SurfaceFactoryOzone::GetInstance() | 343 ui::SurfaceFactoryOzone::GetInstance() |
234 ->CreateSurfacelessEGLSurfaceForWidget(window); | 344 ->CreateSurfacelessEGLSurfaceForWidget(window); |
235 if (!surface_ozone) | 345 if (!surface_ozone) |
236 return NULL; | 346 return NULL; |
237 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); | 347 surface = new GLSurfaceOzoneSurfacelessSurfaceImpl(surface_ozone.Pass(), |
| 348 window); |
238 } else { | 349 } else { |
239 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 350 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
240 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( | 351 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( |
241 window); | 352 window); |
242 if (!surface_ozone) | 353 if (!surface_ozone) |
243 return NULL; | 354 return NULL; |
244 | 355 |
245 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); | 356 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); |
246 } | 357 } |
247 if (!surface->Initialize()) | 358 if (!surface->Initialize()) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 NOTREACHED(); | 394 NOTREACHED(); |
284 return NULL; | 395 return NULL; |
285 } | 396 } |
286 } | 397 } |
287 | 398 |
288 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { | 399 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { |
289 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); | 400 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); |
290 } | 401 } |
291 | 402 |
292 } // namespace gfx | 403 } // namespace gfx |
OLD | NEW |