DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebGLExtensions.h"

#include "mozilla/dom/ToJSValue.h"
#include "mozilla/dom/WebGLRenderingContextBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "GLContext.h"
#include "WebGLContext.h"
#include "WebGLTimerQuery.h"

namespace mozilla {

WebGLExtensionDisjointTimerQuery::WebGLExtensionDisjointTimerQuery(WebGLContext* webgl)
  : WebGLExtensionBase(webgl)
  , mActiveQuery(nullptr)
{
  MOZ_ASSERT(IsSupported(webgl), "Don't construct extension if unsupported.");
}

WebGLExtensionDisjointTimerQuery::~WebGLExtensionDisjointTimerQuery()
{
}

already_AddRefed<WebGLTimerQuery>
WebGLExtensionDisjointTimerQuery::CreateQueryEXT()
{
  if (mIsLost)
    return nullptr;

  RefPtr<WebGLTimerQuery> query = WebGLTimerQuery::Create(mContext);
  return query.forget();
}

void
WebGLExtensionDisjointTimerQuery::DeleteQueryEXT(WebGLTimerQuery* query)
{
  if (mIsLost)
    return;

  if (!mContext->ValidateObject("deleteQueryEXT", query))
    return;

  query->RequestDelete();
}

bool
WebGLExtensionDisjointTimerQuery::IsQueryEXT(WebGLTimerQuery* query)
{
  if (!query)
    return false;

  if (!mContext->ValidateObjectAllowDeleted("isQueryEXT", query))
    return false;

  if (query->IsDeleted())
    return false;

  return true;
}

void
WebGLExtensionDisjointTimerQuery::BeginQueryEXT(GLenum target,
                                                WebGLTimerQuery* query)
{
  if (mIsLost)
    return;

  if (!mContext->ValidateObject("beginQueryEXT", query))
    return;

  if (query->HasEverBeenBound() && query->Target() != target) {
    mContext->ErrorInvalidOperation("beginQueryEXT: Query is already bound"
                                    " to a different target.");
    return;
  }

  if (target != LOCAL_GL_TIME_ELAPSED_EXT) {
    mContext->ErrorInvalidEnumInfo("beginQueryEXT: Can only begin on target"
                                   " TIME_ELAPSED_EXT.", target);
    return;
  }

  if (mActiveQuery) {
    mContext->ErrorInvalidOperation("beginQueryEXT: A query is already"
                                    " active.");
    return;
  }

  mContext->MakeContextCurrent();
  gl::GLContext* gl = mContext->GL();
  gl->fBeginQuery(target, query->mGLName);
  query->mTarget = LOCAL_GL_TIME_ELAPSED_EXT;
  mActiveQuery = query;
}

void
WebGLExtensionDisjointTimerQuery::EndQueryEXT(GLenum target)
{
  if (mIsLost)
    return;

  if (target != LOCAL_GL_TIME_ELAPSED_EXT) {
    mContext->ErrorInvalidEnumInfo("endQueryEXT: Can only end on"
                                   " TIME_ELAPSED_EXT.", target);
    return;
  }

  if (!mActiveQuery) {
    mContext->ErrorInvalidOperation("endQueryEXT: A query is not active.");
    return;
  }

  mContext->MakeContextCurrent();
  mContext->GL()->fEndQuery(target);
  mActiveQuery = nullptr;
}

void
WebGLExtensionDisjointTimerQuery::QueryCounterEXT(WebGLTimerQuery* query,
                                                  GLenum target)
{
  if (mIsLost)
    return;

  if (!mContext->ValidateObject("queryCounterEXT", query))
    return;

  if (target != LOCAL_GL_TIMESTAMP_EXT) {
    mContext->ErrorInvalidEnumInfo("queryCounterEXT: requires"
                                   " TIMESTAMP_EXT.", target);
    return;
  }

  mContext->MakeContextCurrent();
  mContext->GL()->fQueryCounter(query->mGLName, target);
  query->mTarget = LOCAL_GL_TIMESTAMP_EXT;
}

void
WebGLExtensionDisjointTimerQuery::GetQueryEXT(JSContext* cx, GLenum target,
                                              GLenum pname,
                                              JS::MutableHandle<JS::Value> retval)
{
  if (mIsLost)
    return;

  mContext->MakeContextCurrent();
  switch (pname) {
  case LOCAL_GL_CURRENT_QUERY_EXT: {
    if (target != LOCAL_GL_TIME_ELAPSED_EXT) {
      mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query target.",
                                     target);
      return;
    }
    if (mActiveQuery) {
      JS::Rooted<JS::Value> v(cx);
      dom::GetOrCreateDOMReflector(cx, mActiveQuery.get(), &v);
      retval.set(v);
    } else {
      retval.set(JS::NullValue());
    }
    break;
  }
  case LOCAL_GL_QUERY_COUNTER_BITS_EXT: {
    if (target != LOCAL_GL_TIME_ELAPSED_EXT &&
        target != LOCAL_GL_TIMESTAMP_EXT) {
      mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query target.",
                                     target);
      return;
    }
    GLint bits = 0;
    mContext->GL()->fGetQueryiv(target, pname, &bits);
    retval.set(JS::Int32Value(int32_t(bits)));
    break;
  }
  default:
    mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query property.",
                                   pname);
    break;
  }
}

void
WebGLExtensionDisjointTimerQuery::GetQueryObjectEXT(JSContext* cx,
                                                    WebGLTimerQuery* query,
                                                    GLenum pname,
                                                    JS::MutableHandle<JS::Value> retval)
{
  if (mIsLost)
    return;

  if (!mContext->ValidateObject("getQueryObjectEXT", query))
    return;

  if (query == mActiveQuery.get()) {
    mContext->ErrorInvalidOperation("getQueryObjectEXT: Query must not be"
                                    " active.");
  }

  mContext->MakeContextCurrent();
  // XXX: Note that the query result *may change* within the same task!
  // This does not follow the specification, which states that all calls
  // checking query results must return the same value until the event loop
  // is empty.
  switch (pname) {
  case LOCAL_GL_QUERY_RESULT_EXT: {
    GLuint64 result = 0;
    mContext->GL()->fGetQueryObjectui64v(query->mGLName,
                                         LOCAL_GL_QUERY_RESULT_EXT,
                                         &result);
    retval.set(JS::NumberValue(result));
    break;
  }
  case LOCAL_GL_QUERY_RESULT_AVAILABLE_EXT: {
    GLuint avail = 0;
    mContext->GL()->fGetQueryObjectuiv(query->mGLName,
                                       LOCAL_GL_QUERY_RESULT_AVAILABLE_EXT,
                                       &avail);
    retval.set(JS::BooleanValue(bool(avail)));
    break;
  }
  default:
    mContext->ErrorInvalidEnumInfo("getQueryObjectEXT: Invalid query"
                                   " property.", pname);
    break;
  }
}

bool
WebGLExtensionDisjointTimerQuery::IsSupported(const WebGLContext* webgl)
{
  webgl->MakeContextCurrent();
  gl::GLContext* gl = webgl->GL();
  return gl->IsSupported(gl::GLFeature::query_objects) &&
         gl->IsSupported(gl::GLFeature::get_query_object_i64v) &&
         gl->IsSupported(gl::GLFeature::query_counter); // provides GL_TIMESTAMP
}


IMPL_WEBGL_EXTENSION_GOOP(WebGLExtensionDisjointTimerQuery, EXT_disjoint_timer_query)

} // namespace mozilla