Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 1 | /* Copyright 2015 Pierre Ossman for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | |
Pierre Ossman | 8635062 | 2015-11-10 13:02:12 +0100 | [diff] [blame] | 22 | #include <rfb/CConnection.h> |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 23 | #include <rfb/DecodeManager.h> |
| 24 | #include <rfb/Decoder.h> |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 25 | #include <rfb/Region.h> |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 26 | |
| 27 | #include <rfb/LogWriter.h> |
| 28 | |
| 29 | #include <rdr/Exception.h> |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 30 | #include <rdr/MemOutStream.h> |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 31 | |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 32 | #include <os/Mutex.h> |
| 33 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 34 | using namespace rfb; |
| 35 | |
| 36 | static LogWriter vlog("DecodeManager"); |
| 37 | |
| 38 | DecodeManager::DecodeManager(CConnection *conn) : |
| 39 | conn(conn) |
| 40 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 41 | int i; |
| 42 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 43 | memset(decoders, 0, sizeof(decoders)); |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 44 | |
| 45 | queueMutex = new os::Mutex(); |
| 46 | producerCond = new os::Condition(queueMutex); |
| 47 | consumerCond = new os::Condition(queueMutex); |
| 48 | |
| 49 | // Just a single thread for now as we haven't sorted out the |
| 50 | // dependencies between rects |
| 51 | for (i = 0;i < 1;i++) { |
| 52 | // Twice as many possible entries in the queue as there |
| 53 | // are worker threads to make sure they don't stall |
| 54 | freeBuffers.push_back(new rdr::MemOutStream()); |
| 55 | freeBuffers.push_back(new rdr::MemOutStream()); |
| 56 | |
| 57 | threads.push_back(new DecodeThread(this)); |
| 58 | } |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | DecodeManager::~DecodeManager() |
| 62 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 63 | while (!threads.empty()) { |
| 64 | delete threads.back(); |
| 65 | threads.pop_back(); |
| 66 | } |
| 67 | |
| 68 | while (!freeBuffers.empty()) { |
| 69 | delete freeBuffers.back(); |
| 70 | freeBuffers.pop_back(); |
| 71 | } |
| 72 | |
| 73 | delete consumerCond; |
| 74 | delete producerCond; |
| 75 | delete queueMutex; |
| 76 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 77 | for (size_t i = 0; i < sizeof(decoders)/sizeof(decoders[0]); i++) |
| 78 | delete decoders[i]; |
| 79 | } |
| 80 | |
| 81 | void DecodeManager::decodeRect(const Rect& r, int encoding, |
| 82 | ModifiablePixelBuffer* pb) |
| 83 | { |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 84 | Decoder *decoder; |
| 85 | rdr::MemOutStream *bufferStream; |
| 86 | |
| 87 | QueueEntry *entry; |
| 88 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 89 | assert(pb != NULL); |
| 90 | |
| 91 | if (!Decoder::supported(encoding)) { |
| 92 | vlog.error("Unknown encoding %d", encoding); |
| 93 | throw rdr::Exception("Unknown encoding"); |
| 94 | } |
| 95 | |
| 96 | if (!decoders[encoding]) { |
Pierre Ossman | 8635062 | 2015-11-10 13:02:12 +0100 | [diff] [blame] | 97 | decoders[encoding] = Decoder::createDecoder(encoding); |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 98 | if (!decoders[encoding]) { |
| 99 | vlog.error("Unknown encoding %d", encoding); |
| 100 | throw rdr::Exception("Unknown encoding"); |
| 101 | } |
| 102 | } |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 103 | |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 104 | decoder = decoders[encoding]; |
| 105 | |
| 106 | // Wait for an available memory buffer |
| 107 | queueMutex->lock(); |
| 108 | |
| 109 | while (freeBuffers.empty()) |
| 110 | producerCond->wait(); |
| 111 | |
| 112 | // Don't pop the buffer in case we throw an exception |
| 113 | // whilst reading |
| 114 | bufferStream = freeBuffers.front(); |
| 115 | |
| 116 | queueMutex->unlock(); |
| 117 | |
| 118 | // Read the rect |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 119 | bufferStream->clear(); |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 120 | decoder->readRect(r, conn->getInStream(), conn->cp, bufferStream); |
| 121 | |
| 122 | // Then try to put it on the queue |
| 123 | entry = new QueueEntry; |
| 124 | |
| 125 | entry->active = false; |
| 126 | entry->rect = r; |
| 127 | entry->encoding = encoding; |
| 128 | entry->decoder = decoder; |
| 129 | entry->cp = &conn->cp; |
| 130 | entry->pb = pb; |
| 131 | entry->bufferStream = bufferStream; |
| 132 | |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 133 | decoder->getAffectedRegion(r, bufferStream->data(), |
| 134 | bufferStream->length(), conn->cp, |
| 135 | &entry->affectedRegion); |
| 136 | |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 137 | queueMutex->lock(); |
| 138 | |
| 139 | // The workers add buffers to the end so it's safe to assume |
| 140 | // the front is still the same buffer |
| 141 | freeBuffers.pop_front(); |
| 142 | |
| 143 | workQueue.push_back(entry); |
| 144 | |
| 145 | // We only put a single entry on the queue so waking a single |
| 146 | // thread is sufficient |
| 147 | consumerCond->signal(); |
| 148 | |
| 149 | queueMutex->unlock(); |
| 150 | } |
| 151 | |
| 152 | void DecodeManager::flush() |
| 153 | { |
| 154 | queueMutex->lock(); |
| 155 | |
| 156 | while (!workQueue.empty()) |
| 157 | producerCond->wait(); |
| 158 | |
| 159 | queueMutex->unlock(); |
| 160 | } |
| 161 | |
| 162 | DecodeManager::DecodeThread::DecodeThread(DecodeManager* manager) |
| 163 | { |
| 164 | this->manager = manager; |
| 165 | |
| 166 | stopRequested = false; |
| 167 | |
| 168 | start(); |
| 169 | } |
| 170 | |
| 171 | DecodeManager::DecodeThread::~DecodeThread() |
| 172 | { |
| 173 | stop(); |
| 174 | wait(); |
| 175 | } |
| 176 | |
| 177 | void DecodeManager::DecodeThread::stop() |
| 178 | { |
| 179 | os::AutoMutex a(manager->queueMutex); |
| 180 | |
| 181 | if (!isRunning()) |
| 182 | return; |
| 183 | |
| 184 | stopRequested = true; |
| 185 | |
| 186 | // We can't wake just this thread, so wake everyone |
| 187 | manager->consumerCond->broadcast(); |
| 188 | } |
| 189 | |
| 190 | void DecodeManager::DecodeThread::worker() |
| 191 | { |
| 192 | manager->queueMutex->lock(); |
| 193 | |
| 194 | while (!stopRequested) { |
| 195 | DecodeManager::QueueEntry *entry; |
| 196 | |
| 197 | // Look for an available entry in the work queue |
| 198 | entry = findEntry(); |
| 199 | if (entry == NULL) { |
| 200 | // Wait and try again |
| 201 | manager->consumerCond->wait(); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | // This is ours now |
| 206 | entry->active = true; |
| 207 | |
| 208 | manager->queueMutex->unlock(); |
| 209 | |
| 210 | // Do the actual decoding |
| 211 | try { |
| 212 | entry->decoder->decodeRect(entry->rect, entry->bufferStream->data(), |
| 213 | entry->bufferStream->length(), |
| 214 | *entry->cp, entry->pb); |
| 215 | } catch(...) { |
| 216 | // FIXME: Try to get the exception back to the main thread |
| 217 | assert(false); |
| 218 | } |
| 219 | |
| 220 | manager->queueMutex->lock(); |
| 221 | |
| 222 | // Remove the entry from the queue and give back the memory buffer |
| 223 | manager->freeBuffers.push_back(entry->bufferStream); |
| 224 | manager->workQueue.remove(entry); |
| 225 | delete entry; |
| 226 | |
| 227 | // Wake the main thread in case it is waiting for a memory buffer |
| 228 | manager->producerCond->signal(); |
| 229 | // This rect might have been blocking multiple other rects, so |
| 230 | // wake up every worker thread |
| 231 | if (manager->workQueue.size() > 1) |
| 232 | manager->consumerCond->broadcast(); |
| 233 | } |
| 234 | |
| 235 | manager->queueMutex->unlock(); |
| 236 | } |
| 237 | |
| 238 | DecodeManager::QueueEntry* DecodeManager::DecodeThread::findEntry() |
| 239 | { |
| 240 | std::list<DecodeManager::QueueEntry*>::iterator iter; |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 241 | Region lockedRegion; |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 242 | |
| 243 | if (manager->workQueue.empty()) |
| 244 | return NULL; |
| 245 | |
| 246 | if (!manager->workQueue.front()->active) |
| 247 | return manager->workQueue.front(); |
| 248 | |
| 249 | for (iter = manager->workQueue.begin(); |
| 250 | iter != manager->workQueue.end(); |
| 251 | ++iter) { |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 252 | DecodeManager::QueueEntry* entry; |
| 253 | |
| 254 | entry = *iter; |
| 255 | |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 256 | // Another thread working on this? |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 257 | if (entry->active) |
| 258 | goto next; |
| 259 | |
| 260 | // Check overlap with earlier rectangles |
| 261 | if (!lockedRegion.intersect(entry->affectedRegion).is_empty()) |
| 262 | goto next; |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 263 | |
| 264 | // FIXME: check dependencies between rects |
| 265 | |
Pierre Ossman | 1412789 | 2015-11-12 13:17:42 +0100 | [diff] [blame^] | 266 | return entry; |
| 267 | |
| 268 | next: |
| 269 | lockedRegion.assign_union(entry->affectedRegion); |
Pierre Ossman | 504afa2 | 2015-11-12 12:21:58 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return NULL; |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 273 | } |