1 /*
2  * Copyright 2019 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 androidx.camera.core.impl.utils.executor;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 
22 import org.jspecify.annotations.NonNull;
23 
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.ScheduledExecutorService;
26 
27 /**
28  * Utility class for generating specific implementations of {@link Executor}.
29  */
30 public final class CameraXExecutors {
31 
32     // Should not be instantiated
CameraXExecutors()33     private CameraXExecutors() {
34     }
35 
36     /** Returns a cached {@link ScheduledExecutorService} which posts to the main thread. */
mainThreadExecutor()37     public static @NonNull ScheduledExecutorService mainThreadExecutor() {
38         return MainThreadExecutor.getInstance();
39     }
40 
41     /** Returns a cached {@link Executor} suitable for disk I/O. */
ioExecutor()42     public static @NonNull Executor ioExecutor() {
43         return IoExecutor.getInstance();
44     }
45 
46     /** Returns a cached {@link Executor} suitable for audio I/O. */
audioExecutor()47     public static @NonNull Executor audioExecutor() {
48         return AudioExecutor.getInstance();
49     }
50 
51     /** Returns a cached executor that runs tasks directly from the calling thread. */
directExecutor()52     public static @NonNull Executor directExecutor() {
53         return DirectExecutor.getInstance();
54     }
55 
56     /**
57      * Returns a new executor which will perform all tasks sequentially.
58      *
59      * <p>The returned executor delegates all tasks to the provided delegate Executor, but will
60      * ensure all tasks are run in order and without overlapping. Note this can only be
61      * guaranteed for tasks that are submitted via the same sequential executor. Tasks submitted
62      * directly to the delegate or to different instances of the sequential executor do not have
63      * any ordering guarantees.
64      */
newSequentialExecutor(@onNull Executor delegate)65     public static @NonNull Executor newSequentialExecutor(@NonNull Executor delegate) {
66         return new SequentialExecutor(delegate);
67     }
68 
69     /**
70      * Returns whether the executor is a sequential executor as returned by
71      * {@link #newSequentialExecutor(Executor)}.
72      */
isSequentialExecutor(@onNull Executor executor)73     public static boolean isSequentialExecutor(@NonNull Executor executor) {
74         return executor instanceof SequentialExecutor;
75     }
76 
77     /**
78      * Returns an executor which posts to the thread's current {@link Looper}.
79      *
80      * @return An executor which posts to the thread's current looper.
81      * @throws IllegalStateException if the current thread does not have a looper.
82      */
myLooperExecutor()83     public static @NonNull ScheduledExecutorService myLooperExecutor() {
84         return HandlerScheduledExecutorService.currentThreadExecutor();
85     }
86 
87     /**
88      * Returns an executor which posts to the given {@link Handler}.
89      *
90      * @return An executor which posts to the given handler.
91      */
newHandlerExecutor(@onNull Handler handler)92     public static @NonNull ScheduledExecutorService newHandlerExecutor(@NonNull Handler handler) {
93         return new HandlerScheduledExecutorService(handler);
94     }
95 
96     /**
97      * @return a cached high priority {@link Executor} suitable for lightweight tasks.
98      */
highPriorityExecutor()99     public static @NonNull Executor highPriorityExecutor() {
100         return HighPriorityExecutor.getInstance();
101     }
102 }
103