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