Simple Cache: a few tests for rare corner cases with CRC check missing.
[chromium-blink-merge.git] / ui / gl / gl_fence.cc
blob9a40ec2a8b652f11135be687c46aeef9a22216fb
1 // Copyright (c) 2012 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.h"
7 #include "base/compiler_specific.h"
8 #include "ui/gl/gl_bindings.h"
9 #include "ui/gl/gl_context.h"
11 namespace {
13 class GLFenceNVFence: public gfx::GLFence {
14 public:
15 GLFenceNVFence() {
16 // What if either of these GL calls fails? TestFenceNV will return true.
17 // See spec:
18 // http://www.opengl.org/registry/specs/NV/fence.txt
20 // What should happen if TestFenceNV is called for a name before SetFenceNV
21 // is called?
22 // We generate an INVALID_OPERATION error, and return TRUE.
23 // This follows the semantics for texture object names before
24 // they are bound, in that they acquire their state upon binding.
25 // We will arbitrarily return TRUE for consistency.
26 glGenFencesNV(1, &fence_);
27 glSetFenceNV(fence_, GL_ALL_COMPLETED_NV);
28 glFlush();
31 virtual bool HasCompleted() OVERRIDE {
32 return !!glTestFenceNV(fence_);
35 private:
36 virtual ~GLFenceNVFence() {
37 glDeleteFencesNV(1, &fence_);
40 GLuint fence_;
43 class GLFenceARBSync: public gfx::GLFence {
44 public:
45 GLFenceARBSync() {
46 sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
47 glFlush();
50 virtual bool HasCompleted() OVERRIDE {
51 // Handle the case where FenceSync failed.
52 if (!sync_)
53 return true;
55 GLsizei length = 0;
56 GLsizei value = 0;
57 glGetSynciv(sync_,
58 GL_SYNC_STATUS,
59 1, // bufSize
60 &length,
61 &value);
62 return length == 1 && value == GL_SIGNALED;
65 private:
66 virtual ~GLFenceARBSync() {
67 glDeleteSync(sync_);
70 GLsync sync_;
73 } // namespace
75 namespace gfx {
77 GLFence::GLFence() {
80 GLFence::~GLFence() {
83 // static
84 GLFence* GLFence::Create() {
85 if (gfx::g_driver_gl.ext.b_GL_NV_fence) {
86 return new GLFenceNVFence();
87 } else if (gfx::g_driver_gl.ext.b_GL_ARB_sync) {
88 return new GLFenceARBSync();
89 } else {
90 return NULL;
94 } // namespace gfx