Chromium Code Reviews| 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_image_egl.h" | |
| 12 #include "ui/gl/gl_implementation.h" | 13 #include "ui/gl/gl_implementation.h" |
| 13 #include "ui/gl/gl_surface_egl.h" | 14 #include "ui/gl/gl_surface_egl.h" |
| 14 #include "ui/gl/gl_surface_osmesa.h" | 15 #include "ui/gl/gl_surface_osmesa.h" |
| 15 #include "ui/gl/gl_surface_stub.h" | 16 #include "ui/gl/gl_surface_stub.h" |
| 16 #include "ui/gl/scoped_make_current.h" | 17 #include "ui/gl/scoped_make_current.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 if (!ResizePixmaps()) | |
| 224 return false; | |
| 225 } | |
| 226 BindFramebuffer(); | |
| 227 return SurfacelessEGL::OnMakeCurrent(context); | |
| 228 } | |
| 229 | |
| 230 bool Resize(const gfx::Size& size) override { | |
| 231 return GLSurfaceOzoneSurfaceless::Resize(size) && ResizePixmaps(); | |
| 232 } | |
| 233 | |
| 234 bool SupportsPostSubBuffer() override { return false; } | |
| 235 | |
| 236 bool SwapBuffers() override { | |
| 237 bool ret = images_[current_surface_]->ScheduleOverlayPlane( | |
| 238 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE, | |
| 239 gfx::Rect(GetSize()), gfx::RectF(1, 1)) && | |
| 240 GLSurfaceOzoneSurfaceless::SwapBuffers(); | |
| 241 current_surface_ ^= 1; | |
| 242 BindFramebuffer(); | |
| 243 return ret; | |
| 244 } | |
| 245 | |
| 246 private: | |
| 247 ~GLSurfaceOzoneSurfacelessSurfaceImpl() override { | |
| 248 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | |
|
piman
2015/02/19 23:21:19
This is too late to do GL calls, the context is no
achaulk
2015/02/23 20:43:09
Done.
| |
| 249 glDeleteTextures(2, textures_); | |
| 250 glDeleteFramebuffersEXT(1, &fbo_); | |
| 251 } | |
| 252 | |
| 253 void BindFramebuffer() { | |
| 254 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_); | |
| 255 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
| 256 GL_TEXTURE_2D, textures_[current_surface_], 0); | |
| 257 } | |
| 258 | |
| 259 bool ResizePixmaps() { | |
| 260 if (!fbo_) | |
| 261 return true; | |
| 262 for (int i = 0; i < 2; i++) { | |
| 263 scoped_refptr<ui::NativePixmap> pixmap = | |
| 264 ui::SurfaceFactoryOzone::GetInstance()->CreateNativePixmap( | |
| 265 widget_, GetSize(), ui::SurfaceFactoryOzone::RGBA_8888, | |
| 266 ui::SurfaceFactoryOzone::SCANOUT); | |
| 267 if (!pixmap) | |
| 268 return false; | |
| 269 scoped_refptr<GLImageEGL> image = new GLImageEGL(GetSize()); | |
| 270 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
| 271 if (!image->Initialize(EGL_NATIVE_PIXMAP_KHR, | |
| 272 pixmap->GetEGLClientBuffer(), attrs)) | |
| 273 return false; | |
| 274 images_[i] = image; | |
| 275 pixmaps_[i] = pixmap; | |
| 276 // Bind image to texture. | |
| 277 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures_[i]); | |
| 278 if (!images_[i]->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) | |
| 279 return false; | |
| 280 } | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 GLuint fbo_; | |
| 285 GLuint textures_[2]; | |
| 286 scoped_refptr<GLImage> images_[2]; | |
| 287 scoped_refptr<ui::NativePixmap> pixmaps_[2]; | |
| 288 int current_surface_; | |
| 289 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl); | |
| 290 }; | |
| 291 | |
| 197 } // namespace | 292 } // namespace |
| 198 | 293 |
| 199 // static | 294 // static |
| 200 bool GLSurface::InitializeOneOffInternal() { | 295 bool GLSurface::InitializeOneOffInternal() { |
| 201 switch (GetGLImplementation()) { | 296 switch (GetGLImplementation()) { |
| 202 case kGLImplementationEGLGLES2: | 297 case kGLImplementationEGLGLES2: |
| 203 if (!GLSurfaceEGL::InitializeOneOff()) { | 298 if (!GLSurfaceEGL::InitializeOneOff()) { |
| 204 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | 299 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; |
| 205 return false; | 300 return false; |
| 206 } | 301 } |
| 207 | 302 |
| 208 return true; | 303 return true; |
| 209 case kGLImplementationOSMesaGL: | 304 case kGLImplementationOSMesaGL: |
| 210 case kGLImplementationMockGL: | 305 case kGLImplementationMockGL: |
| 211 return true; | 306 return true; |
| 212 default: | 307 default: |
| 213 return false; | 308 return false; |
| 214 } | 309 } |
| 215 } | 310 } |
| 216 | 311 |
| 217 // static | 312 // static |
| 313 scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface( | |
| 314 gfx::AcceleratedWidget window) { | |
| 315 if (GetGLImplementation() == kGLImplementationEGLGLES2 && | |
| 316 window != kNullAcceleratedWidget && | |
| 317 GLSurfaceEGL::IsEGLSurfacelessContextSupported() && | |
| 318 ui::SurfaceFactoryOzone::GetInstance()->CanShowPrimaryPlaneAsOverlay()) { | |
| 319 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | |
| 320 ui::SurfaceFactoryOzone::GetInstance() | |
| 321 ->CreateSurfacelessEGLSurfaceForWidget(window); | |
| 322 if (!surface_ozone) | |
| 323 return NULL; | |
|
piman
2015/02/19 23:21:19
nit: nullptr for consistency
achaulk
2015/02/23 20:43:09
Done.
| |
| 324 scoped_refptr<GLSurface> surface; | |
| 325 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); | |
| 326 if (surface->Initialize()) | |
| 327 return surface; | |
| 328 } | |
| 329 | |
| 330 return nullptr; | |
| 331 } | |
| 332 | |
| 333 // static | |
| 218 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( | 334 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( |
| 219 gfx::AcceleratedWidget window) { | 335 gfx::AcceleratedWidget window) { |
| 220 if (GetGLImplementation() == kGLImplementationOSMesaGL) { | 336 if (GetGLImplementation() == kGLImplementationOSMesaGL) { |
| 221 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); | 337 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); |
| 222 if (!surface->Initialize()) | 338 if (!surface->Initialize()) |
| 223 return NULL; | 339 return NULL; |
| 224 return surface; | 340 return surface; |
| 225 } | 341 } |
| 226 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); | 342 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); |
| 227 if (window != kNullAcceleratedWidget) { | 343 if (window != kNullAcceleratedWidget) { |
| 228 scoped_refptr<GLSurface> surface; | 344 scoped_refptr<GLSurface> surface; |
| 229 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && | 345 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && |
| 230 ui::SurfaceFactoryOzone::GetInstance() | 346 ui::SurfaceFactoryOzone::GetInstance() |
| 231 ->CanShowPrimaryPlaneAsOverlay()) { | 347 ->CanShowPrimaryPlaneAsOverlay()) { |
| 232 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 348 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
| 233 ui::SurfaceFactoryOzone::GetInstance() | 349 ui::SurfaceFactoryOzone::GetInstance() |
| 234 ->CreateSurfacelessEGLSurfaceForWidget(window); | 350 ->CreateSurfacelessEGLSurfaceForWidget(window); |
| 235 if (!surface_ozone) | 351 if (!surface_ozone) |
| 236 return NULL; | 352 return NULL; |
| 237 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); | 353 surface = new GLSurfaceOzoneSurfacelessSurfaceImpl(surface_ozone.Pass(), |
| 354 window); | |
| 238 } else { | 355 } else { |
| 239 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 356 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
| 240 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( | 357 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( |
| 241 window); | 358 window); |
| 242 if (!surface_ozone) | 359 if (!surface_ozone) |
| 243 return NULL; | 360 return NULL; |
| 244 | 361 |
| 245 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); | 362 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); |
| 246 } | 363 } |
| 247 if (!surface->Initialize()) | 364 if (!surface->Initialize()) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 NOTREACHED(); | 400 NOTREACHED(); |
| 284 return NULL; | 401 return NULL; |
| 285 } | 402 } |
| 286 } | 403 } |
| 287 | 404 |
| 288 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { | 405 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { |
| 289 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); | 406 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); |
| 290 } | 407 } |
| 291 | 408 |
| 292 } // namespace gfx | 409 } // namespace gfx |
| OLD | NEW |