blob: 1d321c833d352231805aea86f5897cb1c5a9984b [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 Romeroa4004692023-07-20 21:34:15 +000017use nativewindow::*;
18use std::sync::{Arc, Weak};
19use std::time::Instant;
20
Carlos Martinez Romero43d9afc2023-07-07 22:58:51 +000021/// This function will print Hello World.
22#[no_mangle]
23pub extern "C" fn hello() -> bool {
24 println!("Hello world.");
25 true
26}
Carlos Martinez Romeroa4004692023-07-20 21:34:15 +000027
28/// BufferPublishers provide buffers to BufferSusbscribers. Depending on the
29/// particular object in question, these could be allocated locally or provided
30/// over IPC.
31///
32/// BufferPublishers are required to adhere to the following, based on the
33/// reactive streams specification:
34/// * The total number of on_next´s signalled by a Publisher to a Subscriber
35/// MUST be less than or equal to the total number of elements requested by that
36/// Subscriber´s Subscription at all times.
37/// * A Publisher MAY signal fewer on_next than requested and terminate the
38/// Subscription by calling on_complete or on_error.
39/// * on_subscribe, on_next, on_error and on_complete signaled to a Subscriber
40/// MUST be signaled serially.
41/// * If a Publisher fails it MUST signal an on_error.
42/// * If a Publisher terminates successfully (finite stream) it MUST signal an
43/// on_complete.
44/// * If a Publisher signals either on_error or on_complete on a Subscriber,
45/// that Subscriber’s Subscription MUST be considered cancelled.
46/// * Once a terminal state has been signaled (on_error, on_complete) it is
47/// REQUIRED that no further signals occur.
48/// * If a Subscription is cancelled its Subscriber MUST eventually stop being
49/// signaled.
50/// * A Publisher MAY support multiple Subscribers and decides whether each
51/// Subscription is unicast or multicast.
52pub trait BufferPublisher {
53 /// This function will create the subscription between the publisher and
54 /// the subscriber.
55 fn subscribe(&self, subscriber: Weak<dyn BufferSubscriber>);
56}
57
58/// BufferSubscribers can subscribe to BufferPublishers. They can request Frames
59/// via the BufferSubscription they get from the publisher, then receive Frames
60/// via on_next.
61///
62/// BufferSubcribers are required to adhere to the following, based on the
63/// reactive streams specification:
64/// * The total number of on_next´s signalled by a Publisher to a Subscriber
65/// MUST be less than or equal to the total number of elements requested by that
66/// Subscriber´s Subscription at all times.
67/// * A Publisher MAY signal fewer on_next than requested and terminate the
68/// Subscription by calling on_complete or on_error.
69/// * on_subscribe, on_next, on_error and on_complete signaled to a Subscriber
70/// MUST be signaled serially.
71/// * If a Publisher fails it MUST signal an on_error.
72/// * If a Publisher terminates successfully (finite stream) it MUST signal an
73/// on_complete.
74/// * If a Publisher signals either on_error or on_complete on a Subscriber,
75/// that Subscriber’s Subscription MUST be considered cancelled.
76/// * Once a terminal state has been signaled (on_error, on_complete) it is
77/// REQUIRED that no further signals occur.
78/// * If a Subscription is cancelled its Subscriber MUST eventually stop being
79/// signaled.
80/// * Publisher.subscribe MAY be called as many times as wanted but MUST be
81/// with a different Subscriber each time.
82/// * A Publisher MAY support multiple Subscribers and decides whether each
83/// Subscription is unicast or multicast.
84pub trait BufferSubscriber {
85 /// This function will be called at the beginning of the subscription.
86 fn on_subscribe(&self, subscription: Arc<dyn BufferSubscription>);
87 /// This function will be called for buffer that comes in.
88 fn on_next(&self, frame: Frame);
89 /// This function will be called in case of an error.
90 fn on_error(&self, error: BufferError);
91 /// This function will be called on finite streams when done.
92 fn on_complete(&self);
93}
94
95/// BufferSubscriptions serve as the bridge between BufferPublishers and
96/// BufferSubscribers. BufferSubscribers receive a BufferSubscription when they
97/// subscribe to a BufferPublisher via on_subscribe.
98/// This object is to be used by the BufferSubscriber to cancel its subscription
99/// or request more buffers.
100///
101/// BufferSubcriptions are required to adhere to the following, based on the
102/// reactive streams specification:
103/// * Subscription.request and Subscription.cancel MUST only be called inside
104/// of its Subscriber context.
105/// * The Subscription MUST allow the Subscriber to call Subscription.request
106/// synchronously from within on_next or on_subscribe.
107/// * Subscription.request MUST place an upper bound on possible synchronous
108/// recursion between Publisher and Subscriber.
109/// * Subscription.request SHOULD respect the responsivity of its caller by
110/// returning in a timely manner.
111/// * Subscription.cancel MUST respect the responsivity of its caller by
112/// returning in a timely manner, MUST be idempotent and MUST be thread-safe.
113/// * After the Subscription is cancelled, additional
114/// Subscription.request(n: u64) MUST be NOPs.
115/// * After the Subscription is cancelled, additional Subscription.cancel()
116/// MUST be NOPs.
117/// * While the Subscription is not cancelled, Subscription.request(n: u64)
118/// MUST register the given number of additional elements to be produced to the
119/// respective subscriber.
120/// * While the Subscription is not cancelled, Subscription.request(n: u64)
121/// MUST signal on_error if the argument is <= 0. The cause message SHOULD
122/// explain that non-positive request signals are illegal.
123/// * While the Subscription is not cancelled, Subscription.request(n: u64)
124/// MAY synchronously call on_next on this (or other) subscriber(s).
125/// * While the Subscription is not cancelled, Subscription.request(n: u64)
126/// MAY synchronously call on_complete or on_error on this (or other)
127/// subscriber(s).
128/// * While the Subscription is not cancelled, Subscription.cancel() MUST
129/// request the Publisher to eventually stop signaling its Subscriber. The
130/// operation is NOT REQUIRED to affect the Subscription immediately.
131/// * While the Subscription is not cancelled, Subscription.cancel() MUST
132/// request the Publisher to eventually drop any references to the corresponding
133/// subscriber.
134/// * While the Subscription is not cancelled, calling Subscription.cancel MAY
135/// cause the Publisher, if stateful, to transition into the shut-down state if
136/// no other Subscription exists at this point.
137/// * Calling Subscription.cancel MUST return normally.
138/// * Calling Subscription.request MUST return normally.
139pub trait BufferSubscription {
140 /// request
141 fn request(&self, n: u64);
142 /// cancel
143 fn cancel(&self);
144}
145/// Type used to describe errors produced by subscriptions.
146type BufferError = Box<dyn std::error::Error + Send + Sync + 'static>;
147
148/// Struct used to contain the buffer.
149pub struct Frame {
150 /// A handle to the C buffer interface.
151 pub buffer: AHardwareBuffer,
152 /// The time at which the buffer was dispatched.
153 pub present_time: Instant,
154 /// A fence used for reading/writing safely.
155 pub fence: i32,
156}