Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: ui/gl/gl_surface_ozone.cc

Issue 938873002: Add a new API to create a surfaceless GLSurface for Ozone (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: synchronous swapbuffers Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/gl_surface.h ('k') | ui/ozone/platform/dri/dri_vsync_provider.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
9 #include "ui/gfx/native_widget_types.h" 10 #include "ui/gfx/native_widget_types.h"
10 #include "ui/gl/gl_context.h" 11 #include "ui/gl/gl_context.h"
11 #include "ui/gl/gl_image.h" 12 #include "ui/gl/gl_image.h"
13 #include "ui/gl/gl_image_linux_dma_buffer.h"
12 #include "ui/gl/gl_implementation.h" 14 #include "ui/gl/gl_implementation.h"
13 #include "ui/gl/gl_surface_egl.h" 15 #include "ui/gl/gl_surface_egl.h"
14 #include "ui/gl/gl_surface_osmesa.h" 16 #include "ui/gl/gl_surface_osmesa.h"
15 #include "ui/gl/gl_surface_stub.h" 17 #include "ui/gl/gl_surface_stub.h"
18 #include "ui/gl/scoped_binders.h"
16 #include "ui/gl/scoped_make_current.h" 19 #include "ui/gl/scoped_make_current.h"
20 #include "ui/ozone/public/native_pixmap.h"
17 #include "ui/ozone/public/surface_factory_ozone.h" 21 #include "ui/ozone/public/surface_factory_ozone.h"
18 #include "ui/ozone/public/surface_ozone_egl.h" 22 #include "ui/ozone/public/surface_ozone_egl.h"
19 23
20 namespace gfx { 24 namespace gfx {
21 25
22 namespace { 26 namespace {
23 27
24 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow 28 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
25 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { 29 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL {
26 public: 30 public:
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 return ozone_surface_->OnSwapBuffersAsync(callback); 154 return ozone_surface_->OnSwapBuffersAsync(callback);
151 } 155 }
152 bool PostSubBufferAsync(int x, 156 bool PostSubBufferAsync(int x,
153 int y, 157 int y,
154 int width, 158 int width,
155 int height, 159 int height,
156 const SwapCompletionCallback& callback) override { 160 const SwapCompletionCallback& callback) override {
157 return SwapBuffersAsync(callback); 161 return SwapBuffersAsync(callback);
158 } 162 }
159 163
160 private: 164 protected:
161 ~GLSurfaceOzoneSurfaceless() override { 165 ~GLSurfaceOzoneSurfaceless() override {
162 Destroy(); // EGL surface must be destroyed before SurfaceOzone 166 Destroy(); // EGL surface must be destroyed before SurfaceOzone
163 } 167 }
164 168
165 bool Flush() { 169 bool Flush() {
166 glFlush(); 170 glFlush();
167 // TODO: crbug.com/462360 the following should be replaced by a per surface 171 // TODO: crbug.com/462360 the following should be replaced by a per surface
168 // flush as it gets implemented in GL drivers. 172 // flush as it gets implemented in GL drivers.
169 if (has_implicit_external_sync_) { 173 if (has_implicit_external_sync_) {
170 const EGLint attrib_list[] = { 174 const EGLint attrib_list[] = {
(...skipping 18 matching lines...) Expand all
189 } 193 }
190 194
191 // The native surface. Deleting this is allowed to free the EGLNativeWindow. 195 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
192 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; 196 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
193 AcceleratedWidget widget_; 197 AcceleratedWidget widget_;
194 scoped_ptr<VSyncProvider> vsync_provider_; 198 scoped_ptr<VSyncProvider> vsync_provider_;
195 bool has_implicit_external_sync_; 199 bool has_implicit_external_sync_;
196 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); 200 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless);
197 }; 201 };
198 202
203 // This provides surface-like semantics implemented through surfaceless.
204 // A framebuffer is bound automatically and re-bound at each swap.
205 class GL_EXPORT GLSurfaceOzoneSurfacelessSurfaceImpl
206 : public GLSurfaceOzoneSurfaceless {
207 public:
208 GLSurfaceOzoneSurfacelessSurfaceImpl(
209 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface,
210 AcceleratedWidget widget)
211 : GLSurfaceOzoneSurfaceless(ozone_surface.Pass(), widget),
212 fbo_(0),
213 current_surface_(0) {
214 textures_[0] = 0;
215 textures_[1] = 0;
216 }
217
218 unsigned int GetBackingFrameBufferObject() override { return fbo_; }
219
220 bool OnMakeCurrent(GLContext* context) override {
221 if (!fbo_) {
222 glGenFramebuffersEXT(1, &fbo_);
223 if (!fbo_)
224 return false;
225 glGenTextures(arraysize(textures_), textures_);
226 if (!CreatePixmaps())
227 return false;
228 }
229 BindFramebuffer();
230 return SurfacelessEGL::OnMakeCurrent(context);
231 }
232
233 bool Resize(const gfx::Size& size) override {
234 if (size == GetSize())
235 return true;
236 return GLSurfaceOzoneSurfaceless::Resize(size) && CreatePixmaps();
237 }
238
239 bool SupportsPostSubBuffer() override { return false; }
240
241 bool SwapBuffers() override {
242 if (!images_[current_surface_]->ScheduleOverlayPlane(
243 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE,
244 gfx::Rect(GetSize()), gfx::RectF(1, 1)))
245 return false;
246 if (!GLSurfaceOzoneSurfaceless::SwapBuffers())
247 return false;
248 current_surface_ ^= 1;
249 BindFramebuffer();
250 return true;
251 }
252
253 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override {
254 if (!images_[current_surface_]->ScheduleOverlayPlane(
255 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE,
256 gfx::Rect(GetSize()), gfx::RectF(1, 1)))
257 return false;
258 if (!GLSurfaceOzoneSurfaceless::SwapBuffersAsync(callback))
259 return false;
260 current_surface_ ^= 1;
261 BindFramebuffer();
262 return true;
263 }
264
265 void Destroy() override {
266 GLContext* current_context = GLContext::GetCurrent();
267 DCHECK(current_context && current_context->IsCurrent(this));
268 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
269 if (fbo_) {
270 glDeleteTextures(arraysize(textures_), textures_);
271 memset(textures_, 0, sizeof(textures_));
272 glDeleteFramebuffersEXT(1, &fbo_);
273 fbo_ = 0;
274 }
275 for (size_t i = 0; i < arraysize(textures_); i++) {
piman 2015/02/27 21:53:26 nit: arraysize(images_) That said, you can also u
achaulk 2015/02/27 22:00:10 Oh I didn't know that worked on C-style arrays. Th
276 if (images_[i])
277 images_[i]->Destroy(true);
278 }
279 }
280
281 private:
282 class SurfaceImage : public GLImageLinuxDMABuffer {
283 public:
284 SurfaceImage(const gfx::Size& size, unsigned internalformat)
285 : GLImageLinuxDMABuffer(size, internalformat) {}
286
287 bool Initialize(scoped_refptr<ui::NativePixmap> pixmap,
288 gfx::GpuMemoryBuffer::Format format) {
289 base::FileDescriptor handle(pixmap->GetDmaBufFd(), false);
290 if (!GLImageLinuxDMABuffer::Initialize(handle, format,
291 pixmap->GetDmaBufPitch()))
292 return false;
293 pixmap_ = pixmap;
294 return true;
295 }
296 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
297 int z_order,
298 gfx::OverlayTransform transform,
299 const gfx::Rect& bounds_rect,
300 const gfx::RectF& crop_rect) override {
301 return ui::SurfaceFactoryOzone::GetInstance()->ScheduleOverlayPlane(
302 widget, z_order, transform, pixmap_, bounds_rect, crop_rect);
303 }
304
305 private:
306 scoped_refptr<ui::NativePixmap> pixmap_;
307 };
308
309 ~GLSurfaceOzoneSurfacelessSurfaceImpl() override {
310 DCHECK(!fbo_);
311 DCHECK(!textures_[0]);
312 DCHECK(!textures_[1]);
313 }
314
315 void BindFramebuffer() {
316 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_);
piman 2015/02/27 21:53:26 Use ScopedFrameBufferBinder here to make sure you'
achaulk 2015/02/27 22:00:10 This is intended to modify global state and leave
317 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
318 GL_TEXTURE_2D, textures_[current_surface_], 0);
319 }
320
321 bool CreatePixmaps() {
322 if (!fbo_)
323 return true;
324 for (size_t i = 0; i < arraysize(textures_); i++) {
325 scoped_refptr<ui::NativePixmap> pixmap =
326 ui::SurfaceFactoryOzone::GetInstance()->CreateNativePixmap(
327 widget_, GetSize(), ui::SurfaceFactoryOzone::RGBA_8888,
328 ui::SurfaceFactoryOzone::SCANOUT);
329 if (!pixmap)
330 return false;
331 scoped_refptr<SurfaceImage> image = new SurfaceImage(GetSize(), GL_RGBA);
332 if (!image->Initialize(pixmap, gfx::GpuMemoryBuffer::Format::BGRA_8888))
333 return false;
334 images_[i] = image;
335 // Bind image to texture.
336 ScopedTextureBinder binder(GL_TEXTURE_2D, textures_[i]);
337 if (!images_[i]->BindTexImage(GL_TEXTURE_2D))
338 return false;
339 }
340 return true;
341 }
342
343 GLuint fbo_;
344 GLuint textures_[2];
345 scoped_refptr<GLImage> images_[2];
346 int current_surface_;
347 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl);
348 };
349
199 } // namespace 350 } // namespace
200 351
201 // static 352 // static
202 bool GLSurface::InitializeOneOffInternal() { 353 bool GLSurface::InitializeOneOffInternal() {
203 switch (GetGLImplementation()) { 354 switch (GetGLImplementation()) {
204 case kGLImplementationEGLGLES2: 355 case kGLImplementationEGLGLES2:
205 if (!GLSurfaceEGL::InitializeOneOff()) { 356 if (!GLSurfaceEGL::InitializeOneOff()) {
206 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; 357 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
207 return false; 358 return false;
208 } 359 }
209 360
210 return true; 361 return true;
211 case kGLImplementationOSMesaGL: 362 case kGLImplementationOSMesaGL:
212 case kGLImplementationMockGL: 363 case kGLImplementationMockGL:
213 return true; 364 return true;
214 default: 365 default:
215 return false; 366 return false;
216 } 367 }
217 } 368 }
218 369
219 // static 370 // static
371 scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface(
372 gfx::AcceleratedWidget window) {
373 if (GetGLImplementation() == kGLImplementationEGLGLES2 &&
374 window != kNullAcceleratedWidget &&
375 GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
376 ui::SurfaceFactoryOzone::GetInstance()->CanShowPrimaryPlaneAsOverlay()) {
377 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
378 ui::SurfaceFactoryOzone::GetInstance()
379 ->CreateSurfacelessEGLSurfaceForWidget(window);
380 if (!surface_ozone)
381 return nullptr;
382 scoped_refptr<GLSurface> surface;
383 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window);
384 if (surface->Initialize())
385 return surface;
386 }
387
388 return nullptr;
389 }
390
391 // static
220 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( 392 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
221 gfx::AcceleratedWidget window) { 393 gfx::AcceleratedWidget window) {
222 if (GetGLImplementation() == kGLImplementationOSMesaGL) { 394 if (GetGLImplementation() == kGLImplementationOSMesaGL) {
223 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); 395 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
224 if (!surface->Initialize()) 396 if (!surface->Initialize())
225 return NULL; 397 return NULL;
226 return surface; 398 return surface;
227 } 399 }
228 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); 400 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
229 if (window != kNullAcceleratedWidget) { 401 if (window != kNullAcceleratedWidget) {
230 scoped_refptr<GLSurface> surface; 402 scoped_refptr<GLSurface> surface;
231 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && 403 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
232 ui::SurfaceFactoryOzone::GetInstance() 404 ui::SurfaceFactoryOzone::GetInstance()
233 ->CanShowPrimaryPlaneAsOverlay()) { 405 ->CanShowPrimaryPlaneAsOverlay()) {
234 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = 406 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
235 ui::SurfaceFactoryOzone::GetInstance() 407 ui::SurfaceFactoryOzone::GetInstance()
236 ->CreateSurfacelessEGLSurfaceForWidget(window); 408 ->CreateSurfacelessEGLSurfaceForWidget(window);
237 if (!surface_ozone) 409 if (!surface_ozone)
238 return NULL; 410 return NULL;
239 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); 411 surface = new GLSurfaceOzoneSurfacelessSurfaceImpl(surface_ozone.Pass(),
412 window);
240 } else { 413 } else {
241 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = 414 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
242 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( 415 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(
243 window); 416 window);
244 if (!surface_ozone) 417 if (!surface_ozone)
245 return NULL; 418 return NULL;
246 419
247 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); 420 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window);
248 } 421 }
249 if (!surface->Initialize()) 422 if (!surface->Initialize())
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 NOTREACHED(); 458 NOTREACHED();
286 return NULL; 459 return NULL;
287 } 460 }
288 } 461 }
289 462
290 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { 463 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
291 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); 464 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
292 } 465 }
293 466
294 } // namespace gfx 467 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gl/gl_surface.h ('k') | ui/ozone/platform/dri/dri_vsync_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698