Skip to content

Commit a559a12

Browse files
Jim ShargoAndroid (Google) Code Review
authored andcommitted
Merge changes Ia63685ac,I9c32ac17,I6ca17dda into main
* changes: bufferstreams: Present times are no longer Instants. bufferstreams: Make BufferOwner Send+Sync bufferstreams: Add AIDL interfaces/parcelables for stream types
2 parents c8693d4 + 0c3ff29 commit a559a12

10 files changed

Lines changed: 255 additions & 19 deletions

File tree

libs/bufferstreams/aidl/Android.bp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2024 The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
aidl_interface {
16+
name: "android.graphics.bufferstreams",
17+
unstable: true,
18+
flags: ["-Werror"],
19+
srcs: ["android/graphics/bufferstreams/*.aidl"],
20+
headers: [
21+
"HardwareBuffer_aidl",
22+
],
23+
imports: [
24+
"android.hardware.common-V2",
25+
],
26+
backend: {
27+
cpp: {
28+
enabled: false,
29+
},
30+
java: {
31+
enabled: false,
32+
},
33+
ndk: {
34+
enabled: false,
35+
},
36+
rust: {
37+
enabled: true,
38+
additional_rustlibs: [
39+
"libnativewindow_rs",
40+
],
41+
},
42+
},
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
import android.graphics.bufferstreams.IBufferOwner;
20+
import android.hardware.HardwareBuffer;
21+
22+
// Single mapping between a buffer reference and heavy-weight data (like the
23+
// buffer itself) and data that is stable between frames.
24+
parcelable BufferAttachment {
25+
// The HardwareBuffer itself.
26+
//
27+
// This field is @nullable for codegen, since HardwareBuffer doesn't implement Default in Rust.
28+
// In practice, it should never be null.
29+
@nullable HardwareBuffer buffer;
30+
// The buffer owner to which this buffer should be returned.
31+
IBufferOwner owner;
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
import android.graphics.bufferstreams.BufferAttachment;
20+
21+
// A event that changes the state downstream buffer caches. Clients are responsible for forwarding
22+
// these messages to their clients.
23+
union BufferCacheUpdate {
24+
// Event requiring downstream caches to add new entries.
25+
CacheBuffers cacheBuffers;
26+
// Event requiring downstream caches to remove entries.
27+
ForgetBuffers forgetBuffers;
28+
29+
parcelable CacheBuffers {
30+
// Attachments to add.
31+
List<BufferAttachment> attachments;
32+
}
33+
34+
parcelable ForgetBuffers {
35+
// References to remove.
36+
long[] bufferIds;
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
import android.os.ParcelFileDescriptor;
20+
21+
// A Frame represents a single buffer passing through the stream.
22+
parcelable Frame {
23+
// The service must have provided an associated BufferAttachment and the client is required to
24+
// maintain a cache between the two.
25+
long bufferId;
26+
// The expected present time of this frame, or -1 if immediate.
27+
long presentTimeNs;
28+
// The acquire fence of the buffer for this frame.
29+
@nullable ParcelFileDescriptor fence;
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
import android.os.ParcelFileDescriptor;
20+
21+
// Interface from a client back to the owner of a buffer.
22+
interface IBufferOwner {
23+
// Called when the buffer is done being processed by the stream to return its owner.
24+
oneway void onBufferReleased(in long bufferId, in @nullable ParcelFileDescriptor releaseFence);
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
import android.graphics.bufferstreams.BufferCacheUpdate;
20+
import android.graphics.bufferstreams.IBufferSubscription;
21+
import android.graphics.bufferstreams.Frame;
22+
23+
// Interface provided by clients to a service, mirroring the non-IPC interface.
24+
//
25+
// Clients are required to maintain a local cache of Buffer IDs to BufferAttachments.
26+
interface IBufferSubscriber {
27+
// Provide a BufferSubscription object which the client can use to request frames.
28+
oneway void onSubscribe(in IBufferSubscription subscription);
29+
30+
// Notifies the client to update its local caches.
31+
oneway void onBufferCacheUpdate(in BufferCacheUpdate update);
32+
33+
// Notifies the client that a requested frame is available.
34+
oneway void onNext(in Frame frame);
35+
36+
// Notifies the client that a fatal error has occurred. No subsequent on_next events will be
37+
// sent by the service.
38+
//
39+
// Clients must empty their caches.
40+
oneway void onError();
41+
42+
// Notifies the client that no further on_next events will be sent by the service in response
43+
// to it cancelling the subscription.
44+
//
45+
// Clients must empty their caches.
46+
oneway void onComplete();
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.graphics.bufferstreams;
18+
19+
// Interface provided to a IBufferSubscriber to request frames or gracefully cancel their
20+
// subscription.
21+
interface IBufferSubscription {
22+
// Request n more frames.
23+
oneway void request(long n);
24+
// Cancel the subscription. Requested frames may continue to arrive.
25+
oneway void cancel();
26+
}

libs/bufferstreams/rust/src/buffers/buffer_owner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::Buffer;
1616

1717
/// Trait that represents an owner of a buffer that might need to handle events such as a buffer
1818
/// being dropped.
19-
pub trait BufferOwner {
19+
pub trait BufferOwner: Send + Sync {
2020
/// Called when a buffer is dropped.
2121
fn on_return(&self, buffer: &Buffer);
2222
}

libs/bufferstreams/rust/src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ pub mod subscriptions;
2323
use buffers::Buffer;
2424
pub use stream_config::*;
2525

26-
use std::time::Instant;
27-
2826
/// This function will print Hello World.
2927
#[no_mangle]
3028
pub extern "C" fn hello() -> bool {
@@ -106,7 +104,8 @@ pub trait BufferSubscriber {
106104
/// BufferSubscriptions serve as the bridge between BufferPublishers and
107105
/// BufferSubscribers. BufferSubscribers receive a BufferSubscription when they
108106
/// subscribe to a BufferPublisher via on_subscribe.
109-
/// This object is to be used by the BufferSubscriber to cancel its subscription
107+
///
108+
/// This object is used by the BufferSubscriber to cancel its subscription
110109
/// or request more buffers.
111110
///
112111
/// BufferSubcriptions are required to adhere to the following, based on the
@@ -147,7 +146,7 @@ pub trait BufferSubscriber {
147146
/// no other Subscription exists at this point.
148147
/// * Calling Subscription.cancel MUST return normally.
149148
/// * Calling Subscription.request MUST return normally.
150-
pub trait BufferSubscription {
149+
pub trait BufferSubscription: Send + Sync + 'static {
151150
/// request
152151
fn request(&self, n: u64);
153152
/// cancel
@@ -161,8 +160,8 @@ pub type BufferError = anyhow::Error;
161160
pub struct Frame {
162161
/// A buffer to be used this frame.
163162
pub buffer: Buffer,
164-
/// The time at which the buffer was dispatched.
165-
pub present_time: Instant,
163+
/// The time at which this buffer is expected to be displayed.
164+
pub present_time: i64,
166165
/// A fence used for reading/writing safely.
167166
pub fence: i32,
168167
}
@@ -175,14 +174,12 @@ mod test {
175174
use anyhow::anyhow;
176175
use buffers::Buffer;
177176
use nativewindow::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
178-
use std::borrow::BorrowMut;
179-
use std::error::Error;
180-
use std::ops::Add;
181-
use std::sync::Arc;
182-
use std::time::Duration;
177+
use std::{borrow::BorrowMut, error::Error, ops::Add, sync::Arc};
183178

184-
use crate::publishers::testing::*;
185-
use crate::subscribers::{testing::*, SharedSubscriber};
179+
use crate::{
180+
publishers::testing::*,
181+
subscribers::{testing::*, SharedSubscriber},
182+
};
186183

187184
const STREAM_CONFIG: StreamConfig = StreamConfig {
188185
width: 1,
@@ -200,7 +197,7 @@ mod test {
200197
.create_hardware_buffer()
201198
.expect("Unable to create hardware buffer for test"),
202199
),
203-
present_time: Instant::now() + Duration::from_secs(1),
200+
present_time: 1,
204201
fence: 0,
205202
}
206203
}

libs/bufferstreams/rust/src/publishers/buffer_pool_publisher.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
//!
1616
17-
use std::time::Instant;
18-
1917
use crate::{
2018
buffers::BufferPool, subscriptions::SharedBufferSubscription, BufferPublisher,
2119
BufferSubscriber, Frame, StreamConfig,
@@ -43,7 +41,7 @@ impl BufferPoolPublisher {
4341

4442
/// If the [SharedBufferSubscription] is ready for a [Frame], a buffer will be requested from
4543
/// [BufferPool] and sent over to the [BufferSubscriber].
46-
pub fn send_next_frame(&mut self, present_time: Instant) -> bool {
44+
pub fn send_next_frame(&mut self, present_time: i64) -> bool {
4745
if let Some(subscriber) = self.subscriber.as_mut() {
4846
if self.subscription.take_request() {
4947
if let Some(buffer) = self.buffer_pool.next_buffer() {
@@ -103,7 +101,7 @@ mod test {
103101

104102
subscriber.map_inner(|s| s.request(1));
105103

106-
assert!(buffer_pool_publisher.send_next_frame(Instant::now()));
104+
assert!(buffer_pool_publisher.send_next_frame(1));
107105

108106
let events = subscriber.map_inner_mut(|s| s.take_events());
109107
assert!(matches!(events.last().unwrap(), TestingSubscriberEvent::Next(_)));

0 commit comments

Comments
 (0)