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.
5 #include "ui/gl/gl_fence_arb.h"
7 #include "base/strings/stringprintf.h"
8 #include "ui/gl/gl_bindings.h"
9 #include "ui/gl/gl_context.h"
15 std::string
GetGLErrors() {
16 // Clears and logs all current gl errors.
17 std::string accumulated_errors
;
19 while ((error
= glGetError()) != GL_NO_ERROR
) {
20 accumulated_errors
+= base::StringPrintf("0x%x ", error
);
22 return accumulated_errors
;
27 GLFenceARB::GLFenceARB(bool flush
) {
28 sync_
= glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE
, 0);
29 DCHECK_EQ(GL_TRUE
, glIsSync(sync_
));
33 flush_event_
= GLContext::GetCurrent()->SignalFlush();
37 bool GLFenceARB::HasCompleted() {
38 // Handle the case where FenceSync failed.
42 DCHECK_EQ(GL_TRUE
, glIsSync(sync_
));
43 // We could potentially use glGetSynciv here, but it doesn't work
44 // on OSX 10.7 (always says the fence is not signaled yet).
45 // glClientWaitSync works better, so let's use that instead.
46 GLenum result
= glClientWaitSync(sync_
, 0, 0);
47 if (result
== GL_WAIT_FAILED
) {
48 LOG(FATAL
) << "Failed to wait for GLFence. error code:" << GetGLErrors();
50 return result
!= GL_TIMEOUT_EXPIRED
;
53 void GLFenceARB::ClientWait() {
54 DCHECK_EQ(GL_TRUE
, glIsSync(sync_
));
55 if (!flush_event_
.get() || flush_event_
->IsSignaled()) {
57 glClientWaitSync(sync_
, GL_SYNC_FLUSH_COMMANDS_BIT
, GL_TIMEOUT_IGNORED
);
58 DCHECK_NE(static_cast<GLenum
>(GL_TIMEOUT_EXPIRED
), result
);
59 if (result
== GL_WAIT_FAILED
) {
60 LOG(FATAL
) << "Failed to wait for GLFence. error code:" << GetGLErrors();
63 LOG(ERROR
) << "Trying to wait for uncommitted fence. Skipping...";
67 void GLFenceARB::ServerWait() {
68 DCHECK_EQ(GL_TRUE
, glIsSync(sync_
));
69 if (!flush_event_
.get() || flush_event_
->IsSignaled()) {
70 glWaitSync(sync_
, 0, GL_TIMEOUT_IGNORED
);
72 LOG(ERROR
) << "Trying to wait for uncommitted fence. Skipping...";
76 GLFenceARB::~GLFenceARB() {
77 DCHECK_EQ(GL_TRUE
, glIsSync(sync_
));