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_nv.h"
6
7#include "ui/gl/gl_bindings.h"
8#include "ui/gl/gl_context.h"
9
10namespace gfx {
11
12GLFenceNV::GLFenceNV(bool flush) {
13  // What if either of these GL calls fails? TestFenceNV will return true.
14  // See spec:
15  // http://www.opengl.org/registry/specs/NV/fence.txt
16  //
17  // What should happen if TestFenceNV is called for a name before SetFenceNV
18  // is called?
19  //     We generate an INVALID_OPERATION error, and return TRUE.
20  //     This follows the semantics for texture object names before
21  //     they are bound, in that they acquire their state upon binding.
22  //     We will arbitrarily return TRUE for consistency.
23  glGenFencesNV(1, &fence_);
24  glSetFenceNV(fence_, GL_ALL_COMPLETED_NV);
25  DCHECK(glIsFenceNV(fence_));
26  if (flush) {
27    glFlush();
28  } else {
29    flush_event_ = GLContext::GetCurrent()->SignalFlush();
30  }
31}
32
33bool GLFenceNV::HasCompleted() {
34  DCHECK(glIsFenceNV(fence_));
35  return !!glTestFenceNV(fence_);
36}
37
38void GLFenceNV::ClientWait() {
39  DCHECK(glIsFenceNV(fence_));
40  if (!flush_event_.get() || flush_event_->IsSignaled()) {
41    glFinishFenceNV(fence_);
42  } else {
43    LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping...";
44  }
45}
46
47void GLFenceNV::ServerWait() {
48  DCHECK(glIsFenceNV(fence_));
49  ClientWait();
50}
51
52GLFenceNV::~GLFenceNV() {
53  DCHECK(glIsFenceNV(fence_));
54  glDeleteFencesNV(1, &fence_);
55}
56
57}  // namespace gfx
58