blob: 8e4b9883a3538db1ff167fb285500f11f1ae5013 [file] [log] [blame]
Jason Samsd19f10d2009-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 Samsd19f10d2009-05-22 14:03:28 -070019#include "rsThreadIO.h"
20
Jason Sams4c2e4c82012-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 Samsd19f10d2009-05-22 14:03:28 -070029using namespace android;
30using namespace android::renderscript;
31
Jason Sams4c2e4c82012-02-07 15:32:08 -080032ThreadIO::ThreadIO() {
33 mRunning = true;
Jason Samsd19f10d2009-05-22 14:03:28 -070034}
35
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080036ThreadIO::~ThreadIO() {
Jason Samsd19f10d2009-05-22 14:03:28 -070037}
38
Jason Sams4c2e4c82012-02-07 15:32:08 -080039void ThreadIO::init() {
40 mToClient.init();
41 mToCore.init();
Jason Samsf5b45962009-08-25 14:49:07 -070042}
43
Jason Samsedbfabd2011-05-17 15:01:29 -070044void ThreadIO::shutdown() {
Jason Sams4c2e4c82012-02-07 15:32:08 -080045 mRunning = false;
Jason Samsedbfabd2011-05-17 15:01:29 -070046 mToCore.shutdown();
Jason Samsedbfabd2011-05-17 15:01:29 -070047}
48
49void * ThreadIO::coreHeader(uint32_t cmdID, size_t dataLen) {
Steve Block3762c312012-01-06 19:20:56 +000050 //ALOGE("coreHeader %i %i", cmdID, dataLen);
Jason Sams4c2e4c82012-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 Samsedbfabd2011-05-17 15:01:29 -070058}
59
60void ThreadIO::coreCommit() {
Jason Sams4c2e4c82012-02-07 15:32:08 -080061 mToCore.writeAsync(&mSendBuffer, mSendLen);
Jason Samsedbfabd2011-05-17 15:01:29 -070062}
63
64void ThreadIO::clientShutdown() {
Jason Samsedbfabd2011-05-17 15:01:29 -070065 mToClient.shutdown();
Jason Samsedbfabd2011-05-17 15:01:29 -070066}
67
68void ThreadIO::coreSetReturn(const void *data, size_t dataLen) {
Jason Sams4c2e4c82012-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 Samsedbfabd2011-05-17 15:01:29 -070076}
77
78void ThreadIO::coreGetReturn(void *data, size_t dataLen) {
Jason Sams4c2e4c82012-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 Samsedbfabd2011-05-17 15:01:29 -070086}
87
Jason Sams4c2e4c82012-02-07 15:32:08 -080088void ThreadIO::setTimeoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) {
89 //mToCore.setTimeoutCallback(cb, dat, timeout);
Jason Sams5316b9e2011-09-13 15:41:01 -070090}
91
Jason Sams4c2e4c82012-02-07 15:32:08 -080092bool ThreadIO::playCoreCommands(Context *con, bool waitForCommand, int waitFd) {
Jason Samsa09f11d2009-06-04 17:58:03 -070093 bool ret = false;
Jason Samsbfc78912011-08-12 15:05:15 -070094
Jason Sams4c2e4c82012-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 Onorato9ac2c662009-09-23 16:37:36 -0700120 }
Jason Samsbfc78912011-08-12 15:05:15 -0700121
Jason Sams4c2e4c82012-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 }
135 waitForCommand = false;
136 //ALOGV("playCoreCommands 3 %i %i", cmd->cmdID, cmd->bytes);
137
138 if (cmd->cmdID >= (sizeof(gPlaybackFuncs) / sizeof(void *))) {
139 rsAssert(cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)));
140 ALOGE("playCoreCommands error con %p, cmd %i", con, cmd->cmdID);
141 }
142 gPlaybackFuncs[cmd->cmdID](con, data, cmd->bytes);
143
144 if (con->props.mLogTimes) {
145 con->timerSet(Context::RS_TIMER_IDLE);
146 }
147
148 if (waitFd < 0) {
149 // If we don't have a secondary wait object we should stop blocking now
150 // that at least one command has been processed.
151 waitTime = 0;
Jason Samsbfc78912011-08-12 15:05:15 -0700152 }
153 }
Stephen Hinese3f9cc62012-01-26 17:09:43 -0800154
Jason Sams4c2e4c82012-02-07 15:32:08 -0800155 if (p[1].revents && !p[0].revents) {
156 // We want to finish processing fifo events before processing the vsync.
157 // Otherwise we can end up falling behind and having tremendous lag.
Stephen Hinese3f9cc62012-01-26 17:09:43 -0800158 break;
159 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700160 }
Jason Samsa09f11d2009-06-04 17:58:03 -0700161 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700162}
163
Jason Samsedbfabd2011-05-17 15:01:29 -0700164RsMessageToClientType ThreadIO::getClientHeader(size_t *receiveLen, uint32_t *usrID) {
Jason Sams4c2e4c82012-02-07 15:32:08 -0800165 //ALOGE("getClientHeader");
166 mToClient.read(&mLastClientHeader, sizeof(mLastClientHeader));
167
Jason Samsedbfabd2011-05-17 15:01:29 -0700168 receiveLen[0] = mLastClientHeader.bytes;
169 usrID[0] = mLastClientHeader.userID;
Jason Sams4c2e4c82012-02-07 15:32:08 -0800170 //ALOGE("getClientHeader %i %i %i", mLastClientHeader.cmdID, usrID[0], receiveLen[0]);
Jason Samsedbfabd2011-05-17 15:01:29 -0700171 return (RsMessageToClientType)mLastClientHeader.cmdID;
172}
173
174RsMessageToClientType ThreadIO::getClientPayload(void *data, size_t *receiveLen,
175 uint32_t *usrID, size_t bufferLen) {
Jason Sams4c2e4c82012-02-07 15:32:08 -0800176 //ALOGE("getClientPayload");
Jason Samsedbfabd2011-05-17 15:01:29 -0700177 receiveLen[0] = mLastClientHeader.bytes;
178 usrID[0] = mLastClientHeader.userID;
179 if (bufferLen < mLastClientHeader.bytes) {
180 return RS_MESSAGE_TO_CLIENT_RESIZE;
181 }
Jason Sams4c2e4c82012-02-07 15:32:08 -0800182 if (receiveLen[0]) {
183 mToClient.read(data, receiveLen[0]);
Jason Samsedbfabd2011-05-17 15:01:29 -0700184 }
Jason Sams4c2e4c82012-02-07 15:32:08 -0800185 //ALOGE("getClientPayload x");
186 return (RsMessageToClientType)mLastClientHeader.cmdID;
Jason Samsedbfabd2011-05-17 15:01:29 -0700187}
188
189bool ThreadIO::sendToClient(RsMessageToClientType cmdID, uint32_t usrID, const void *data,
190 size_t dataLen, bool waitForSpace) {
Jason Sams4c2e4c82012-02-07 15:32:08 -0800191
192 //ALOGE("sendToClient %i %i %i", cmdID, usrID, (int)dataLen);
Jason Samsedbfabd2011-05-17 15:01:29 -0700193 ClientCmdHeader hdr;
194 hdr.bytes = dataLen;
195 hdr.cmdID = cmdID;
196 hdr.userID = usrID;
Jason Samsedbfabd2011-05-17 15:01:29 -0700197
Jason Sams4c2e4c82012-02-07 15:32:08 -0800198 mToClient.writeAsync(&hdr, sizeof(hdr));
199 if (dataLen) {
200 mToClient.writeAsync(data, dataLen);
Jason Samsedbfabd2011-05-17 15:01:29 -0700201 }
Jason Sams4c2e4c82012-02-07 15:32:08 -0800202
203 //ALOGE("sendToClient x");
204 return true;
Jason Samsedbfabd2011-05-17 15:01:29 -0700205}
Jason Samsd19f10d2009-05-22 14:03:28 -0700206