Bug 1421586 - Limit maximum number of SurfaceTextures in existence. r=snorp

Limit the number of SurfaceTextures to ensure that we do not reach the
process's fd limit. 200 was chosen as the limit because it allows for
more tiles than any reasonable page should require, yet should not
come close to the 1024 fd limit, even with 2 fds (buffer and sync
fence) per SurfaceTexture.

MozReview-Commit-ID: F21H1Rj4FVF

--HG--
extra : rebase_source : 379b460c8067cc3c5aa11a47cc66f865bae1ac64
This commit is contained in:
Jamie Nicol 2017-11-29 16:08:12 +00:00
parent b1ad4c3c99
commit 834586be68
2 changed files with 27 additions and 16 deletions

View File

@ -17,6 +17,7 @@ import org.mozilla.gecko.annotation.WrapForJNI;
public final class GeckoSurfaceTexture extends SurfaceTexture {
private static final String LOGTAG = "GeckoSurfaceTexture";
private static final int MAX_SURFACE_TEXTURES = 200;
private static volatile int sNextHandle = 1;
private static final HashMap<Integer, GeckoSurfaceTexture> sSurfaceTextures = new HashMap<Integer, GeckoSurfaceTexture>();
@ -155,10 +156,6 @@ public final class GeckoSurfaceTexture extends SurfaceTexture {
if (useCount == 0) {
setListener(null);
synchronized (sSurfaceTextures) {
sSurfaceTextures.remove(mHandle);
}
if (mAttachedContext == 0) {
release();
return;
@ -188,6 +185,10 @@ public final class GeckoSurfaceTexture extends SurfaceTexture {
for (GeckoSurfaceTexture tex : list) {
try {
synchronized (sSurfaceTextures) {
sSurfaceTextures.remove(tex.mHandle);
}
if (tex.isSingleBuffer()) {
tex.releaseTexImage();
}
@ -213,26 +214,31 @@ public final class GeckoSurfaceTexture extends SurfaceTexture {
throw new IllegalArgumentException("single buffer mode not supported on API version < 19");
}
int handle = sNextHandle++;
final GeckoSurfaceTexture gst;
if (isSingleBufferSupported()) {
gst = new GeckoSurfaceTexture(handle, singleBufferMode);
} else {
gst = new GeckoSurfaceTexture(handle);
}
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;
}
int handle = sNextHandle++;
final GeckoSurfaceTexture gst;
if (isSingleBufferSupported()) {
gst = new GeckoSurfaceTexture(handle, singleBufferMode);
} else {
gst = new GeckoSurfaceTexture(handle);
}
if (sSurfaceTextures.containsKey(handle)) {
gst.release();
throw new IllegalArgumentException("Already have a GeckoSurfaceTexture with that handle");
}
sSurfaceTextures.put(handle, gst);
return gst;
}
return gst;
}
@WrapForJNI

View File

@ -23,6 +23,11 @@ public class SurfaceAllocatorService extends Service {
private Binder mBinder = new ISurfaceAllocator.Stub() {
public GeckoSurface acquireSurface(int width, int height, boolean singleBufferMode) {
GeckoSurfaceTexture gst = GeckoSurfaceTexture.acquire(singleBufferMode);
if (gst == null) {
return null;
}
if (width > 0 && height > 0) {
gst.setDefaultBufferSize(width, height);
}