-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
SurfaceWrapper.java
122 lines (105 loc) · 3.96 KB
/
SurfaceWrapper.java
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content.common;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Surface;
import android.view.SurfaceControl;
import androidx.annotation.RequiresApi;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.build.annotations.MainDex;
/**
* A wrapper for marshalling a Surface without self-destruction.
*/
@JNINamespace("content")
@MainDex
public class SurfaceWrapper implements Parcelable {
private final boolean mWrapsSurface;
private Surface mSurface;
private final boolean mCanBeUsedWithSurfaceControl;
private SurfaceControl mSurfaceControl;
private SurfaceWrapper(Surface surface, boolean canBeUsedWithSurfaceControl) {
mWrapsSurface = true;
mSurface = surface;
mCanBeUsedWithSurfaceControl = canBeUsedWithSurfaceControl;
mSurfaceControl = null;
}
@RequiresApi(Build.VERSION_CODES.Q)
private SurfaceWrapper(SurfaceControl surfaceControl) {
mWrapsSurface = false;
mSurface = null;
mCanBeUsedWithSurfaceControl = true;
mSurfaceControl = surfaceControl;
}
@CalledByNative
private boolean getWrapsSurface() {
return mWrapsSurface;
}
@CalledByNative
private Surface takeSurface() {
assert mWrapsSurface;
Surface surface = mSurface;
mSurface = null;
return surface;
}
@CalledByNative
private boolean canBeUsedWithSurfaceControl() {
return mCanBeUsedWithSurfaceControl;
}
@CalledByNative
private SurfaceControl takeSurfaceControl() {
assert !mWrapsSurface;
SurfaceControl sc = mSurfaceControl;
mSurfaceControl = null;
return sc;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mWrapsSurface ? 1 : 0);
if (mWrapsSurface) {
// Ignore flags so that the Surface won't call release()
mSurface.writeToParcel(out, 0);
out.writeInt(mCanBeUsedWithSurfaceControl ? 1 : 0);
} else {
// Ignore flags so that SurfaceControl won't call release().
mSurfaceControl.writeToParcel(out, 0);
}
}
@CalledByNative
private static SurfaceWrapper create(Surface surface, boolean canBeUsedWithSurfaceControl) {
return new SurfaceWrapper(surface, canBeUsedWithSurfaceControl);
}
@RequiresApi(Build.VERSION_CODES.Q)
@CalledByNative
private static SurfaceWrapper createFromSurfaceControl(SurfaceControl surfaceControl) {
return new SurfaceWrapper(surfaceControl);
}
public static final Parcelable.Creator<SurfaceWrapper> CREATOR =
new Parcelable.Creator<SurfaceWrapper>() {
@Override
public SurfaceWrapper createFromParcel(Parcel in) {
final boolean wrapsSurface = (in.readInt() == 1);
if (wrapsSurface) {
Surface surface = Surface.CREATOR.createFromParcel(in);
boolean canBeUsedWithSurfaceControl = (in.readInt() == 1);
return new SurfaceWrapper(surface, canBeUsedWithSurfaceControl);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
SurfaceControl surfaceControl = SurfaceControl.CREATOR.createFromParcel(in);
return new SurfaceWrapper(surfaceControl);
} else {
throw new RuntimeException("not reached");
}
}
@Override
public SurfaceWrapper[] newArray(int size) {
return new SurfaceWrapper[size];
}
};
}