Bug 1048721 - Implement WebGL2Sync. r=jgilbert
authorDan Glastonbury <dglastonbury@mozilla.com>
Wed, 18 Jun 2014 14:25:53 +1000
changeset 263875 f5957afe1f8d1b985efcaa9675f27d0460b52177
parent 263865 5e0659bb1220312e6703f38370810d0448358358
child 263876 d193686b3d0ba07e052181a05ff8d993b413d594
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjgilbert
bugs1048721
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1048721 - Implement WebGL2Sync. r=jgilbert
dom/canvas/WebGL2ContextSync.cpp
dom/canvas/WebGLContext.h
dom/canvas/WebGLSync.cpp
dom/canvas/WebGLSync.h
--- a/dom/canvas/WebGL2ContextSync.cpp
+++ b/dom/canvas/WebGL2ContextSync.cpp
@@ -1,52 +1,133 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* 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 "WebGL2Context.h"
+
 #include "GLContext.h"
+#include "WebGLSync.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 // -------------------------------------------------------------------------
 // Sync objects
 
 already_AddRefed<WebGLSync>
 WebGL2Context::FenceSync(GLenum condition, GLbitfield flags)
 {
-    MOZ_CRASH("Not Implemented.");
-    return nullptr;
+   if (IsContextLost())
+       return nullptr;
+
+   if (condition != LOCAL_GL_SYNC_GPU_COMMANDS_COMPLETE) {
+       ErrorInvalidEnum("fenceSync: condition must be SYNC_GPU_COMMANDS_COMPLETE");
+       return nullptr;
+   }
+
+   if (flags != 0) {
+       ErrorInvalidValue("fenceSync: flags must be 0");
+       return nullptr;
+   }
+
+   MakeContextCurrent();
+   nsRefPtr<WebGLSync> globj = new WebGLSync(this, condition, flags);
+   return globj.forget();
 }
 
 bool
 WebGL2Context::IsSync(WebGLSync* sync)
 {
-    MOZ_CRASH("Not Implemented.");
-    return false;
+   if (IsContextLost())
+       return false;
+
+   return ValidateObjectAllowDeleted("isSync", sync) && !sync->IsDeleted();
 }
 
 void
 WebGL2Context::DeleteSync(WebGLSync* sync)
 {
-    MOZ_CRASH("Not Implemented.");
+    if (IsContextLost())
+        return;
+
+    if (!ValidateObjectAllowDeletedOrNull("deleteSync", sync))
+        return;
+
+    if (!sync || sync->IsDeleted())
+        return;
+
+    sync->RequestDelete();
 }
 
 GLenum
 WebGL2Context::ClientWaitSync(WebGLSync* sync, GLbitfield flags, GLuint64 timeout)
 {
-    MOZ_CRASH("Not Implemented.");
-    return LOCAL_GL_FALSE;
+    if (IsContextLost())
+        return LOCAL_GL_WAIT_FAILED;
+
+    if (!sync || sync->IsDeleted()) {
+        ErrorInvalidValue("clientWaitSync: sync is not a sync object.");
+        return LOCAL_GL_WAIT_FAILED;
+    }
+
+    if (flags != 0 && flags != LOCAL_GL_SYNC_FLUSH_COMMANDS_BIT) {
+        ErrorInvalidValue("clientWaitSync: flag must be SYNC_FLUSH_COMMANDS_BIT or 0");
+        return LOCAL_GL_WAIT_FAILED;
+    }
+
+    MakeContextCurrent();
+    return gl->fClientWaitSync(sync->mGLName, flags, timeout);
 }
 
 void
 WebGL2Context::WaitSync(WebGLSync* sync, GLbitfield flags, GLuint64 timeout)
 {
-    MOZ_CRASH("Not Implemented.");
+    if (IsContextLost())
+        return;
+
+    if (!sync || sync->IsDeleted()) {
+        ErrorInvalidValue("waitSync: sync is not a sync object.");
+        return;
+    }
+
+    if (flags != 0) {
+        ErrorInvalidValue("waitSync: flags must be 0");
+        return;
+    }
+
+    if (timeout != LOCAL_GL_TIMEOUT_IGNORED) {
+        ErrorInvalidValue("waitSync: timeout must be TIMEOUT_IGNORED");
+        return;
+    }
+
+    MakeContextCurrent();
+    gl->fWaitSync(sync->mGLName, flags, timeout);
 }
 
 void
 WebGL2Context::GetSyncParameter(JSContext*, WebGLSync* sync, GLenum pname, JS::MutableHandleValue retval)
 {
-    MOZ_CRASH("Not Implemented.");
+    if (IsContextLost())
+        return;
+
+    if (!sync || sync->IsDeleted()) {
+        ErrorInvalidValue("getSyncParameter: sync is not a sync object.");
+        return;
+    }
+
+    retval.set(JS::NullValue());
+
+    GLint result = 0;
+    switch (pname) {
+    case LOCAL_GL_OBJECT_TYPE:
+    case LOCAL_GL_SYNC_STATUS:
+    case LOCAL_GL_SYNC_CONDITION:
+    case LOCAL_GL_SYNC_FLAGS:
+        MakeContextCurrent();
+        gl->fGetSynciv(sync->mGLName, pname, 1, nullptr, &result);
+        retval.set(JS::Int32Value(result));
+        break;
+    }
+
+    ErrorInvalidEnum("getSyncParameter: Invalid pname 0x%04x", pname);
 }
--- a/dom/canvas/WebGLContext.h
+++ b/dom/canvas/WebGLContext.h
@@ -1586,16 +1586,17 @@ public:
     friend class WebGLTexture;
     friend class WebGLFramebuffer;
     friend class WebGLRenderbuffer;
     friend class WebGLProgram;
     friend class WebGLQuery;
     friend class WebGLBuffer;
     friend class WebGLSampler;
     friend class WebGLShader;
+    friend class WebGLSync;
     friend class WebGLTransformFeedback;
     friend class WebGLUniformLocation;
     friend class WebGLVertexArray;
     friend class WebGLVertexArrayFake;
     friend class WebGLVertexArrayGL;
 };
 
 // used by DOM bindings in conjunction with GetParentObject
--- a/dom/canvas/WebGLSync.cpp
+++ b/dom/canvas/WebGLSync.cpp
@@ -4,36 +4,40 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "WebGLSync.h"
 
 #include "mozilla/dom/WebGL2RenderingContextBinding.h"
 
 namespace mozilla {
 
-WebGLSync::WebGLSync(WebGLContext* webgl):
-    WebGLContextBoundObject(webgl)
+WebGLSync::WebGLSync(WebGLContext* webgl, GLenum condition, GLbitfield flags)
+    : WebGLContextBoundObject(webgl)
 {
-    MOZ_CRASH("Not Implemented.");
+   mGLName = mContext->gl->fFenceSync(condition, flags);
 }
 
 WebGLSync::~WebGLSync()
-{}
+{
+    DeleteOnce();
+}
 
 void
 WebGLSync::Delete()
 {
-    MOZ_CRASH("Not Implemented.");
+    mContext->MakeContextCurrent();
+    mContext->gl->fDeleteSync(mGLName);
+    mGLName = 0;
+    LinkedListElement<WebGLSync>::remove();
 }
 
 WebGLContext*
 WebGLSync::GetParentObject() const
 {
-    MOZ_CRASH("Not Implemented.");
-    return nullptr;
+    return Context();
 }
 
 // -------------------------------------------------------------------------
 // IMPLEMENT NS
 JSObject*
 WebGLSync::WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto)
 {
     return dom::WebGLSyncBinding::Wrap(cx, this, aGivenProto);
--- a/dom/canvas/WebGLSync.h
+++ b/dom/canvas/WebGLSync.h
@@ -16,25 +16,27 @@ class WebGLSync final
     : public nsWrapperCache
     , public WebGLRefCountedObject<WebGLSync>
     , public LinkedListElement<WebGLSync>
     , public WebGLContextBoundObject
 {
     friend class WebGL2Context;
 
 public:
-    explicit WebGLSync(WebGLContext* webgl);
+    WebGLSync(WebGLContext* webgl, GLenum condition, GLbitfield flags);
 
     void Delete();
     WebGLContext* GetParentObject() const;
 
     virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto) override;
 
     NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLSync)
     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLSync)
 
 private:
     ~WebGLSync();
+
+    GLsync mGLName;
 };
 
 } // namespace mozilla
 
 #endif // WEBGL_SYNC_H_