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