Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #include "Scheduler.h" |
| 18 | |
| 19 | #include <cinttypes> |
| 20 | #include <cstdint> |
| 21 | #include <memory> |
| 22 | |
| 23 | #include <gui/ISurfaceComposer.h> |
| 24 | |
| 25 | #include "DispSync.h" |
| 26 | #include "DispSyncSource.h" |
| 27 | #include "EventThread.h" |
| 28 | #include "InjectVSyncSource.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | std::atomic<int64_t> Scheduler::sNextId = 0; |
| 33 | |
| 34 | sp<Scheduler::ConnectionHandle> Scheduler::createConnection( |
| 35 | const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
| 36 | impl::EventThread::ResyncWithRateLimitCallback resyncCallback, |
| 37 | impl::EventThread::InterceptVSyncsCallback interceptCallback) { |
| 38 | const int64_t id = sNextId++; |
| 39 | ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id); |
| 40 | |
| 41 | std::unique_ptr<VSyncSource> eventThreadSource = |
| 42 | std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, true, connectionName); |
| 43 | std::unique_ptr<EventThread> eventThread = |
| 44 | std::make_unique<impl::EventThread>(std::move(eventThreadSource), resyncCallback, |
| 45 | interceptCallback, connectionName); |
| 46 | auto connection = std::make_unique<Connection>(new ConnectionHandle(id), |
| 47 | eventThread->createEventConnection(), |
| 48 | std::move(eventThread)); |
| 49 | mConnections.insert(std::make_pair(id, std::move(connection))); |
| 50 | return mConnections[id]->handle; |
| 51 | } |
| 52 | |
| 53 | sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection( |
| 54 | const sp<Scheduler::ConnectionHandle>& handle) { |
| 55 | if (mConnections.count(handle->id) != 0) { |
| 56 | return mConnections[handle->id]->thread->createEventConnection(); |
| 57 | } |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) { |
| 62 | if (mConnections.count(handle->id) != 0) { |
| 63 | return mConnections[handle->id]->thread.get(); |
| 64 | } |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | sp<BnDisplayEventConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) { |
| 69 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 70 | return mConnections[handle->id]->eventConnection; |
| 71 | } |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle, |
| 76 | EventThread::DisplayType displayType, bool connected) { |
| 77 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 78 | mConnections[handle->id]->thread->onHotplugReceived(displayType, connected); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) { |
| 83 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 84 | mConnections[handle->id]->thread->onScreenAcquired(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) { |
| 89 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 90 | mConnections[handle->id]->thread->onScreenReleased(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, String8& result) const { |
| 95 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 96 | mConnections.at(handle->id)->thread->dump(result); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) { |
| 101 | if (mConnections.find(handle->id) != mConnections.end()) { |
| 102 | mConnections[handle->id]->thread->setPhaseOffset(phaseOffset); |
| 103 | } |
| 104 | } |
| 105 | } // namespace android |