blob: 5001d7a2e61a503ae9e81c1ffeddbe371e5e1827 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 Sams326e0dd2009-05-22 14:03:28 -070018#include "rsThreadIO.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080019#include "rsgApiStructs.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070020
Jason Sams5f27d6f2012-02-07 15:32:08 -080021#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 Sams326e0dd2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
Jason Sams5f27d6f2012-02-07 15:32:08 -080032ThreadIO::ThreadIO() {
33 mRunning = true;
Jason Samsbda75a92012-02-16 17:21:32 -080034 mMaxInlineSize = 1024;
Jason Sams326e0dd2009-05-22 14:03:28 -070035}
36
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080037ThreadIO::~ThreadIO() {
Jason Sams326e0dd2009-05-22 14:03:28 -070038}
39
Jason Sams5f27d6f2012-02-07 15:32:08 -080040void ThreadIO::init() {
41 mToClient.init();
42 mToCore.init();
Jason Sams8c0ee652009-08-25 14:49:07 -070043}
44
Jason Sams1a4efa32011-05-17 15:01:29 -070045void ThreadIO::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080046 mRunning = false;
Jason Sams1a4efa32011-05-17 15:01:29 -070047 mToCore.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070048}
49
50void * ThreadIO::coreHeader(uint32_t cmdID, size_t dataLen) {
Steve Blockaf12ac62012-01-06 19:20:56 +000051 //ALOGE("coreHeader %i %i", cmdID, dataLen);
Jason Sams5f27d6f2012-02-07 15:32:08 -080052 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 Sams1a4efa32011-05-17 15:01:29 -070059}
60
61void ThreadIO::coreCommit() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080062 mToCore.writeAsync(&mSendBuffer, mSendLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070063}
64
65void ThreadIO::clientShutdown() {
Jason Sams1a4efa32011-05-17 15:01:29 -070066 mToClient.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070067}
68
Jason Samsbda75a92012-02-16 17:21:32 -080069void 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
74void ThreadIO::coreRead(void *data, size_t len) {
75 //ALOGV("core read %p %i", data, (int)len);
76 mToCore.read(data, len);
77}
78
Jason Sams1a4efa32011-05-17 15:01:29 -070079void ThreadIO::coreSetReturn(const void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080080 uint32_t buf;
Chris Wailes44bef6f2014-08-12 13:51:10 -070081 if (data == nullptr) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080082 data = &buf;
83 dataLen = sizeof(buf);
84 }
85
86 mToCore.readReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070087}
88
89void ThreadIO::coreGetReturn(void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080090 uint32_t buf;
Chris Wailes44bef6f2014-08-12 13:51:10 -070091 if (data == nullptr) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080092 data = &buf;
93 dataLen = sizeof(buf);
94 }
95
96 mToCore.writeWaitReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070097}
98
Jason Sams5f27d6f2012-02-07 15:32:08 -080099void ThreadIO::setTimeoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) {
100 //mToCore.setTimeoutCallback(cb, dat, timeout);
Jason Sams2382aba2011-09-13 15:41:01 -0700101}
102
Jason Sams963a2fb2012-02-09 14:36:14 -0800103bool ThreadIO::playCoreCommands(Context *con, int waitFd) {
Jason Samsa44cb292009-06-04 17:58:03 -0700104 bool ret = false;
Jason Samse0aab4a2011-08-12 15:05:15 -0700105
Jason Sams5f27d6f2012-02-07 15:32:08 -0800106 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 Onorato76371ff2009-09-23 16:37:36 -0700131 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700132
Jason Sams5f27d6f2012-02-07 15:32:08 -0800133 if (p[0].revents) {
Jason Samsbda75a92012-02-16 17:21:32 -0800134 size_t r = 0;
Matt Walab74514d2015-07-20 14:03:25 -0700135 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 Sams5f27d6f2012-02-07 15:32:08 -0800140 }
141
142 ret = true;
143 if (con->props.mLogTimes) {
144 con->timerSet(Context::RS_TIMER_INTERNAL);
145 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800146 //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 Samsbda75a92012-02-16 17:21:32 -0800152
Matt Walab74514d2015-07-20 14:03:25 -0700153 gPlaybackFuncs[cmd->cmdID](con, data, cmd->bytes);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800154
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 Samse0aab4a2011-08-12 15:05:15 -0700163 }
164 }
Stephen Hines36838462012-01-26 17:09:43 -0800165
Jason Sams5f27d6f2012-02-07 15:32:08 -0800166 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 Hines36838462012-01-26 17:09:43 -0800169 break;
170 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700171 }
Jason Samsa44cb292009-06-04 17:58:03 -0700172 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700173}
174
Jason Sams1a4efa32011-05-17 15:01:29 -0700175RsMessageToClientType ThreadIO::getClientHeader(size_t *receiveLen, uint32_t *usrID) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800176 //ALOGE("getClientHeader");
177 mToClient.read(&mLastClientHeader, sizeof(mLastClientHeader));
178
Jason Sams1a4efa32011-05-17 15:01:29 -0700179 receiveLen[0] = mLastClientHeader.bytes;
180 usrID[0] = mLastClientHeader.userID;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800181 //ALOGE("getClientHeader %i %i %i", mLastClientHeader.cmdID, usrID[0], receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700182 return (RsMessageToClientType)mLastClientHeader.cmdID;
183}
184
185RsMessageToClientType ThreadIO::getClientPayload(void *data, size_t *receiveLen,
186 uint32_t *usrID, size_t bufferLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800187 //ALOGE("getClientPayload");
Jason Sams1a4efa32011-05-17 15:01:29 -0700188 receiveLen[0] = mLastClientHeader.bytes;
189 usrID[0] = mLastClientHeader.userID;
190 if (bufferLen < mLastClientHeader.bytes) {
191 return RS_MESSAGE_TO_CLIENT_RESIZE;
192 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800193 if (receiveLen[0]) {
194 mToClient.read(data, receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700195 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800196 //ALOGE("getClientPayload x");
197 return (RsMessageToClientType)mLastClientHeader.cmdID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700198}
199
200bool ThreadIO::sendToClient(RsMessageToClientType cmdID, uint32_t usrID, const void *data,
201 size_t dataLen, bool waitForSpace) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800202
203 //ALOGE("sendToClient %i %i %i", cmdID, usrID, (int)dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700204 ClientCmdHeader hdr;
Tim Murraye4ed0872014-08-18 16:13:08 -0700205 hdr.bytes = (uint32_t)dataLen;
Jason Sams1a4efa32011-05-17 15:01:29 -0700206 hdr.cmdID = cmdID;
207 hdr.userID = usrID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700208
Jason Sams5f27d6f2012-02-07 15:32:08 -0800209 mToClient.writeAsync(&hdr, sizeof(hdr));
210 if (dataLen) {
211 mToClient.writeAsync(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700212 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800213
214 //ALOGE("sendToClient x");
215 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -0700216}
Jason Sams326e0dd2009-05-22 14:03:28 -0700217