blob: 9c48b498128d84e0df67342a5e978a007940cb9d [file] [log] [blame]
Carlos Martinez Romero43d9afc2023-07-07 22:58:51 +00001// Copyright (C) 2023 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//! libbufferstreams: Reactive Streams for Graphics Buffers
16
Carlos Martinez Romero89aab242023-10-03 21:03:47 +000017pub mod buffers;
James Shargo77e3c542023-09-12 11:58:07 -040018pub mod publishers;
James Shargo83baba52023-09-11 20:17:40 -040019mod stream_config;
James Shargo77e3c542023-09-12 11:58:07 -040020pub mod subscribers;
21pub mod subscriptions;
James Shargo83baba52023-09-11 20:17:40 -040022
Carlos Martinez Romero89aab242023-10-03 21:03:47 +000023use buffers::Buffer;
James Shargo83baba52023-09-11 20:17:40 -040024pub use stream_config::*;
25
Carlos Martinez Romero43d9afc2023-07-07 22:58:51 +000026/// This function will print Hello World.
27#[no_mangle]
28pub extern "C" fn hello() -> bool {
29 println!("Hello world.");
30 true
31}
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000032
33/// BufferPublishers provide buffers to BufferSusbscribers. Depending on the
34/// particular object in question, these could be allocated locally or provided
35/// over IPC.
36///
37/// BufferPublishers are required to adhere to the following, based on the
38/// reactive streams specification:
James Shargo77e3c542023-09-12 11:58:07 -040039/// * The total number of on_next´s signalled by a Publisher to a Subscriber
Chris Wailes098f1ac2024-08-19 17:09:04 -070040/// MUST be less than or equal to the total number of elements requested by that
41/// Subscriber´s Subscription at all times.
James Shargo77e3c542023-09-12 11:58:07 -040042/// * A Publisher MAY signal fewer on_next than requested and terminate the
Chris Wailes098f1ac2024-08-19 17:09:04 -070043/// Subscription by calling on_complete or on_error.
James Shargo77e3c542023-09-12 11:58:07 -040044/// * on_subscribe, on_next, on_error and on_complete signaled to a Subscriber
Chris Wailes098f1ac2024-08-19 17:09:04 -070045/// MUST be signaled serially.
James Shargo77e3c542023-09-12 11:58:07 -040046/// * If a Publisher fails it MUST signal an on_error.
47/// * If a Publisher terminates successfully (finite stream) it MUST signal an
Chris Wailes098f1ac2024-08-19 17:09:04 -070048/// on_complete.
James Shargo77e3c542023-09-12 11:58:07 -040049/// * If a Publisher signals either on_error or on_complete on a Subscriber,
Chris Wailes098f1ac2024-08-19 17:09:04 -070050/// that Subscriber’s Subscription MUST be considered cancelled.
James Shargo77e3c542023-09-12 11:58:07 -040051/// * Once a terminal state has been signaled (on_error, on_complete) it is
Chris Wailes098f1ac2024-08-19 17:09:04 -070052/// REQUIRED that no further signals occur.
James Shargo77e3c542023-09-12 11:58:07 -040053/// * If a Subscription is cancelled its Subscriber MUST eventually stop being
Chris Wailes098f1ac2024-08-19 17:09:04 -070054/// signaled.
James Shargo77e3c542023-09-12 11:58:07 -040055/// * A Publisher MAY support multiple Subscribers and decides whether each
Chris Wailes098f1ac2024-08-19 17:09:04 -070056/// Subscription is unicast or multicast.
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000057pub trait BufferPublisher {
James Shargo83baba52023-09-11 20:17:40 -040058 /// Returns the StreamConfig of buffers that publisher creates.
59 fn get_publisher_stream_config(&self) -> StreamConfig;
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000060 /// This function will create the subscription between the publisher and
61 /// the subscriber.
James Shargo77e3c542023-09-12 11:58:07 -040062 fn subscribe(&mut self, subscriber: impl BufferSubscriber + 'static);
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000063}
64
65/// BufferSubscribers can subscribe to BufferPublishers. They can request Frames
66/// via the BufferSubscription they get from the publisher, then receive Frames
67/// via on_next.
68///
69/// BufferSubcribers are required to adhere to the following, based on the
70/// reactive streams specification:
James Shargo77e3c542023-09-12 11:58:07 -040071/// * The total number of on_next´s signalled by a Publisher to a Subscriber
Chris Wailes098f1ac2024-08-19 17:09:04 -070072/// MUST be less than or equal to the total number of elements requested by that
73/// Subscriber´s Subscription at all times.
James Shargo77e3c542023-09-12 11:58:07 -040074/// * A Publisher MAY signal fewer on_next than requested and terminate the
Chris Wailes098f1ac2024-08-19 17:09:04 -070075/// Subscription by calling on_complete or on_error.
James Shargo77e3c542023-09-12 11:58:07 -040076/// * on_subscribe, on_next, on_error and on_complete signaled to a Subscriber
Chris Wailes098f1ac2024-08-19 17:09:04 -070077/// MUST be signaled serially.
James Shargo77e3c542023-09-12 11:58:07 -040078/// * If a Publisher fails it MUST signal an on_error.
79/// * If a Publisher terminates successfully (finite stream) it MUST signal an
Chris Wailes098f1ac2024-08-19 17:09:04 -070080/// on_complete.
James Shargo77e3c542023-09-12 11:58:07 -040081/// * If a Publisher signals either on_error or on_complete on a Subscriber,
Chris Wailes098f1ac2024-08-19 17:09:04 -070082/// that Subscriber’s Subscription MUST be considered cancelled.
James Shargo77e3c542023-09-12 11:58:07 -040083/// * Once a terminal state has been signaled (on_error, on_complete) it is
Chris Wailes098f1ac2024-08-19 17:09:04 -070084/// REQUIRED that no further signals occur.
James Shargo77e3c542023-09-12 11:58:07 -040085/// * If a Subscription is cancelled its Subscriber MUST eventually stop being
Chris Wailes098f1ac2024-08-19 17:09:04 -070086/// signaled.
James Shargo77e3c542023-09-12 11:58:07 -040087/// * Publisher.subscribe MAY be called as many times as wanted but MUST be
Chris Wailes098f1ac2024-08-19 17:09:04 -070088/// with a different Subscriber each time.
James Shargo77e3c542023-09-12 11:58:07 -040089/// * A Publisher MAY support multiple Subscribers and decides whether each
Chris Wailes098f1ac2024-08-19 17:09:04 -070090/// Subscription is unicast or multicast.
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000091pub trait BufferSubscriber {
James Shargo83baba52023-09-11 20:17:40 -040092 /// The StreamConfig of buffers that this subscriber expects.
93 fn get_subscriber_stream_config(&self) -> StreamConfig;
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000094 /// This function will be called at the beginning of the subscription.
James Shargo77e3c542023-09-12 11:58:07 -040095 fn on_subscribe(&mut self, subscription: Box<dyn BufferSubscription>);
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000096 /// This function will be called for buffer that comes in.
James Shargo77e3c542023-09-12 11:58:07 -040097 fn on_next(&mut self, frame: Frame);
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000098 /// This function will be called in case of an error.
James Shargo77e3c542023-09-12 11:58:07 -040099 fn on_error(&mut self, error: BufferError);
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000100 /// This function will be called on finite streams when done.
James Shargo77e3c542023-09-12 11:58:07 -0400101 fn on_complete(&mut self);
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000102}
103
104/// BufferSubscriptions serve as the bridge between BufferPublishers and
105/// BufferSubscribers. BufferSubscribers receive a BufferSubscription when they
106/// subscribe to a BufferPublisher via on_subscribe.
James Shargo0c3ff292024-02-01 14:08:04 -0500107///
108/// This object is used by the BufferSubscriber to cancel its subscription
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000109/// or request more buffers.
110///
111/// BufferSubcriptions are required to adhere to the following, based on the
112/// reactive streams specification:
James Shargo77e3c542023-09-12 11:58:07 -0400113/// * Subscription.request and Subscription.cancel MUST only be called inside
Chris Wailes098f1ac2024-08-19 17:09:04 -0700114/// of its Subscriber context.
James Shargo77e3c542023-09-12 11:58:07 -0400115/// * The Subscription MUST allow the Subscriber to call Subscription.request
Chris Wailes098f1ac2024-08-19 17:09:04 -0700116/// synchronously from within on_next or on_subscribe.
James Shargo77e3c542023-09-12 11:58:07 -0400117/// * Subscription.request MUST place an upper bound on possible synchronous
Chris Wailes098f1ac2024-08-19 17:09:04 -0700118/// recursion between Publisher and Subscriber.
James Shargo77e3c542023-09-12 11:58:07 -0400119/// * Subscription.request SHOULD respect the responsivity of its caller by
Chris Wailes098f1ac2024-08-19 17:09:04 -0700120/// returning in a timely manner.
James Shargo77e3c542023-09-12 11:58:07 -0400121/// * Subscription.cancel MUST respect the responsivity of its caller by
Chris Wailes098f1ac2024-08-19 17:09:04 -0700122/// returning in a timely manner, MUST be idempotent and MUST be thread-safe.
James Shargo77e3c542023-09-12 11:58:07 -0400123/// * After the Subscription is cancelled, additional
Chris Wailes098f1ac2024-08-19 17:09:04 -0700124/// Subscription.request(n: u64) MUST be NOPs.
James Shargo77e3c542023-09-12 11:58:07 -0400125/// * After the Subscription is cancelled, additional Subscription.cancel()
Chris Wailes098f1ac2024-08-19 17:09:04 -0700126/// MUST be NOPs.
James Shargo77e3c542023-09-12 11:58:07 -0400127/// * While the Subscription is not cancelled, Subscription.request(n: u64)
Chris Wailes098f1ac2024-08-19 17:09:04 -0700128/// MUST register the given number of additional elements to be produced to the
129/// respective subscriber.
James Shargo77e3c542023-09-12 11:58:07 -0400130/// * While the Subscription is not cancelled, Subscription.request(n: u64)
Chris Wailes098f1ac2024-08-19 17:09:04 -0700131/// MUST signal on_error if the argument is <= 0. The cause message SHOULD
132/// explain that non-positive request signals are illegal.
James Shargo77e3c542023-09-12 11:58:07 -0400133/// * While the Subscription is not cancelled, Subscription.request(n: u64)
Chris Wailes098f1ac2024-08-19 17:09:04 -0700134/// MAY synchronously call on_next on this (or other) subscriber(s).
James Shargo77e3c542023-09-12 11:58:07 -0400135/// * While the Subscription is not cancelled, Subscription.request(n: u64)
Chris Wailes098f1ac2024-08-19 17:09:04 -0700136/// MAY synchronously call on_complete or on_error on this (or other)
137/// subscriber(s).
James Shargo77e3c542023-09-12 11:58:07 -0400138/// * While the Subscription is not cancelled, Subscription.cancel() MUST
Chris Wailes098f1ac2024-08-19 17:09:04 -0700139/// request the Publisher to eventually stop signaling its Subscriber. The
140/// operation is NOT REQUIRED to affect the Subscription immediately.
James Shargo77e3c542023-09-12 11:58:07 -0400141/// * While the Subscription is not cancelled, Subscription.cancel() MUST
Chris Wailes098f1ac2024-08-19 17:09:04 -0700142/// request the Publisher to eventually drop any references to the corresponding
143/// subscriber.
James Shargo77e3c542023-09-12 11:58:07 -0400144/// * While the Subscription is not cancelled, calling Subscription.cancel MAY
Chris Wailes098f1ac2024-08-19 17:09:04 -0700145/// cause the Publisher, if stateful, to transition into the shut-down state if
146/// no other Subscription exists at this point.
James Shargo77e3c542023-09-12 11:58:07 -0400147/// * Calling Subscription.cancel MUST return normally.
148/// * Calling Subscription.request MUST return normally.
James Shargo0c3ff292024-02-01 14:08:04 -0500149pub trait BufferSubscription: Send + Sync + 'static {
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000150 /// request
151 fn request(&self, n: u64);
152 /// cancel
153 fn cancel(&self);
154}
James Shargo77e3c542023-09-12 11:58:07 -0400155
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000156/// Type used to describe errors produced by subscriptions.
James Shargo77e3c542023-09-12 11:58:07 -0400157pub type BufferError = anyhow::Error;
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000158
159/// Struct used to contain the buffer.
160pub struct Frame {
Carlos Martinez Romero89aab242023-10-03 21:03:47 +0000161 /// A buffer to be used this frame.
162 pub buffer: Buffer,
James Shargo0c3ff292024-02-01 14:08:04 -0500163 /// The time at which this buffer is expected to be displayed.
164 pub present_time: i64,
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +0000165 /// A fence used for reading/writing safely.
166 pub fence: i32,
167}
James Shargo77e3c542023-09-12 11:58:07 -0400168
169#[cfg(test)]
170mod test {
171 #![allow(warnings, unused)]
172 use super::*;
173
174 use anyhow::anyhow;
Carlos Martinez Romero89aab242023-10-03 21:03:47 +0000175 use buffers::Buffer;
176 use nativewindow::{AHardwareBuffer_Format, AHardwareBuffer_UsageFlags};
James Shargo0c3ff292024-02-01 14:08:04 -0500177 use std::{borrow::BorrowMut, error::Error, ops::Add, sync::Arc};
James Shargo77e3c542023-09-12 11:58:07 -0400178
James Shargo0c3ff292024-02-01 14:08:04 -0500179 use crate::{
180 publishers::testing::*,
181 subscribers::{testing::*, SharedSubscriber},
182 };
James Shargo77e3c542023-09-12 11:58:07 -0400183
184 const STREAM_CONFIG: StreamConfig = StreamConfig {
185 width: 1,
186 height: 1,
187 layers: 1,
188 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
189 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
190 stride: 0,
191 };
192
193 fn make_frame() -> Frame {
194 Frame {
Carlos Martinez Romero89aab242023-10-03 21:03:47 +0000195 buffer: Buffer::new_unowned(
196 STREAM_CONFIG
197 .create_hardware_buffer()
198 .expect("Unable to create hardware buffer for test"),
199 ),
James Shargo0c3ff292024-02-01 14:08:04 -0500200 present_time: 1,
James Shargo77e3c542023-09-12 11:58:07 -0400201 fence: 0,
202 }
203 }
204
205 #[test]
206 fn test_test_implementations_next() {
207 let subscriber = SharedSubscriber::new(TestSubscriber::new(STREAM_CONFIG));
208 let mut publisher = TestPublisher::new(STREAM_CONFIG);
209
210 publisher.subscribe(subscriber.clone());
211 assert!(subscriber.map_inner(|s| s.has_subscription()));
212 assert!(publisher.has_subscriber());
213
214 publisher.send_frame(make_frame());
215 let events = subscriber.map_inner_mut(|s| s.take_events());
216 assert!(!matches!(events.last().unwrap(), TestingSubscriberEvent::Next(_)));
217
218 subscriber.map_inner(|s| s.request(1));
219 assert_eq!(publisher.pending_requests(), 1);
220
221 publisher.send_frame(make_frame());
222 let events = subscriber.map_inner_mut(|s| s.take_events());
223 assert!(matches!(events.last().unwrap(), TestingSubscriberEvent::Next(_)));
224 assert_eq!(publisher.pending_requests(), 0);
225 }
226
227 #[test]
228 fn test_test_implementations_complete() {
229 let subscriber = SharedSubscriber::new(TestSubscriber::new(STREAM_CONFIG));
230 let mut publisher = TestPublisher::new(STREAM_CONFIG);
231
232 publisher.subscribe(subscriber.clone());
233 assert!(subscriber.map_inner(|s| s.has_subscription()));
234 assert!(publisher.has_subscriber());
235
236 publisher.send_complete();
237 let events = subscriber.map_inner_mut(|s| s.take_events());
238 assert!(matches!(events.last().unwrap(), TestingSubscriberEvent::Complete));
239 }
240
241 #[test]
242 fn test_test_implementations_error() {
243 let subscriber = SharedSubscriber::new(TestSubscriber::new(STREAM_CONFIG));
244 let mut publisher = TestPublisher::new(STREAM_CONFIG);
245
246 publisher.subscribe(subscriber.clone());
247 assert!(subscriber.map_inner(|s| s.has_subscription()));
248 assert!(publisher.has_subscriber());
249
250 publisher.send_error(anyhow!("error"));
251 let events = subscriber.map_inner_mut(|s| s.take_events());
252 assert!(matches!(events.last().unwrap(), TestingSubscriberEvent::Error(_)));
253 }
254}