OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/webgl/WebGLQuery.h" | 5 #include "modules/webgl/WebGLQuery.h" |
6 | 6 |
| 7 #include "core/dom/TaskRunnerHelper.h" |
7 #include "gpu/command_buffer/client/gles2_interface.h" | 8 #include "gpu/command_buffer/client/gles2_interface.h" |
8 #include "modules/webgl/WebGL2RenderingContextBase.h" | 9 #include "modules/webgl/WebGL2RenderingContextBase.h" |
9 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
10 | 11 |
11 namespace blink { | 12 namespace blink { |
12 | 13 |
13 WebGLQuery* WebGLQuery::create(WebGL2RenderingContextBase* ctx) | 14 WebGLQuery* WebGLQuery::create(WebGL2RenderingContextBase* ctx) |
14 { | 15 { |
15 return new WebGLQuery(ctx); | 16 return new WebGLQuery(ctx); |
16 } | 17 } |
17 | 18 |
18 WebGLQuery::~WebGLQuery() | 19 WebGLQuery::~WebGLQuery() |
19 { | 20 { |
20 unregisterTaskObserver(); | |
21 | |
22 // See the comment in WebGLObject::detachAndDeleteObject(). | 21 // See the comment in WebGLObject::detachAndDeleteObject(). |
23 detachAndDeleteObject(); | 22 detachAndDeleteObject(); |
24 } | 23 } |
25 | 24 |
26 WebGLQuery::WebGLQuery(WebGL2RenderingContextBase* ctx) | 25 WebGLQuery::WebGLQuery(WebGL2RenderingContextBase* ctx) |
27 : WebGLSharedPlatform3DObject(ctx) | 26 : WebGLSharedPlatform3DObject(ctx) |
28 , m_target(0) | 27 , m_target(0) |
29 , m_taskObserverRegistered(false) | |
30 , m_canUpdateAvailability(false) | 28 , m_canUpdateAvailability(false) |
31 , m_queryResultAvailable(false) | 29 , m_queryResultAvailable(false) |
32 , m_queryResult(0) | 30 , m_queryResult(0) |
| 31 , m_taskRunner(TaskRunnerHelper::get(TaskType::Unthrottled, &ctx->canvas()->
document())->clone()) |
| 32 , m_cancellableTaskFactory(CancellableTaskFactory::create(this, &WebGLQuery:
:allowAvailabilityUpdate)) |
33 { | 33 { |
34 GLuint query; | 34 GLuint query; |
35 ctx->contextGL()->GenQueriesEXT(1, &query); | 35 ctx->contextGL()->GenQueriesEXT(1, &query); |
36 setObject(query); | 36 setObject(query); |
37 } | 37 } |
38 | 38 |
39 void WebGLQuery::setTarget(GLenum target) | 39 void WebGLQuery::setTarget(GLenum target) |
40 { | 40 { |
41 ASSERT(object()); | 41 ASSERT(object()); |
42 ASSERT(!m_target); | 42 ASSERT(!m_target); |
43 m_target = target; | 43 m_target = target; |
44 } | 44 } |
45 | 45 |
46 void WebGLQuery::deleteObjectImpl(gpu::gles2::GLES2Interface* gl) | 46 void WebGLQuery::deleteObjectImpl(gpu::gles2::GLES2Interface* gl) |
47 { | 47 { |
48 gl->DeleteQueriesEXT(1, &m_object); | 48 gl->DeleteQueriesEXT(1, &m_object); |
49 m_object = 0; | 49 m_object = 0; |
50 } | 50 } |
51 | 51 |
52 void WebGLQuery::resetCachedResult() | 52 void WebGLQuery::resetCachedResult() |
53 { | 53 { |
54 m_canUpdateAvailability = false; | 54 m_canUpdateAvailability = false; |
55 m_queryResultAvailable = false; | 55 m_queryResultAvailable = false; |
56 m_queryResult = 0; | 56 m_queryResult = 0; |
57 // When this is called, the implication is that we should start | 57 // When this is called, the implication is that we should start |
58 // keeping track of whether we can update the cached availability | 58 // keeping track of whether we can update the cached availability |
59 // and result. | 59 // and result. |
60 registerTaskObserver(); | 60 scheduleAllowAvailabilityUpdate(); |
61 } | 61 } |
62 | 62 |
63 void WebGLQuery::updateCachedResult(gpu::gles2::GLES2Interface* gl) | 63 void WebGLQuery::updateCachedResult(gpu::gles2::GLES2Interface* gl) |
64 { | 64 { |
65 if (m_queryResultAvailable) | 65 if (m_queryResultAvailable) |
66 return; | 66 return; |
67 | 67 |
68 if (!m_canUpdateAvailability) | 68 if (!m_canUpdateAvailability) |
69 return; | 69 return; |
70 | 70 |
71 if (!hasTarget()) | 71 if (!hasTarget()) |
72 return; | 72 return; |
73 | 73 |
74 // We can only update the cached result when control returns to the browser. | 74 // We can only update the cached result when control returns to the browser. |
75 m_canUpdateAvailability = false; | 75 m_canUpdateAvailability = false; |
76 GLuint available = 0; | 76 GLuint available = 0; |
77 gl->GetQueryObjectuivEXT(object(), GL_QUERY_RESULT_AVAILABLE_EXT, &available
); | 77 gl->GetQueryObjectuivEXT(object(), GL_QUERY_RESULT_AVAILABLE_EXT, &available
); |
78 m_queryResultAvailable = !!available; | 78 m_queryResultAvailable = !!available; |
79 if (m_queryResultAvailable) { | 79 if (m_queryResultAvailable) { |
80 GLuint result = 0; | 80 GLuint result = 0; |
81 gl->GetQueryObjectuivEXT(object(), GL_QUERY_RESULT_EXT, &result); | 81 gl->GetQueryObjectuivEXT(object(), GL_QUERY_RESULT_EXT, &result); |
82 m_queryResult = result; | 82 m_queryResult = result; |
83 unregisterTaskObserver(); | 83 m_cancellableTaskFactory->cancel(); |
| 84 } else { |
| 85 scheduleAllowAvailabilityUpdate(); |
84 } | 86 } |
85 } | 87 } |
86 | 88 |
87 bool WebGLQuery::isQueryResultAvailable() | 89 bool WebGLQuery::isQueryResultAvailable() |
88 { | 90 { |
89 return m_queryResultAvailable; | 91 return m_queryResultAvailable; |
90 } | 92 } |
91 | 93 |
92 GLuint WebGLQuery::getQueryResult() | 94 GLuint WebGLQuery::getQueryResult() |
93 { | 95 { |
94 return m_queryResult; | 96 return m_queryResult; |
95 } | 97 } |
96 | 98 |
97 void WebGLQuery::registerTaskObserver() | 99 void WebGLQuery::scheduleAllowAvailabilityUpdate() |
98 { | 100 { |
99 if (!m_taskObserverRegistered) { | 101 if (!m_cancellableTaskFactory->isPending()) |
100 m_taskObserverRegistered = true; | 102 m_taskRunner->postTask(BLINK_FROM_HERE, m_cancellableTaskFactory->cancel
AndCreate()); |
101 Platform::current()->currentThread()->addTaskObserver(this); | |
102 } | |
103 } | 103 } |
104 | 104 |
105 void WebGLQuery::unregisterTaskObserver() | 105 void WebGLQuery::allowAvailabilityUpdate() |
106 { | |
107 if (m_taskObserverRegistered) { | |
108 m_taskObserverRegistered = false; | |
109 Platform::current()->currentThread()->removeTaskObserver(this); | |
110 } | |
111 } | |
112 | |
113 void WebGLQuery::didProcessTask() | |
114 { | 106 { |
115 m_canUpdateAvailability = true; | 107 m_canUpdateAvailability = true; |
116 } | 108 } |
117 | 109 |
118 } // namespace blink | 110 } // namespace blink |
OLD | NEW |