Avoid unnecessary texture bind

In SurfaceFlingerConsumer, check to see if native fence sync is
enabled.  If so, defer the texture binding step to Layer::onDraw.

Change-Id: I7d4034a31c0143207eea2509dfa13ef3820f9b8c
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index 1c5403f..a1d46d9 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -327,17 +327,11 @@
         return;
     }
 
-    // Bind the current buffer to the GL texture.
+    // Bind the current buffer to the GL texture, and wait for it to be
+    // ready for us to draw into.
     status_t err = mSurfaceFlingerConsumer->bindTextureImage();
     if (err != NO_ERROR) {
-        ALOGW("Layer::onDraw: bindTextureImage failed");
-        // keep going
-    }
-
-    // Wait for the buffer to be ready for us to draw into.
-    err = mSurfaceFlingerConsumer->doGLFenceWait();
-    if (err != OK) {
-        ALOGE("onDraw: failed waiting for fence: %d", err);
+        ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
         // Go ahead and draw the buffer anyway; no matter what we do the screen
         // is probably going to have something visibly wrong.
     }
diff --git a/services/surfaceflinger/SurfaceFlingerConsumer.cpp b/services/surfaceflinger/SurfaceFlingerConsumer.cpp
index dbe187b..a316896 100644
--- a/services/surfaceflinger/SurfaceFlingerConsumer.cpp
+++ b/services/surfaceflinger/SurfaceFlingerConsumer.cpp
@@ -77,12 +77,24 @@
         return err;
     }
 
-    // Bind the new buffer to the GL texture.
-    // TODO: skip this on devices that support explicit sync
-    // (glEGLImageTargetTexture2DOES provides required implicit sync;
-    // without this we get wedged on older devices, but newer devices
-    // don't need it.)
-    return bindTextureImage();
+    if (!sUseNativeFenceSync) {
+        // Bind the new buffer to the GL texture.
+        //
+        // Older devices require the "implicit" synchronization provided
+        // by glEGLImageTargetTexture2DOES, which this method calls.  Newer
+        // devices will either call this in Layer::onDraw, or (if it's not
+        // a GL-composited layer) not at all.
+        err = bindTextureImageLocked();
+    }
+
+    return err;
+}
+
+status_t SurfaceFlingerConsumer::bindTextureImage()
+{
+    Mutex::Autolock lock(mMutex);
+
+    return bindTextureImageLocked();
 }
 
 // ---------------------------------------------------------------------------
diff --git a/services/surfaceflinger/SurfaceFlingerConsumer.h b/services/surfaceflinger/SurfaceFlingerConsumer.h
index d91f27e..308a288 100644
--- a/services/surfaceflinger/SurfaceFlingerConsumer.h
+++ b/services/surfaceflinger/SurfaceFlingerConsumer.h
@@ -49,8 +49,8 @@
     // texture.
     status_t updateTexImage(BufferRejecter* rejecter);
 
-    // Pass-through to SurfaceTexture implementation.
-    status_t bindTextureImage() { return SurfaceTexture::bindTextureImage(); }
+    // See SurfaceTexture::bindTextureImageLocked().
+    status_t bindTextureImage();
 };
 
 // ----------------------------------------------------------------------------