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