Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 "rsContext.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include "rsThreadIO.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 19 | #include "rsgApiStructs.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <poll.h> |
| 27 | |
| 28 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | using namespace android; |
| 30 | using namespace android::renderscript; |
| 31 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 32 | ThreadIO::ThreadIO() { |
| 33 | mRunning = true; |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 34 | mMaxInlineSize = 1024; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 37 | ThreadIO::~ThreadIO() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 40 | void ThreadIO::init() { |
| 41 | mToClient.init(); |
| 42 | mToCore.init(); |
Jason Sams | 8c0ee65 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 45 | void ThreadIO::shutdown() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 46 | mRunning = false; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 47 | mToCore.shutdown(); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void * ThreadIO::coreHeader(uint32_t cmdID, size_t dataLen) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 51 | //ALOGE("coreHeader %i %i", cmdID, dataLen); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 52 | CoreCmdHeader *hdr = (CoreCmdHeader *)&mSendBuffer[0]; |
| 53 | hdr->bytes = dataLen; |
| 54 | hdr->cmdID = cmdID; |
| 55 | mSendLen = dataLen + sizeof(CoreCmdHeader); |
| 56 | //mToCoreSocket.writeAsync(&hdr, sizeof(hdr)); |
| 57 | //ALOGE("coreHeader ret "); |
| 58 | return &mSendBuffer[sizeof(CoreCmdHeader)]; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void ThreadIO::coreCommit() { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 62 | mToCore.writeAsync(&mSendBuffer, mSendLen); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ThreadIO::clientShutdown() { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 66 | mToClient.shutdown(); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 69 | void ThreadIO::coreWrite(const void *data, size_t len) { |
| 70 | //ALOGV("core write %p %i", data, (int)len); |
| 71 | mToCore.writeAsync(data, len, true); |
| 72 | } |
| 73 | |
| 74 | void ThreadIO::coreRead(void *data, size_t len) { |
| 75 | //ALOGV("core read %p %i", data, (int)len); |
| 76 | mToCore.read(data, len); |
| 77 | } |
| 78 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 79 | void ThreadIO::coreSetReturn(const void *data, size_t dataLen) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 80 | uint32_t buf; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 81 | if (data == nullptr) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 82 | data = &buf; |
| 83 | dataLen = sizeof(buf); |
| 84 | } |
| 85 | |
| 86 | mToCore.readReturn(data, dataLen); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ThreadIO::coreGetReturn(void *data, size_t dataLen) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 90 | uint32_t buf; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 91 | if (data == nullptr) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 92 | data = &buf; |
| 93 | dataLen = sizeof(buf); |
| 94 | } |
| 95 | |
| 96 | mToCore.writeWaitReturn(data, dataLen); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 99 | void ThreadIO::setTimeoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) { |
| 100 | //mToCore.setTimeoutCallback(cb, dat, timeout); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jason Sams | 963a2fb | 2012-02-09 14:36:14 -0800 | [diff] [blame] | 103 | bool ThreadIO::playCoreCommands(Context *con, int waitFd) { |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 104 | bool ret = false; |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 105 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 106 | uint8_t buf[2 * 1024]; |
| 107 | const CoreCmdHeader *cmd = (const CoreCmdHeader *)&buf[0]; |
| 108 | const void * data = (const void *)&buf[sizeof(CoreCmdHeader)]; |
| 109 | |
| 110 | struct pollfd p[2]; |
| 111 | p[0].fd = mToCore.getReadFd(); |
| 112 | p[0].events = POLLIN; |
| 113 | p[0].revents = 0; |
| 114 | p[1].fd = waitFd; |
| 115 | p[1].events = POLLIN; |
| 116 | p[1].revents = 0; |
| 117 | int pollCount = 1; |
| 118 | if (waitFd >= 0) { |
| 119 | pollCount = 2; |
| 120 | } |
| 121 | |
| 122 | if (con->props.mLogTimes) { |
| 123 | con->timerSet(Context::RS_TIMER_IDLE); |
| 124 | } |
| 125 | |
| 126 | int waitTime = -1; |
| 127 | while (mRunning) { |
| 128 | int pr = poll(p, pollCount, waitTime); |
| 129 | if (pr <= 0) { |
| 130 | break; |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 131 | } |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 132 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 133 | if (p[0].revents) { |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 134 | size_t r = 0; |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 135 | r = mToCore.read(&buf[0], sizeof(CoreCmdHeader)); |
| 136 | mToCore.read(&buf[sizeof(CoreCmdHeader)], cmd->bytes); |
| 137 | if (r != sizeof(CoreCmdHeader)) { |
| 138 | // exception or timeout occurred. |
| 139 | break; |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | ret = true; |
| 143 | if (con->props.mLogTimes) { |
| 144 | con->timerSet(Context::RS_TIMER_INTERNAL); |
| 145 | } |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 146 | //ALOGV("playCoreCommands 3 %i %i", cmd->cmdID, cmd->bytes); |
| 147 | |
| 148 | if (cmd->cmdID >= (sizeof(gPlaybackFuncs) / sizeof(void *))) { |
| 149 | rsAssert(cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *))); |
| 150 | ALOGE("playCoreCommands error con %p, cmd %i", con, cmd->cmdID); |
| 151 | } |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 152 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 153 | gPlaybackFuncs[cmd->cmdID](con, data, cmd->bytes); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 154 | |
| 155 | if (con->props.mLogTimes) { |
| 156 | con->timerSet(Context::RS_TIMER_IDLE); |
| 157 | } |
| 158 | |
| 159 | if (waitFd < 0) { |
| 160 | // If we don't have a secondary wait object we should stop blocking now |
| 161 | // that at least one command has been processed. |
| 162 | waitTime = 0; |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Stephen Hines | 3683846 | 2012-01-26 17:09:43 -0800 | [diff] [blame] | 165 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 166 | if (p[1].revents && !p[0].revents) { |
| 167 | // We want to finish processing fifo events before processing the vsync. |
| 168 | // Otherwise we can end up falling behind and having tremendous lag. |
Stephen Hines | 3683846 | 2012-01-26 17:09:43 -0800 | [diff] [blame] | 169 | break; |
| 170 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 171 | } |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 172 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 175 | RsMessageToClientType ThreadIO::getClientHeader(size_t *receiveLen, uint32_t *usrID) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 176 | //ALOGE("getClientHeader"); |
| 177 | mToClient.read(&mLastClientHeader, sizeof(mLastClientHeader)); |
| 178 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 179 | receiveLen[0] = mLastClientHeader.bytes; |
| 180 | usrID[0] = mLastClientHeader.userID; |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 181 | //ALOGE("getClientHeader %i %i %i", mLastClientHeader.cmdID, usrID[0], receiveLen[0]); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 182 | return (RsMessageToClientType)mLastClientHeader.cmdID; |
| 183 | } |
| 184 | |
| 185 | RsMessageToClientType ThreadIO::getClientPayload(void *data, size_t *receiveLen, |
| 186 | uint32_t *usrID, size_t bufferLen) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 187 | //ALOGE("getClientPayload"); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 188 | receiveLen[0] = mLastClientHeader.bytes; |
| 189 | usrID[0] = mLastClientHeader.userID; |
| 190 | if (bufferLen < mLastClientHeader.bytes) { |
| 191 | return RS_MESSAGE_TO_CLIENT_RESIZE; |
| 192 | } |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 193 | if (receiveLen[0]) { |
| 194 | mToClient.read(data, receiveLen[0]); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 195 | } |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 196 | //ALOGE("getClientPayload x"); |
| 197 | return (RsMessageToClientType)mLastClientHeader.cmdID; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool ThreadIO::sendToClient(RsMessageToClientType cmdID, uint32_t usrID, const void *data, |
| 201 | size_t dataLen, bool waitForSpace) { |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 202 | |
| 203 | //ALOGE("sendToClient %i %i %i", cmdID, usrID, (int)dataLen); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 204 | ClientCmdHeader hdr; |
Tim Murray | e4ed087 | 2014-08-18 16:13:08 -0700 | [diff] [blame] | 205 | hdr.bytes = (uint32_t)dataLen; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 206 | hdr.cmdID = cmdID; |
| 207 | hdr.userID = usrID; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 208 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 209 | mToClient.writeAsync(&hdr, sizeof(hdr)); |
| 210 | if (dataLen) { |
| 211 | mToClient.writeAsync(data, dataLen); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 212 | } |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 213 | |
| 214 | //ALOGE("sendToClient x"); |
| 215 | return true; |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 216 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 217 | |