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
3 // found in the LICENSE file.
4
5 #include "ui/gl/gl_fence_egl.h"
6
7 #include "ui/gl/gl_bindings.h"
8 #include "ui/gl/gl_context.h"
9
10 namespace gfx {
11
GLFenceEGL(bool flush)12 GLFenceEGL::GLFenceEGL(bool flush) {
13 display_ = eglGetCurrentDisplay();
14 sync_ = eglCreateSyncKHR(display_, EGL_SYNC_FENCE_KHR, NULL);
15 DCHECK(sync_ != EGL_NO_SYNC_KHR);
16 if (flush) {
17 glFlush();
18 } else {
19 flush_event_ = GLContext::GetCurrent()->SignalFlush();
20 }
21 }
22
HasCompleted()23 bool GLFenceEGL::HasCompleted() {
24 EGLint value = 0;
25 if (eglGetSyncAttribKHR(display_, sync_, EGL_SYNC_STATUS_KHR, &value) !=
26 EGL_TRUE) {
27 return true;
28 }
29 DCHECK(value == EGL_SIGNALED_KHR || value == EGL_UNSIGNALED_KHR);
30 return !value || value == EGL_SIGNALED_KHR;
31 }
32
ClientWait()33 void GLFenceEGL::ClientWait() {
34 if (!flush_event_ || flush_event_->IsSignaled()) {
35 EGLint flags = 0;
36 EGLTimeKHR time = EGL_FOREVER_KHR;
37 eglClientWaitSyncKHR(display_, sync_, flags, time);
38 } else {
39 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping...";
40 }
41 }
42
ServerWait()43 void GLFenceEGL::ServerWait() {
44 if (!gfx::g_driver_egl.ext.b_EGL_KHR_wait_sync) {
45 ClientWait();
46 return;
47 }
48 if (!flush_event_ || flush_event_->IsSignaled()) {
49 EGLint flags = 0;
50 eglWaitSyncKHR(display_, sync_, flags);
51 } else {
52 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping...";
53 }
54 }
55
~GLFenceEGL()56 GLFenceEGL::~GLFenceEGL() {
57 eglDestroySyncKHR(display_, sync_);
58 }
59
60 } // namespace gfx
61