Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_surface_glx.cc
index 58f97f5..10f0681 100644 (file)
@@ -15,12 +15,13 @@ extern "C" {
 #include "base/memory/scoped_ptr.h"
 #include "base/memory/weak_ptr.h"
 #include "base/message_loop/message_loop.h"
+#include "base/single_thread_task_runner.h"
 #include "base/synchronization/cancellation_flag.h"
 #include "base/synchronization/lock.h"
+#include "base/thread_task_runner_handle.h"
 #include "base/threading/non_thread_safe.h"
 #include "base/threading/thread.h"
 #include "base/time/time.h"
-#include "third_party/mesa/src/include/GL/osmesa.h"
 #include "ui/events/platform/platform_event_source.h"
 #include "ui/gfx/x/x11_connection.h"
 #include "ui/gfx/x/x11_types.h"
@@ -55,6 +56,19 @@ bool g_glx_get_msc_rate_oml_supported = false;
 
 bool g_glx_sgi_video_sync_supported = false;
 
+static const base::TimeDelta kGetVSyncParametersMinPeriod =
+#if defined(OS_LINUX)
+    // See crbug.com/373489
+    // On Linux, querying the vsync parameters might burn CPU for up to an
+    // entire vsync, so we only query periodically to reduce CPU usage.
+    // 5 seconds is chosen somewhat abitrarily as a balance between:
+    //  a) Drift in the phase of our signal.
+    //  b) Potential janks from periodically pegging the CPU.
+    base::TimeDelta::FromSeconds(5);
+#else
+    base::TimeDelta::FromSeconds(0);
+#endif
+
 class OMLSyncControlVSyncProvider
     : public gfx::SyncControlVSyncProvider {
  public:
@@ -129,7 +143,7 @@ class SGIVideoSyncProviderThreadShim {
   explicit SGIVideoSyncProviderThreadShim(XID window)
       : window_(window),
         context_(NULL),
-        message_loop_(base::MessageLoopProxy::current()),
+        task_runner_(base::ThreadTaskRunnerHandle::Get()),
         cancel_vsync_flag_(),
         vsync_lock_() {
     // This ensures that creation of |window_| has occured when this shim
@@ -205,7 +219,7 @@ class SGIVideoSyncProviderThreadShim {
     const base::TimeDelta kDefaultInterval =
         base::TimeDelta::FromSeconds(1) / 60;
 
-    message_loop_->PostTask(
+    task_runner_->PostTask(
         FROM_HERE, base::Bind(callback, now, kDefaultInterval));
   }
 
@@ -219,7 +233,7 @@ class SGIVideoSyncProviderThreadShim {
   XID window_;
   GLXContext context_;
 
-  scoped_refptr<base::MessageLoopProxy> message_loop_;
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
 
   base::CancellationFlag cancel_vsync_flag_;
   base::Lock vsync_lock_;
@@ -256,6 +270,14 @@ class SGIVideoSyncVSyncProvider
 
   virtual void GetVSyncParameters(
       const VSyncProvider::UpdateVSyncCallback& callback) OVERRIDE {
+    if (kGetVSyncParametersMinPeriod > base::TimeDelta()) {
+      base::TimeTicks now = base::TimeTicks::Now();
+      base::TimeDelta delta = now - last_get_vsync_parameters_time_;
+      if (delta < kGetVSyncParametersMinPeriod)
+        return;
+      last_get_vsync_parameters_time_ = now;
+    }
+
     // Only one outstanding request per surface.
     if (!pending_callback_) {
       pending_callback_.reset(
@@ -291,6 +313,8 @@ class SGIVideoSyncVSyncProvider
   base::CancellationFlag* cancel_vsync_flag_;
   base::Lock* vsync_lock_;
 
+  base::TimeTicks last_get_vsync_parameters_time_;
+
   DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncVSyncProvider);
 };