Android EglBase: Add support for creating EGLSurface from Surface, not SurfaceHolder

Review URL: https://codereview.webrtc.org/1438223003

Cr-Commit-Position: refs/heads/master@{#10646}
diff --git a/talk/app/webrtc/java/android/org/webrtc/EglBase.java b/talk/app/webrtc/java/android/org/webrtc/EglBase.java
index 2ee3688..2f8e4aa 100644
--- a/talk/app/webrtc/java/android/org/webrtc/EglBase.java
+++ b/talk/app/webrtc/java/android/org/webrtc/EglBase.java
@@ -27,7 +27,10 @@
 
 package org.webrtc;
 
+import android.graphics.Canvas;
 import android.graphics.SurfaceTexture;
+import android.graphics.Rect;
+import android.view.Surface;
 import android.view.SurfaceHolder;
 
 import org.webrtc.Logging;
@@ -86,9 +89,71 @@
     eglContext = createEglContext(sharedContext, eglDisplay, eglConfig);
   }
 
-  // Create EGLSurface from the Android SurfaceHolder.
-  public void createSurface(SurfaceHolder surfaceHolder) {
-    createSurfaceInternal(surfaceHolder);
+  // Create EGLSurface from the Android Surface.
+  public void createSurface(Surface surface) {
+    /**
+     * We have to wrap Surface in a SurfaceHolder because for some reason eglCreateWindowSurface
+     * couldn't actually take a Surface object until API 17. Older versions fortunately just call
+     * SurfaceHolder.getSurface(), so we'll do that. No other methods are relevant.
+     */
+    class FakeSurfaceHolder implements SurfaceHolder {
+      private final Surface surface;
+
+      FakeSurfaceHolder(Surface surface) {
+        this.surface = surface;
+      }
+
+      @Override
+      public void addCallback(Callback callback) {}
+
+      @Override
+      public void removeCallback(Callback callback) {}
+
+      @Override
+      public boolean isCreating() {
+        return false;
+      }
+
+      @Override
+      public void setType(int i) {}
+
+      @Override
+      public void setFixedSize(int i, int i2) {}
+
+      @Override
+      public void setSizeFromLayout() {}
+
+      @Override
+      public void setFormat(int i) {}
+
+      @Override
+      public void setKeepScreenOn(boolean b) {}
+
+      @Override
+      public Canvas lockCanvas() {
+        return null;
+      }
+
+      @Override
+      public Canvas lockCanvas(Rect rect) {
+        return null;
+      }
+
+      @Override
+      public void unlockCanvasAndPost(Canvas canvas) {}
+
+      @Override
+      public Rect getSurfaceFrame() {
+        return null;
+      }
+
+      @Override
+      public Surface getSurface() {
+        return surface;
+      }
+    }
+
+    createSurfaceInternal(new FakeSurfaceHolder(surface));
   }
 
   // Create EGLSurface from the Android SurfaceTexture.
diff --git a/talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java b/talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java
index 07e835f..59ec415 100644
--- a/talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java
+++ b/talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java
@@ -176,7 +176,7 @@
       @Override public void run() {
         synchronized (layoutLock) {
           if (isSurfaceCreated) {
-            eglBase.createSurface(getHolder());
+            eglBase.createSurface(getHolder().getSurface());
             eglBase.makeCurrent();
             // Necessary for YUV frames with odd width.
             GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);