blob: 4f3057317ab9c7bd0ac60999a442b6de6dff1ee4 [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"
18
Jason Sams326e0dd2009-05-22 14:03:28 -070019#include "rsThreadIO.h"
20
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 Sams326e0dd2009-05-22 14:03:28 -070034}
35
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080036ThreadIO::~ThreadIO() {
Jason Sams326e0dd2009-05-22 14:03:28 -070037}
38
Jason Sams5f27d6f2012-02-07 15:32:08 -080039void ThreadIO::init() {
40 mToClient.init();
41 mToCore.init();
Jason Sams8c0ee652009-08-25 14:49:07 -070042}
43
Jason Sams1a4efa32011-05-17 15:01:29 -070044void ThreadIO::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080045 mRunning = false;
Jason Sams1a4efa32011-05-17 15:01:29 -070046 mToCore.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070047}
48
49void * ThreadIO::coreHeader(uint32_t cmdID, size_t dataLen) {
Steve Blockaf12ac62012-01-06 19:20:56 +000050 //ALOGE("coreHeader %i %i", cmdID, dataLen);
Jason Sams5f27d6f2012-02-07 15:32:08 -080051 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 Sams1a4efa32011-05-17 15:01:29 -070058}
59
60void ThreadIO::coreCommit() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080061 mToCore.writeAsync(&mSendBuffer, mSendLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070062}
63
64void ThreadIO::clientShutdown() {
Jason Sams1a4efa32011-05-17 15:01:29 -070065 mToClient.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070066}
67
68void ThreadIO::coreSetReturn(const void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080069 uint32_t buf;
70 if (data == NULL) {
71 data = &buf;
72 dataLen = sizeof(buf);
73 }
74
75 mToCore.readReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070076}
77
78void ThreadIO::coreGetReturn(void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080079 uint32_t buf;
80 if (data == NULL) {
81 data = &buf;
82 dataLen = sizeof(buf);
83 }
84
85 mToCore.writeWaitReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070086}
87
Jason Sams5f27d6f2012-02-07 15:32:08 -080088void ThreadIO::setTimeoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) {
89 //mToCore.setTimeoutCallback(cb, dat, timeout);
Jason Sams2382aba2011-09-13 15:41:01 -070090}
91
Jason Sams963a2fb2012-02-09 14:36:14 -080092bool ThreadIO::playCoreCommands(Context *con, int waitFd) {
Jason Samsa44cb292009-06-04 17:58:03 -070093 bool ret = false;
Jason Samse0aab4a2011-08-12 15:05:15 -070094
Jason Sams5f27d6f2012-02-07 15:32:08 -080095 uint8_t buf[2 * 1024];
96 const CoreCmdHeader *cmd = (const CoreCmdHeader *)&buf[0];
97 const void * data = (const void *)&buf[sizeof(CoreCmdHeader)];
98
99 struct pollfd p[2];
100 p[0].fd = mToCore.getReadFd();
101 p[0].events = POLLIN;
102 p[0].revents = 0;
103 p[1].fd = waitFd;
104 p[1].events = POLLIN;
105 p[1].revents = 0;
106 int pollCount = 1;
107 if (waitFd >= 0) {
108 pollCount = 2;
109 }
110
111 if (con->props.mLogTimes) {
112 con->timerSet(Context::RS_TIMER_IDLE);
113 }
114
115 int waitTime = -1;
116 while (mRunning) {
117 int pr = poll(p, pollCount, waitTime);
118 if (pr <= 0) {
119 break;
Joe Onorato76371ff2009-09-23 16:37:36 -0700120 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700121
Jason Sams5f27d6f2012-02-07 15:32:08 -0800122 if (p[0].revents) {
123 size_t r = mToCore.read(&buf[0], sizeof(CoreCmdHeader));
124 mToCore.read(&buf[sizeof(CoreCmdHeader)], cmd->bytes);
125
126 if (r != sizeof(CoreCmdHeader)) {
127 // exception or timeout occurred.
128 break;
129 }
130
131 ret = true;
132 if (con->props.mLogTimes) {
133 con->timerSet(Context::RS_TIMER_INTERNAL);
134 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800135 //ALOGV("playCoreCommands 3 %i %i", cmd->cmdID, cmd->bytes);
136
137 if (cmd->cmdID >= (sizeof(gPlaybackFuncs) / sizeof(void *))) {
138 rsAssert(cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)));
139 ALOGE("playCoreCommands error con %p, cmd %i", con, cmd->cmdID);
140 }
141 gPlaybackFuncs[cmd->cmdID](con, data, cmd->bytes);
142
143 if (con->props.mLogTimes) {
144 con->timerSet(Context::RS_TIMER_IDLE);
145 }
146
147 if (waitFd < 0) {
148 // If we don't have a secondary wait object we should stop blocking now
149 // that at least one command has been processed.
150 waitTime = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700151 }
152 }
Stephen Hines36838462012-01-26 17:09:43 -0800153
Jason Sams5f27d6f2012-02-07 15:32:08 -0800154 if (p[1].revents && !p[0].revents) {
155 // We want to finish processing fifo events before processing the vsync.
156 // Otherwise we can end up falling behind and having tremendous lag.
Stephen Hines36838462012-01-26 17:09:43 -0800157 break;
158 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 }
Jason Samsa44cb292009-06-04 17:58:03 -0700160 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700161}
162
Jason Sams1a4efa32011-05-17 15:01:29 -0700163RsMessageToClientType ThreadIO::getClientHeader(size_t *receiveLen, uint32_t *usrID) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800164 //ALOGE("getClientHeader");
165 mToClient.read(&mLastClientHeader, sizeof(mLastClientHeader));
166
Jason Sams1a4efa32011-05-17 15:01:29 -0700167 receiveLen[0] = mLastClientHeader.bytes;
168 usrID[0] = mLastClientHeader.userID;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800169 //ALOGE("getClientHeader %i %i %i", mLastClientHeader.cmdID, usrID[0], receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700170 return (RsMessageToClientType)mLastClientHeader.cmdID;
171}
172
173RsMessageToClientType ThreadIO::getClientPayload(void *data, size_t *receiveLen,
174 uint32_t *usrID, size_t bufferLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800175 //ALOGE("getClientPayload");
Jason Sams1a4efa32011-05-17 15:01:29 -0700176 receiveLen[0] = mLastClientHeader.bytes;
177 usrID[0] = mLastClientHeader.userID;
178 if (bufferLen < mLastClientHeader.bytes) {
179 return RS_MESSAGE_TO_CLIENT_RESIZE;
180 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800181 if (receiveLen[0]) {
182 mToClient.read(data, receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700183 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800184 //ALOGE("getClientPayload x");
185 return (RsMessageToClientType)mLastClientHeader.cmdID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700186}
187
188bool ThreadIO::sendToClient(RsMessageToClientType cmdID, uint32_t usrID, const void *data,
189 size_t dataLen, bool waitForSpace) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800190
191 //ALOGE("sendToClient %i %i %i", cmdID, usrID, (int)dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700192 ClientCmdHeader hdr;
193 hdr.bytes = dataLen;
194 hdr.cmdID = cmdID;
195 hdr.userID = usrID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700196
Jason Sams5f27d6f2012-02-07 15:32:08 -0800197 mToClient.writeAsync(&hdr, sizeof(hdr));
198 if (dataLen) {
199 mToClient.writeAsync(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700200 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800201
202 //ALOGE("sendToClient x");
203 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -0700204}
Jason Sams326e0dd2009-05-22 14:03:28 -0700205