blob: 8a0a5dce20766fd13a34ecc995431c7443baab1f [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 mPureFifo = false;
35 mMaxInlineSize = 1024;
Jason Sams326e0dd2009-05-22 14:03:28 -070036}
37
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038ThreadIO::~ThreadIO() {
Jason Sams326e0dd2009-05-22 14:03:28 -070039}
40
Jason Sams5f27d6f2012-02-07 15:32:08 -080041void ThreadIO::init() {
42 mToClient.init();
43 mToCore.init();
Jason Sams8c0ee652009-08-25 14:49:07 -070044}
45
Jason Sams1a4efa32011-05-17 15:01:29 -070046void ThreadIO::shutdown() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080047 mRunning = false;
Jason Sams1a4efa32011-05-17 15:01:29 -070048 mToCore.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070049}
50
51void * ThreadIO::coreHeader(uint32_t cmdID, size_t dataLen) {
Steve Blockaf12ac62012-01-06 19:20:56 +000052 //ALOGE("coreHeader %i %i", cmdID, dataLen);
Jason Sams5f27d6f2012-02-07 15:32:08 -080053 CoreCmdHeader *hdr = (CoreCmdHeader *)&mSendBuffer[0];
54 hdr->bytes = dataLen;
55 hdr->cmdID = cmdID;
56 mSendLen = dataLen + sizeof(CoreCmdHeader);
57 //mToCoreSocket.writeAsync(&hdr, sizeof(hdr));
58 //ALOGE("coreHeader ret ");
59 return &mSendBuffer[sizeof(CoreCmdHeader)];
Jason Sams1a4efa32011-05-17 15:01:29 -070060}
61
62void ThreadIO::coreCommit() {
Jason Sams5f27d6f2012-02-07 15:32:08 -080063 mToCore.writeAsync(&mSendBuffer, mSendLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070064}
65
66void ThreadIO::clientShutdown() {
Jason Sams1a4efa32011-05-17 15:01:29 -070067 mToClient.shutdown();
Jason Sams1a4efa32011-05-17 15:01:29 -070068}
69
Jason Samsbda75a92012-02-16 17:21:32 -080070void ThreadIO::coreWrite(const void *data, size_t len) {
71 //ALOGV("core write %p %i", data, (int)len);
72 mToCore.writeAsync(data, len, true);
73}
74
75void ThreadIO::coreRead(void *data, size_t len) {
76 //ALOGV("core read %p %i", data, (int)len);
77 mToCore.read(data, len);
78}
79
Jason Sams1a4efa32011-05-17 15:01:29 -070080void ThreadIO::coreSetReturn(const void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080081 uint32_t buf;
82 if (data == NULL) {
83 data = &buf;
84 dataLen = sizeof(buf);
85 }
86
87 mToCore.readReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070088}
89
90void ThreadIO::coreGetReturn(void *data, size_t dataLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -080091 uint32_t buf;
92 if (data == NULL) {
93 data = &buf;
94 dataLen = sizeof(buf);
95 }
96
97 mToCore.writeWaitReturn(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -070098}
99
Jason Sams5f27d6f2012-02-07 15:32:08 -0800100void ThreadIO::setTimeoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) {
101 //mToCore.setTimeoutCallback(cb, dat, timeout);
Jason Sams2382aba2011-09-13 15:41:01 -0700102}
103
Jason Sams963a2fb2012-02-09 14:36:14 -0800104bool ThreadIO::playCoreCommands(Context *con, int waitFd) {
Jason Samsa44cb292009-06-04 17:58:03 -0700105 bool ret = false;
Jason Samsbda75a92012-02-16 17:21:32 -0800106 const bool isLocal = !isPureFifo();
Jason Samse0aab4a2011-08-12 15:05:15 -0700107
Jason Sams5f27d6f2012-02-07 15:32:08 -0800108 uint8_t buf[2 * 1024];
109 const CoreCmdHeader *cmd = (const CoreCmdHeader *)&buf[0];
110 const void * data = (const void *)&buf[sizeof(CoreCmdHeader)];
111
112 struct pollfd p[2];
113 p[0].fd = mToCore.getReadFd();
114 p[0].events = POLLIN;
115 p[0].revents = 0;
116 p[1].fd = waitFd;
117 p[1].events = POLLIN;
118 p[1].revents = 0;
119 int pollCount = 1;
120 if (waitFd >= 0) {
121 pollCount = 2;
122 }
123
124 if (con->props.mLogTimes) {
125 con->timerSet(Context::RS_TIMER_IDLE);
126 }
127
128 int waitTime = -1;
129 while (mRunning) {
130 int pr = poll(p, pollCount, waitTime);
131 if (pr <= 0) {
132 break;
Joe Onorato76371ff2009-09-23 16:37:36 -0700133 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700134
Jason Sams5f27d6f2012-02-07 15:32:08 -0800135 if (p[0].revents) {
Jason Samsbda75a92012-02-16 17:21:32 -0800136 size_t r = 0;
137 if (isLocal) {
138 r = mToCore.read(&buf[0], sizeof(CoreCmdHeader));
139 mToCore.read(&buf[sizeof(CoreCmdHeader)], cmd->bytes);
140 if (r != sizeof(CoreCmdHeader)) {
141 // exception or timeout occurred.
142 break;
143 }
144 } else {
145 r = mToCore.read((void *)&cmd->cmdID, sizeof(cmd->cmdID));
Jason Sams5f27d6f2012-02-07 15:32:08 -0800146 }
147
Jason Samsbda75a92012-02-16 17:21:32 -0800148
Jason Sams5f27d6f2012-02-07 15:32:08 -0800149 ret = true;
150 if (con->props.mLogTimes) {
151 con->timerSet(Context::RS_TIMER_INTERNAL);
152 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800153 //ALOGV("playCoreCommands 3 %i %i", cmd->cmdID, cmd->bytes);
154
155 if (cmd->cmdID >= (sizeof(gPlaybackFuncs) / sizeof(void *))) {
156 rsAssert(cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)));
157 ALOGE("playCoreCommands error con %p, cmd %i", con, cmd->cmdID);
158 }
Jason Samsbda75a92012-02-16 17:21:32 -0800159
160 if (isLocal) {
161 gPlaybackFuncs[cmd->cmdID](con, data, cmd->bytes);
162 } else {
163 gPlaybackRemoteFuncs[cmd->cmdID](con, this);
164 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800165
166 if (con->props.mLogTimes) {
167 con->timerSet(Context::RS_TIMER_IDLE);
168 }
169
170 if (waitFd < 0) {
171 // If we don't have a secondary wait object we should stop blocking now
172 // that at least one command has been processed.
173 waitTime = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700174 }
175 }
Stephen Hines36838462012-01-26 17:09:43 -0800176
Jason Sams5f27d6f2012-02-07 15:32:08 -0800177 if (p[1].revents && !p[0].revents) {
178 // We want to finish processing fifo events before processing the vsync.
179 // Otherwise we can end up falling behind and having tremendous lag.
Stephen Hines36838462012-01-26 17:09:43 -0800180 break;
181 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700182 }
Jason Samsa44cb292009-06-04 17:58:03 -0700183 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700184}
185
Jason Sams1a4efa32011-05-17 15:01:29 -0700186RsMessageToClientType ThreadIO::getClientHeader(size_t *receiveLen, uint32_t *usrID) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800187 //ALOGE("getClientHeader");
188 mToClient.read(&mLastClientHeader, sizeof(mLastClientHeader));
189
Jason Sams1a4efa32011-05-17 15:01:29 -0700190 receiveLen[0] = mLastClientHeader.bytes;
191 usrID[0] = mLastClientHeader.userID;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800192 //ALOGE("getClientHeader %i %i %i", mLastClientHeader.cmdID, usrID[0], receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700193 return (RsMessageToClientType)mLastClientHeader.cmdID;
194}
195
196RsMessageToClientType ThreadIO::getClientPayload(void *data, size_t *receiveLen,
197 uint32_t *usrID, size_t bufferLen) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800198 //ALOGE("getClientPayload");
Jason Sams1a4efa32011-05-17 15:01:29 -0700199 receiveLen[0] = mLastClientHeader.bytes;
200 usrID[0] = mLastClientHeader.userID;
201 if (bufferLen < mLastClientHeader.bytes) {
202 return RS_MESSAGE_TO_CLIENT_RESIZE;
203 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800204 if (receiveLen[0]) {
205 mToClient.read(data, receiveLen[0]);
Jason Sams1a4efa32011-05-17 15:01:29 -0700206 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800207 //ALOGE("getClientPayload x");
208 return (RsMessageToClientType)mLastClientHeader.cmdID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700209}
210
211bool ThreadIO::sendToClient(RsMessageToClientType cmdID, uint32_t usrID, const void *data,
212 size_t dataLen, bool waitForSpace) {
Jason Sams5f27d6f2012-02-07 15:32:08 -0800213
214 //ALOGE("sendToClient %i %i %i", cmdID, usrID, (int)dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700215 ClientCmdHeader hdr;
216 hdr.bytes = dataLen;
217 hdr.cmdID = cmdID;
218 hdr.userID = usrID;
Jason Sams1a4efa32011-05-17 15:01:29 -0700219
Jason Sams5f27d6f2012-02-07 15:32:08 -0800220 mToClient.writeAsync(&hdr, sizeof(hdr));
221 if (dataLen) {
222 mToClient.writeAsync(data, dataLen);
Jason Sams1a4efa32011-05-17 15:01:29 -0700223 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800224
225 //ALOGE("sendToClient x");
226 return true;
Jason Sams1a4efa32011-05-17 15:01:29 -0700227}
Jason Sams326e0dd2009-05-22 14:03:28 -0700228