summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/modules/webgl/EXTDisjointTimerQuery.cpp
blob: 2217f40cc0e6ee4aa467ca7c6ad63c151ef02c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"

#include "modules/webgl/EXTDisjointTimerQuery.h"

#include "bindings/modules/v8/WebGLAny.h"
#include "modules/webgl/WebGLRenderingContextBase.h"
#include "modules/webgl/WebGLTimerQueryEXT.h"

namespace blink {

EXTDisjointTimerQuery::~EXTDisjointTimerQuery()
{
}

WebGLExtensionName EXTDisjointTimerQuery::name() const
{
    return EXTDisjointTimerQueryName;
}

EXTDisjointTimerQuery* EXTDisjointTimerQuery::create(WebGLRenderingContextBase* context)
{
    EXTDisjointTimerQuery* o = new EXTDisjointTimerQuery(context);
    return o;
}

bool EXTDisjointTimerQuery::supported(WebGLRenderingContextBase* context)
{
    return context->extensionsUtil()->supportsExtension("GL_EXT_disjoint_timer_query");
}

const char* EXTDisjointTimerQuery::extensionName()
{
    return "EXT_disjoint_timer_query";
}

WebGLTimerQueryEXT* EXTDisjointTimerQuery::createQueryEXT()
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return nullptr;

    WebGLTimerQueryEXT* o = WebGLTimerQueryEXT::create(scoped.context());
    scoped.context()->addContextObject(o);
    return o;
}

void EXTDisjointTimerQuery::deleteQueryEXT(WebGLTimerQueryEXT* query)
{
    WebGLExtensionScopedContext scoped(this);
    if (!query || scoped.isLost())
        return;
    query->deleteObject(scoped.context()->webContext());

    if (query == m_currentElapsedQuery)
        m_currentElapsedQuery.clear();
}

GLboolean EXTDisjointTimerQuery::isQueryEXT(WebGLTimerQueryEXT* query)
{
    WebGLExtensionScopedContext scoped(this);
    if (!query || scoped.isLost() || query->isDeleted() || !query->validate(0, scoped.context())) {
        return false;
    }

    return scoped.context()->webContext()->isQueryEXT(query->object());
}

void EXTDisjointTimerQuery::beginQueryEXT(GLenum target, WebGLTimerQueryEXT* query)
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return;

    if (!query || query->isDeleted() || !query->validate(0, scoped.context())) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    if (target != GL_TIME_ELAPSED_EXT) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_ENUM);
        return;
    }

    if (m_currentElapsedQuery.get()) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    if (query->hasTarget() && query->target() != target) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    scoped.context()->webContext()->beginQueryEXT(target, query->object());
    query->setTarget(target);
    m_currentElapsedQuery = query;
}

void EXTDisjointTimerQuery::endQueryEXT(GLenum target)
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return;

    if (target != GL_TIME_ELAPSED_EXT) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_ENUM);
        return;
    }

    if (!m_currentElapsedQuery) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    scoped.context()->webContext()->endQueryEXT(target);
    m_currentElapsedQuery->resetCachedResult();
    m_currentElapsedQuery.clear();
}

void EXTDisjointTimerQuery::queryCounterEXT(WebGLTimerQueryEXT* query, GLenum target)
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return;

    if (!query || query->isDeleted() || !query->validate(0, scoped.context())) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    if (target != GL_TIMESTAMP_EXT) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_ENUM);
        return;
    }

    if (query->hasTarget() && query->target() != target) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return;
    }

    scoped.context()->webContext()->queryCounterEXT(query->object(), target);
    query->setTarget(target);
    query->resetCachedResult();
}

ScriptValue EXTDisjointTimerQuery::getQueryEXT(ScriptState* scriptState, GLenum target, GLenum pname)
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return ScriptValue::createNull(scriptState);

    if (target == GL_TIMESTAMP_EXT || target == GL_TIME_ELAPSED_EXT) {
        switch (pname) {
        case GL_CURRENT_QUERY_EXT:
            if (GL_TIME_ELAPSED_EXT == target && m_currentElapsedQuery.get())
                return WebGLAny(scriptState, m_currentElapsedQuery.get());
            return ScriptValue::createNull(scriptState);
        case GL_QUERY_COUNTER_BITS_EXT: {
            GLint value = 0;
            scoped.context()->webContext()->getQueryivEXT(target, pname, &value);
            return WebGLAny(scriptState, value);
        }
        default:
            break;
        }
    }

    scoped.context()->webContext()->synthesizeGLError(GL_INVALID_ENUM);
    return ScriptValue::createNull(scriptState);
}

ScriptValue EXTDisjointTimerQuery::getQueryObjectEXT(ScriptState* scriptState, WebGLTimerQueryEXT* query, GLenum pname)
{
    WebGLExtensionScopedContext scoped(this);
    if (scoped.isLost())
        return ScriptValue::createNull(scriptState);

    if (!query || query->isDeleted() || !query->validate(0, scoped.context()) || m_currentElapsedQuery == query) {
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_OPERATION);
        return ScriptValue::createNull(scriptState);
    }

    switch (pname) {
    case GL_QUERY_RESULT_EXT: {
        query->updateCachedResult(scoped.context()->webContext());
        return WebGLAny(scriptState, query->getQueryResult());
    }
    case GL_QUERY_RESULT_AVAILABLE_EXT: {
        query->updateCachedResult(scoped.context()->webContext());
        return WebGLAny(scriptState, query->isQueryResultAvailable());
    }
    default:
        scoped.context()->webContext()->synthesizeGLError(GL_INVALID_ENUM);
        break;
    }

    return ScriptValue::createNull(scriptState);
}

DEFINE_TRACE(EXTDisjointTimerQuery)
{
    visitor->trace(m_currentElapsedQuery);
    WebGLExtension::trace(visitor);
}

EXTDisjointTimerQuery::EXTDisjointTimerQuery(WebGLRenderingContextBase* context)
    : WebGLExtension(context)
{
    context->extensionsUtil()->ensureExtensionEnabled("GL_EXT_disjoint_timer_query");
}

} // namespace blink