Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 34d57c0

Browse files
authored
vp in composeConstraintLayout (#819)
* vp in composeConstraintLayout * small cleanup
1 parent 20a3dc5 commit 34d57c0

File tree

5 files changed

+1266
-206
lines changed

5 files changed

+1266
-206
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package androidx.constraintlayout.extension.util;
2+
3+
public interface UiDelegate {
4+
public boolean post(Runnable runnable);
5+
6+
public boolean postDelayed(Runnable runnable, long delayMillis);
7+
8+
public void invalidate();
9+
10+
public int getWidth();
11+
12+
public int getHeight();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
package androidx.constraintlayout.extension.util;
2+
3+
import java.util.HashMap;
4+
5+
public class Vp {
6+
static HashMap<String, MSChannel> channels = new HashMap<>();
7+
8+
static class MSChannel {
9+
int mSize = 32000;
10+
String mName;
11+
int mLast = 0;
12+
int mWritten = 0;
13+
long[] mTime = new long[mSize];
14+
15+
private MSChannel() {
16+
17+
}
18+
19+
private MSChannel(int size) {
20+
mSize = size;
21+
mTime = new long[mSize];
22+
}
23+
24+
int getLatest() {
25+
if (mWritten == 0) {
26+
return -1;
27+
}
28+
return (mLast == 0 ? mSize : mLast) - 1;
29+
}
30+
31+
long getPos() {
32+
int written;
33+
int last;
34+
synchronized (this) {
35+
written = mWritten;
36+
last = mLast;
37+
}
38+
return ((long) written) << 32 | last;
39+
}
40+
41+
void increment() {
42+
synchronized (this) {
43+
mLast++;
44+
if (mLast == mSize) {
45+
mLast = 0;
46+
}
47+
mWritten++;
48+
}
49+
}
50+
51+
synchronized int getLast() {
52+
return mLast;
53+
}
54+
}
55+
56+
// ======================== Support for float ==============================
57+
static class MSChannelFloat extends MSChannel {
58+
float[] mData = new float[mSize];
59+
60+
MSChannelFloat(String name) {
61+
this.mName = name;
62+
}
63+
64+
MSChannelFloat(String name, int size) {
65+
super(size);
66+
this.mName = name;
67+
mData = new float[size];
68+
}
69+
70+
void add(long time, float value) {
71+
int last = getLast();
72+
mTime[last] = time;
73+
mData[last] = value;
74+
increment();
75+
}
76+
77+
78+
public int get(long start, long[] time, float[] value) {
79+
int count = 0;
80+
81+
int written;
82+
int last, first;
83+
long firstTime;
84+
long lastTime;
85+
synchronized (this) {
86+
written = mWritten;
87+
last = mLast;
88+
89+
if (written > mSize) {
90+
firstTime = mTime[(last + 1) % mSize];
91+
} else {
92+
firstTime = mTime[last - written];
93+
}
94+
int lt = last - 1;
95+
lastTime = mTime[lt >= 0 ? lt : lt + mSize];
96+
}
97+
int startIndex = -1;
98+
if (mWritten > mSize && mLast != mSize - 1) { // it has wrapped
99+
if (mTime[mSize - 1] > start) {
100+
startIndex = search(mTime, last, mSize - 1, start);
101+
} else {
102+
startIndex = search(mTime, 0, last, start);
103+
}
104+
} else {
105+
startIndex = search(mTime, 0, last, start);
106+
}
107+
return getIndex(startIndex, time, value);
108+
}
109+
110+
static int search(long arr[], int low, int high, long key) {
111+
int pos = -1;
112+
while (low <= high) {
113+
pos = low + (high - low) / 2;
114+
if (arr[pos] == key) {
115+
return pos;
116+
}
117+
118+
if (arr[pos] < key) {
119+
low = pos + 1;
120+
} else {
121+
high = pos - 1;
122+
}
123+
}
124+
return pos;
125+
}
126+
127+
public void findIndex(long[] time, int start, int end, int key) {
128+
129+
}
130+
131+
132+
public int getIndex(int startIndex, long[] time, float[] value) {
133+
int len = time.length;
134+
if (len + startIndex < mSize) {
135+
System.arraycopy(mTime, startIndex, time, 0, len);
136+
System.arraycopy(mData, startIndex, value, 0, len);
137+
return (len + startIndex > mLast) ? mLast - startIndex : len;
138+
} else {
139+
int block1len = mSize - startIndex;
140+
System.arraycopy(mTime, startIndex, time, 0, block1len);
141+
System.arraycopy(mData, startIndex, value, 0, block1len);
142+
int copyLen = (mLast > len - block1len) ? len - block1len : mLast;
143+
System.arraycopy(mTime, 0, time, block1len, copyLen);
144+
System.arraycopy(mData, 0, value, block1len, copyLen);
145+
return block1len + copyLen;
146+
}
147+
148+
}
149+
150+
int getLatest(long[] time, float[] value) {
151+
int written;
152+
int last;
153+
synchronized (this) {
154+
written = mWritten;
155+
last = mLast;
156+
}
157+
int maxLen = time.length;
158+
if (written == 0) return -1;
159+
if (maxLen > written) {
160+
maxLen = (int) mWritten;
161+
}
162+
if (maxLen > mSize) {
163+
maxLen = mSize;
164+
}
165+
if (last - maxLen < 0) { // copy [0....last, size-(maxLen-last)...size-1]
166+
int copy1src = 0;
167+
int copy1Dest = maxLen - last;
168+
int copy1len = last;
169+
170+
System.arraycopy(mTime, copy1src, time, copy1Dest, copy1len);
171+
System.arraycopy(mData, copy1src, value, copy1Dest, copy1len);
172+
int copy2src = mSize - (maxLen - last);
173+
int copy2Dest = 0;
174+
int copy2len = maxLen - last;
175+
176+
System.arraycopy(mTime, copy2src, time, copy2Dest, copy2len);
177+
System.arraycopy(mData, copy2src, value, copy2Dest, copy2len);
178+
} else {
179+
int copy1src = last - maxLen;
180+
int copy1Dest = 0;
181+
int copy1len = maxLen;
182+
System.arraycopy(mTime, copy1src, time, copy1Dest, copy1len);
183+
System.arraycopy(mData, copy1src, value, copy1Dest, copy1len);
184+
}
185+
186+
return maxLen;
187+
}
188+
}
189+
190+
public static void initChannel(String channel, int size) {
191+
channels.put(channel, new MSChannelFloat(channel, size));
192+
}
193+
194+
195+
public static void send(String channel, float value) {
196+
send(channel, System.nanoTime(), value);
197+
}
198+
199+
public static void send(String channel, long time, float value) {
200+
MSChannelFloat c = (MSChannelFloat) channels.get(channel);
201+
if (c == null) {
202+
c = new MSChannelFloat(channel);
203+
channels.put(channel, c);
204+
}
205+
c.add(time, value);
206+
}
207+
208+
public static int getLatest(String channel, long[] times, float[] values) {
209+
MSChannelFloat c = (MSChannelFloat) channels.get(channel);
210+
if (c == null) {
211+
return -1;
212+
}
213+
return c.getLatest(times, values);
214+
}
215+
216+
public static int totalWritten(String channel) {
217+
MSChannelFloat c = (MSChannelFloat) channels.get(channel);
218+
if (c == null) {
219+
return -1;
220+
}
221+
return c.mWritten;
222+
}
223+
224+
public static int getAfter(String channel, long time, long[] times, float[] values) {
225+
MSChannelFloat c = (MSChannelFloat) channels.get(channel);
226+
if (c == null) {
227+
return -1;
228+
}
229+
return c.get(time, times, values);
230+
}
231+
232+
public static void sinWaveGen(String channel) {
233+
System.out.println("================ sinWaveGen =================");
234+
Thread t = new Thread() {
235+
@Override
236+
public void run() {
237+
238+
long[] time = new long[100];
239+
float[] value = new float[100];
240+
for (int i = 0; i < 100000000; i++) {
241+
long nt = System.nanoTime();
242+
float v = (float) (Math.sin(nt * 1E-9 * Math.PI) * Math.sin(
243+
nt * 1E-9 * Math.PI / 40));
244+
Vp.send(channel, nt, v);
245+
246+
try {
247+
Thread.sleep(10);
248+
} catch (InterruptedException e) {
249+
e.printStackTrace();
250+
}
251+
}
252+
}
253+
};
254+
t.setDaemon(false);
255+
t.start();
256+
}
257+
258+
private static final HashMap<String, long[]> last_fps_time = new HashMap<>();
259+
260+
public static void fps(String channel) {
261+
long[] ans = last_fps_time.get(channel);
262+
if (ans == null) {
263+
ans = new long[1];
264+
last_fps_time.put(channel, ans);
265+
ans[0] = System.nanoTime();
266+
}
267+
long now = System.nanoTime();
268+
float duration = (now - ans[0]) * 1E-9f;
269+
270+
if (duration < 1 / 500f) {
271+
send(channel, now, 0);
272+
} else {
273+
send(channel, now, 1 / duration);
274+
}
275+
ans[0] = now;
276+
277+
}
278+
279+
// ======================= END Support for Floats==================================
280+
281+
public static void main(String[] args) throws InterruptedException {
282+
long last = System.nanoTime();
283+
Thread t = new Thread() {
284+
@Override
285+
public void run() {
286+
287+
long[] time = new long[100];
288+
float[] value = new float[100];
289+
for (int i = 0; i < 100000000; i++) {
290+
int len = getLatest("bob", time, value);
291+
if (len == -1) {
292+
System.out.println("skip");
293+
try {
294+
Thread.sleep(0, 100);
295+
} catch (InterruptedException e) {
296+
e.printStackTrace();
297+
}
298+
continue;
299+
}
300+
long current = System.nanoTime();
301+
System.out.println(" " + len + " "
302+
+ (current - time[len - 1]) * 1E-6f + "ms " + value[len - 1]);
303+
304+
try {
305+
Thread.sleep(1000);
306+
} catch (InterruptedException e) {
307+
e.printStackTrace();
308+
}
309+
}
310+
}
311+
};
312+
t.setDaemon(false);
313+
t.start();
314+
int sample = 10000000;
315+
for (long i = 0; i < 100000000000L; i++) {
316+
send("bob", i);
317+
// Thread.sleep(0,100);
318+
if (i % sample == 0) {
319+
long now = System.nanoTime();
320+
321+
System.out.println(i + " per sec =" + sample / ((now - last) * 1E-9));
322+
last = now;
323+
}
324+
325+
}
326+
327+
}
328+
}

0 commit comments

Comments
 (0)