xref: /aosp_15_r20/frameworks/wilhelm/src/sync.cpp (revision bebae9c0e76121f8312ccb50385c080b3a0b023c)
1*bebae9c0SAndroid Build Coastguard Worker /*
2*bebae9c0SAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*bebae9c0SAndroid Build Coastguard Worker  *
4*bebae9c0SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*bebae9c0SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*bebae9c0SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*bebae9c0SAndroid Build Coastguard Worker  *
8*bebae9c0SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*bebae9c0SAndroid Build Coastguard Worker  *
10*bebae9c0SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*bebae9c0SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*bebae9c0SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*bebae9c0SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*bebae9c0SAndroid Build Coastguard Worker  * limitations under the License.
15*bebae9c0SAndroid Build Coastguard Worker  */
16*bebae9c0SAndroid Build Coastguard Worker 
17*bebae9c0SAndroid Build Coastguard Worker /* sync */
18*bebae9c0SAndroid Build Coastguard Worker 
19*bebae9c0SAndroid Build Coastguard Worker #include "sles_allinclusive.h"
20*bebae9c0SAndroid Build Coastguard Worker 
21*bebae9c0SAndroid Build Coastguard Worker 
22*bebae9c0SAndroid Build Coastguard Worker /** \brief Sync thread.
23*bebae9c0SAndroid Build Coastguard Worker  *  The sync thread runs periodically to synchronize audio state between
24*bebae9c0SAndroid Build Coastguard Worker  *  the application and platform-specific device driver; for best results
25*bebae9c0SAndroid Build Coastguard Worker  *  it should run about every graphics frame (e.g. 20 Hz to 50 Hz).
26*bebae9c0SAndroid Build Coastguard Worker  */
27*bebae9c0SAndroid Build Coastguard Worker 
sync_start(void * arg)28*bebae9c0SAndroid Build Coastguard Worker void *sync_start(void *arg)
29*bebae9c0SAndroid Build Coastguard Worker {
30*bebae9c0SAndroid Build Coastguard Worker     CEngine *thiz = (CEngine *) arg;
31*bebae9c0SAndroid Build Coastguard Worker     for (;;) {
32*bebae9c0SAndroid Build Coastguard Worker 
33*bebae9c0SAndroid Build Coastguard Worker         // FIXME should be driven by cond_signal rather than polling,
34*bebae9c0SAndroid Build Coastguard Worker         // or at least make the poll interval longer or configurable
35*bebae9c0SAndroid Build Coastguard Worker         usleep(20000*5);
36*bebae9c0SAndroid Build Coastguard Worker 
37*bebae9c0SAndroid Build Coastguard Worker         object_lock_exclusive(&thiz->mObject);
38*bebae9c0SAndroid Build Coastguard Worker         if (thiz->mEngine.mShutdown) {
39*bebae9c0SAndroid Build Coastguard Worker             thiz->mEngine.mShutdownAck = SL_BOOLEAN_TRUE;
40*bebae9c0SAndroid Build Coastguard Worker             // broadcast not signal, because this condition is also used for other purposes
41*bebae9c0SAndroid Build Coastguard Worker             object_cond_broadcast(&thiz->mObject);
42*bebae9c0SAndroid Build Coastguard Worker             object_unlock_exclusive(&thiz->mObject);
43*bebae9c0SAndroid Build Coastguard Worker             break;
44*bebae9c0SAndroid Build Coastguard Worker         }
45*bebae9c0SAndroid Build Coastguard Worker         if (thiz->m3DCommit.mWaiting) {
46*bebae9c0SAndroid Build Coastguard Worker             thiz->m3DCommit.mWaiting = 0;
47*bebae9c0SAndroid Build Coastguard Worker             ++thiz->m3DCommit.mGeneration;
48*bebae9c0SAndroid Build Coastguard Worker             // There might be more than one thread blocked in Commit, so wake them all
49*bebae9c0SAndroid Build Coastguard Worker             object_cond_broadcast(&thiz->mObject);
50*bebae9c0SAndroid Build Coastguard Worker             // here is where we would process the enqueued 3D commands
51*bebae9c0SAndroid Build Coastguard Worker         }
52*bebae9c0SAndroid Build Coastguard Worker         // unsigned instanceMask = thiz->mEngine.mInstanceMask; // for debugger
53*bebae9c0SAndroid Build Coastguard Worker         unsigned changedMask = thiz->mEngine.mChangedMask;
54*bebae9c0SAndroid Build Coastguard Worker         thiz->mEngine.mChangedMask = 0;
55*bebae9c0SAndroid Build Coastguard Worker         object_unlock_exclusive(&thiz->mObject);
56*bebae9c0SAndroid Build Coastguard Worker 
57*bebae9c0SAndroid Build Coastguard Worker         // now we know which objects exist, and which of those have changes
58*bebae9c0SAndroid Build Coastguard Worker 
59*bebae9c0SAndroid Build Coastguard Worker         unsigned combinedMask = changedMask /* | instanceMask for debugger */;
60*bebae9c0SAndroid Build Coastguard Worker         while (combinedMask) {
61*bebae9c0SAndroid Build Coastguard Worker             unsigned i = ctz(combinedMask);
62*bebae9c0SAndroid Build Coastguard Worker             assert(MAX_INSTANCE > i);
63*bebae9c0SAndroid Build Coastguard Worker             combinedMask &= ~(1 << i);
64*bebae9c0SAndroid Build Coastguard Worker             IObject *instance = (IObject *) thiz->mEngine.mInstances[i];
65*bebae9c0SAndroid Build Coastguard Worker             // Could be NULL during construct or destroy
66*bebae9c0SAndroid Build Coastguard Worker             if (NULL == instance) {
67*bebae9c0SAndroid Build Coastguard Worker                 continue;
68*bebae9c0SAndroid Build Coastguard Worker             }
69*bebae9c0SAndroid Build Coastguard Worker 
70*bebae9c0SAndroid Build Coastguard Worker             object_lock_exclusive(instance);
71*bebae9c0SAndroid Build Coastguard Worker             unsigned attributesMask = instance->mAttributesMask;
72*bebae9c0SAndroid Build Coastguard Worker             instance->mAttributesMask = 0;
73*bebae9c0SAndroid Build Coastguard Worker 
74*bebae9c0SAndroid Build Coastguard Worker             switch (IObjectToObjectID(instance)) {
75*bebae9c0SAndroid Build Coastguard Worker             case SL_OBJECTID_AUDIOPLAYER:
76*bebae9c0SAndroid Build Coastguard Worker                 // do something here
77*bebae9c0SAndroid Build Coastguard Worker                 object_unlock_exclusive(instance);
78*bebae9c0SAndroid Build Coastguard Worker #ifdef USE_SNDFILE
79*bebae9c0SAndroid Build Coastguard Worker                 if (attributesMask & (ATTR_POSITION | ATTR_TRANSPORT)) {
80*bebae9c0SAndroid Build Coastguard Worker                     CAudioPlayer *audioPlayer = (CAudioPlayer *) instance;
81*bebae9c0SAndroid Build Coastguard Worker                     audioPlayerTransportUpdate(audioPlayer);
82*bebae9c0SAndroid Build Coastguard Worker                 }
83*bebae9c0SAndroid Build Coastguard Worker #endif
84*bebae9c0SAndroid Build Coastguard Worker                 break;
85*bebae9c0SAndroid Build Coastguard Worker 
86*bebae9c0SAndroid Build Coastguard Worker             default:
87*bebae9c0SAndroid Build Coastguard Worker                 object_unlock_exclusive(instance);
88*bebae9c0SAndroid Build Coastguard Worker                 break;
89*bebae9c0SAndroid Build Coastguard Worker             }
90*bebae9c0SAndroid Build Coastguard Worker         }
91*bebae9c0SAndroid Build Coastguard Worker     }
92*bebae9c0SAndroid Build Coastguard Worker     return NULL;
93*bebae9c0SAndroid Build Coastguard Worker }
94