summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/GeckoSurfaceTexture.java
blob: 2d045edb4423e4105fd9326b0810991d195e6e1c (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * 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/. */

package org.mozilla.gecko.gfx;

import android.graphics.SurfaceTexture;
import android.os.Build;
import android.util.Log;
import android.util.LongSparseArray;
import androidx.annotation.RequiresApi;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.mozglue.JNIObject;

/* package */ final class GeckoSurfaceTexture extends SurfaceTexture {
  private static final String LOGTAG = "GeckoSurfaceTexture";
  private static final int MAX_SURFACE_TEXTURES = 200;
  private static final LongSparseArray<GeckoSurfaceTexture> sSurfaceTextures =
      new LongSparseArray<GeckoSurfaceTexture>();

  private static LongSparseArray<LinkedList<GeckoSurfaceTexture>> sUnusedTextures =
      new LongSparseArray<LinkedList<GeckoSurfaceTexture>>();

  private long mHandle;
  private boolean mIsSingleBuffer;

  private long mAttachedContext;
  private int mTexName;

  private GeckoSurfaceTexture.Callbacks mListener;
  private AtomicInteger mUseCount;
  private boolean mFinalized;

  private long mUpstream;
  private NativeGLBlitHelper mBlitter;

  private GeckoSurfaceTexture(final long handle) {
    super(0);
    init(handle, false);
  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  private GeckoSurfaceTexture(final long handle, final boolean singleBufferMode) {
    super(0, singleBufferMode);
    init(handle, singleBufferMode);
  }

  @Override
  protected void finalize() throws Throwable {
    // We only want finalize() to be called once
    if (mFinalized) {
      return;
    }

    mFinalized = true;
    super.finalize();
  }

  private void init(final long handle, final boolean singleBufferMode) {
    mHandle = handle;
    mIsSingleBuffer = singleBufferMode;
    mUseCount = new AtomicInteger(1);

    // Start off detached
    detachFromGLContext();
  }

  @WrapForJNI
  public long getHandle() {
    return mHandle;
  }

  @WrapForJNI
  public int getTexName() {
    return mTexName;
  }

  @WrapForJNI(exceptionMode = "nsresult")
  public synchronized void attachToGLContext(final long context, final int texName) {
    if (context == mAttachedContext && texName == mTexName) {
      return;
    }

    attachToGLContext(texName);

    mAttachedContext = context;
    mTexName = texName;
  }

  @Override
  @WrapForJNI(exceptionMode = "nsresult")
  public synchronized void detachFromGLContext() {
    super.detachFromGLContext();

    mAttachedContext = mTexName = 0;
  }

  @WrapForJNI
  public synchronized boolean isAttachedToGLContext(final long context) {
    return mAttachedContext == context;
  }

  @WrapForJNI
  public boolean isSingleBuffer() {
    return mIsSingleBuffer;
  }

  @Override
  @WrapForJNI
  public synchronized void updateTexImage() {
    try {
      if (mUpstream != 0) {
        SurfaceAllocator.sync(mUpstream);
      }
      super.updateTexImage();
      if (mListener != null) {
        mListener.onUpdateTexImage();
      }
    } catch (final Exception e) {
      Log.w(LOGTAG, "updateTexImage() failed", e);
    }
  }

  @Override
  public synchronized void release() {
    mUpstream = 0;
    if (mBlitter != null) {
      mBlitter.close();
    }
    try {
      super.release();
      synchronized (sSurfaceTextures) {
        sSurfaceTextures.remove(mHandle);
      }
    } catch (final Exception e) {
      Log.w(LOGTAG, "release() failed", e);
    }
  }

  @Override
  @WrapForJNI
  public synchronized void releaseTexImage() {
    if (!mIsSingleBuffer) {
      return;
    }

    try {
      super.releaseTexImage();
      if (mListener != null) {
        mListener.onReleaseTexImage();
      }
    } catch (final Exception e) {
      Log.w(LOGTAG, "releaseTexImage() failed", e);
    }
  }

  public synchronized void setListener(final GeckoSurfaceTexture.Callbacks listener) {
    mListener = listener;
  }

  @WrapForJNI
  public static boolean isSingleBufferSupported() {
    return Build.VERSION.SDK_INT >= 19;
  }

  @WrapForJNI
  public synchronized void incrementUse() {
    mUseCount.incrementAndGet();
  }

  @WrapForJNI
  public synchronized void decrementUse() {
    final int useCount = mUseCount.decrementAndGet();

    if (useCount == 0) {
      setListener(null);

      if (mAttachedContext == 0) {
        release();
        synchronized (sUnusedTextures) {
          sSurfaceTextures.remove(mHandle);
        }
        return;
      }

      synchronized (sUnusedTextures) {
        LinkedList<GeckoSurfaceTexture> list = sUnusedTextures.get(mAttachedContext);
        if (list == null) {
          list = new LinkedList<GeckoSurfaceTexture>();
          sUnusedTextures.put(mAttachedContext, list);
        }
        list.addFirst(this);
      }
    }
  }

  @WrapForJNI
  public static void destroyUnused(final long context) {
    final LinkedList<GeckoSurfaceTexture> list;
    synchronized (sUnusedTextures) {
      list = sUnusedTextures.get(context);
      sUnusedTextures.delete(context);
    }

    if (list == null) {
      return;
    }

    for (final GeckoSurfaceTexture tex : list) {
      try {
        if (tex.isSingleBuffer()) {
          tex.releaseTexImage();
        }

        tex.detachFromGLContext();
        tex.release();

        // We need to manually call finalize here, otherwise we can run out
        // of file descriptors if the GC doesn't kick in soon enough. Bug 1416015.
        try {
          tex.finalize();
        } catch (final Throwable t) {
          Log.e(LOGTAG, "Failed to finalize SurfaceTexture", t);
        }
      } catch (final Exception e) {
        Log.e(LOGTAG, "Failed to destroy SurfaceTexture", e);
      }
    }
  }

  public static GeckoSurfaceTexture acquire(final boolean singleBufferMode, final long handle) {
    if (singleBufferMode && !isSingleBufferSupported()) {
      throw new IllegalArgumentException("single buffer mode not supported on API version < 19");
    }

    // Attempting to create a SurfaceTexture from an isolated process on Android versions prior to
    // 8.0 results in an indefinite hang. See bug 1706656.
    if (GeckoAppShell.isIsolatedProcess() && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
      return null;
    }

    synchronized (sSurfaceTextures) {
      // We want to limit the maximum number of SurfaceTextures at any one time.
      // This is because they use a large number of fds, and once the process' limit
      // is reached bad things happen. See bug 1421586.
      if (sSurfaceTextures.size() >= MAX_SURFACE_TEXTURES) {
        return null;
      }

      if (sSurfaceTextures.indexOfKey(handle) >= 0) {
        throw new IllegalArgumentException("Already have a GeckoSurfaceTexture with that handle");
      }

      final GeckoSurfaceTexture gst;
      if (isSingleBufferSupported()) {
        gst = new GeckoSurfaceTexture(handle, singleBufferMode);
      } else {
        gst = new GeckoSurfaceTexture(handle);
      }

      sSurfaceTextures.put(handle, gst);

      return gst;
    }
  }

  @WrapForJNI
  public static GeckoSurfaceTexture lookup(final long handle) {
    synchronized (sSurfaceTextures) {
      return sSurfaceTextures.get(handle);
    }
  }

  /* package */ synchronized void track(final long upstream) {
    mUpstream = upstream;
  }

  /* package */ synchronized void configureSnapshot(
      final GeckoSurface target, final int width, final int height) {
    mBlitter = NativeGLBlitHelper.create(mHandle, target, width, height);
  }

  /* package */ synchronized void takeSnapshot() {
    mBlitter.blit();
  }

  public interface Callbacks {
    void onUpdateTexImage();

    void onReleaseTexImage();
  }

  @WrapForJNI
  public static final class NativeGLBlitHelper extends JNIObject {
    public static NativeGLBlitHelper create(
        final long textureHandle,
        final GeckoSurface targetSurface,
        final int width,
        final int height) {
      final NativeGLBlitHelper helper = nativeCreate(textureHandle, targetSurface, width, height);
      helper.mTargetSurface = targetSurface; // Take ownership of surface.
      return helper;
    }

    public static native NativeGLBlitHelper nativeCreate(
        final long textureHandle,
        final GeckoSurface targetSurface,
        final int width,
        final int height);

    public native void blit();

    public void close() {
      disposeNative();
      if (mTargetSurface != null) {
        mTargetSurface.release();
        mTargetSurface = null;
      }
    }

    @Override
    protected native void disposeNative();

    private GeckoSurface mTargetSurface;
  }
}