OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ui/ozone/platform/drm/gpu/gbm_surface.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/gl/gl_surface_egl.h" |
| 11 #include "ui/ozone/gl/gl_image_ozone_native_pixmap.h" |
| 12 #include "ui/ozone/platform/drm/gpu/drm_window_proxy.h" |
| 13 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h" |
| 14 #include "ui/ozone/public/native_pixmap.h" |
| 15 |
| 16 namespace ui { |
| 17 |
| 18 GbmSurface::GbmSurface(GbmSurfaceFactory* surface_factory, |
| 19 std::unique_ptr<DrmWindowProxy> window, |
| 20 gfx::AcceleratedWidget widget) |
| 21 : GbmSurfaceless(surface_factory, std::move(window), widget) { |
| 22 for (auto& texture : textures_) |
| 23 texture = 0; |
| 24 } |
| 25 |
| 26 unsigned int GbmSurface::GetBackingFrameBufferObject() { |
| 27 return fbo_; |
| 28 } |
| 29 |
| 30 bool GbmSurface::OnMakeCurrent(gl::GLContext* context) { |
| 31 DCHECK(!context_ || context == context_); |
| 32 context_ = context; |
| 33 if (!fbo_) { |
| 34 glGenFramebuffersEXT(1, &fbo_); |
| 35 if (!fbo_) |
| 36 return false; |
| 37 glGenTextures(arraysize(textures_), textures_); |
| 38 if (!CreatePixmaps()) |
| 39 return false; |
| 40 } |
| 41 BindFramebuffer(); |
| 42 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_); |
| 43 return SurfacelessEGL::OnMakeCurrent(context); |
| 44 } |
| 45 |
| 46 bool GbmSurface::Resize(const gfx::Size& size, |
| 47 float scale_factor, |
| 48 bool has_alpha) { |
| 49 if (size == GetSize()) |
| 50 return true; |
| 51 // Alpha value isn't actually used in allocating buffers yet, so always use |
| 52 // true instead. |
| 53 return GbmSurfaceless::Resize(size, scale_factor, true) && CreatePixmaps(); |
| 54 } |
| 55 |
| 56 bool GbmSurface::SupportsPostSubBuffer() { |
| 57 return false; |
| 58 } |
| 59 |
| 60 void GbmSurface::SwapBuffersAsync(const SwapCompletionCallback& callback) { |
| 61 if (!images_[current_surface_]->ScheduleOverlayPlane( |
| 62 widget(), 0, gfx::OverlayTransform::OVERLAY_TRANSFORM_NONE, |
| 63 gfx::Rect(GetSize()), gfx::RectF(1, 1))) { |
| 64 callback.Run(gfx::SwapResult::SWAP_FAILED); |
| 65 return; |
| 66 } |
| 67 GbmSurfaceless::SwapBuffersAsync(callback); |
| 68 current_surface_ ^= 1; |
| 69 BindFramebuffer(); |
| 70 } |
| 71 |
| 72 void GbmSurface::Destroy() { |
| 73 if (!context_) |
| 74 return; |
| 75 scoped_refptr<gl::GLContext> previous_context = gl::GLContext::GetCurrent(); |
| 76 scoped_refptr<GLSurface> previous_surface; |
| 77 |
| 78 bool was_current = previous_context && previous_context->IsCurrent(nullptr) && |
| 79 GLSurface::GetCurrent() == this; |
| 80 if (!was_current) { |
| 81 // Only take a reference to previous surface if it's not |this| |
| 82 // because otherwise we can take a self reference from our own dtor. |
| 83 previous_surface = GLSurface::GetCurrent(); |
| 84 context_->MakeCurrent(this); |
| 85 } |
| 86 |
| 87 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); |
| 88 if (fbo_) { |
| 89 glDeleteTextures(arraysize(textures_), textures_); |
| 90 for (auto& texture : textures_) |
| 91 texture = 0; |
| 92 glDeleteFramebuffersEXT(1, &fbo_); |
| 93 fbo_ = 0; |
| 94 } |
| 95 for (auto image : images_) { |
| 96 if (image) |
| 97 image->Destroy(true); |
| 98 } |
| 99 |
| 100 if (!was_current) { |
| 101 if (previous_context) { |
| 102 previous_context->MakeCurrent(previous_surface.get()); |
| 103 } else { |
| 104 context_->ReleaseCurrent(this); |
| 105 } |
| 106 } |
| 107 } |
| 108 |
| 109 bool GbmSurface::IsSurfaceless() const { |
| 110 return false; |
| 111 } |
| 112 |
| 113 GbmSurface::~GbmSurface() { |
| 114 Destroy(); |
| 115 } |
| 116 |
| 117 void GbmSurface::BindFramebuffer() { |
| 118 gl::ScopedFrameBufferBinder fb(fbo_); |
| 119 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 120 textures_[current_surface_], 0); |
| 121 } |
| 122 |
| 123 bool GbmSurface::CreatePixmaps() { |
| 124 if (!fbo_) |
| 125 return true; |
| 126 for (size_t i = 0; i < arraysize(textures_); i++) { |
| 127 scoped_refptr<NativePixmap> pixmap = surface_factory()->CreateNativePixmap( |
| 128 widget(), GetSize(), gfx::BufferFormat::BGRA_8888, |
| 129 gfx::BufferUsage::SCANOUT); |
| 130 if (!pixmap) |
| 131 return false; |
| 132 scoped_refptr<GLImageOzoneNativePixmap> image = |
| 133 new GLImageOzoneNativePixmap(GetSize(), GL_BGRA_EXT); |
| 134 if (!image->Initialize(pixmap.get(), gfx::BufferFormat::BGRA_8888)) |
| 135 return false; |
| 136 // GLImage must have Destroy() called before destructor is called. |
| 137 if (images_[i]) |
| 138 images_[i]->Destroy(true); |
| 139 images_[i] = image; |
| 140 // Bind image to texture. |
| 141 gl::ScopedTextureBinder binder(GL_TEXTURE_2D, textures_[i]); |
| 142 if (!images_[i]->BindTexImage(GL_TEXTURE_2D)) |
| 143 return false; |
| 144 } |
| 145 return true; |
| 146 } |
| 147 |
| 148 } // namespace ui |
OLD | NEW |