blob: 3f8854330acfdf33be9d489eedb87688341a2d10 [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 "rsLocklessFifo.h"
Jason Sams3b9c52a2010-10-14 17:48:46 -070018#include "utils/Timers.h"
19#include "utils/StopWatch.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070020
21using namespace android;
Jason Samsc1d726c2010-03-18 11:39:44 -070022using namespace android::renderscript;
Jason Samsd19f10d2009-05-22 14:03:28 -070023
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080024LocklessCommandFifo::LocklessCommandFifo() {
Jason Samsd19f10d2009-05-22 14:03:28 -070025}
26
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080027LocklessCommandFifo::~LocklessCommandFifo() {
Jason Samsf5b45962009-08-25 14:49:07 -070028 if (!mInShutdown) {
29 shutdown();
30 }
31 free(mBuffer);
32}
33
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080034void LocklessCommandFifo::shutdown() {
Jason Samsf5b45962009-08-25 14:49:07 -070035 mInShutdown = true;
36 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -070037}
38
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080039bool LocklessCommandFifo::init(uint32_t sizeInBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -070040 // Add room for a buffer reset command
41 mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4));
42 if (!mBuffer) {
43 LOGE("LocklessFifo allocation failure");
44 return false;
45 }
46
Jason Sams5f7fc272009-06-18 16:58:42 -070047 if (!mSignalToControl.init() || !mSignalToWorker.init()) {
48 LOGE("Signal setup failed");
Jason Samsd19f10d2009-05-22 14:03:28 -070049 free(mBuffer);
50 return false;
51 }
52
Jason Samsf5b45962009-08-25 14:49:07 -070053 mInShutdown = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070054 mSize = sizeInBytes;
55 mPut = mBuffer;
56 mGet = mBuffer;
57 mEnd = mBuffer + (sizeInBytes) - 1;
Jason Sams3bc47d42009-11-12 15:10:25 -080058 //dumpState("init");
Jason Samsd19f10d2009-05-22 14:03:28 -070059 return true;
60}
61
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080062uint32_t LocklessCommandFifo::getFreeSpace() const {
Jason Samsd19f10d2009-05-22 14:03:28 -070063 int32_t freeSpace = 0;
64 //dumpState("getFreeSpace");
65
66 if (mPut >= mGet) {
67 freeSpace = mEnd - mPut;
68 } else {
69 freeSpace = mGet - mPut;
70 }
71
72 if (freeSpace < 0) {
73 freeSpace = 0;
74 }
Jason Samsd19f10d2009-05-22 14:03:28 -070075 return freeSpace;
76}
77
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080078bool LocklessCommandFifo::isEmpty() const {
Jason Samsfcecc242011-01-24 17:33:21 -080079 uint32_t p = android_atomic_acquire_load((int32_t *)&mPut);
80 return ((uint8_t *)p) == mGet;
Jason Samsd19f10d2009-05-22 14:03:28 -070081}
82
83
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080084void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) {
Jason Samse2ae85f2009-06-03 16:04:54 -070085 // Add space for command header and loop token;
86 sizeInBytes += 8;
Jason Samsd19f10d2009-05-22 14:03:28 -070087
88 //dumpState("reserve");
89 if (getFreeSpace() < sizeInBytes) {
90 makeSpace(sizeInBytes);
91 }
92
93 return mPut + 4;
94}
95
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080096void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) {
Jason Sams516c3192009-10-06 13:58:47 -070097 if (mInShutdown) {
98 return;
99 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700100 //dumpState("commit 1");
101 reinterpret_cast<uint16_t *>(mPut)[0] = command;
102 reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes;
103 mPut += ((sizeInBytes + 3) & ~3) + 4;
104 //dumpState("commit 2");
Jason Sams5f7fc272009-06-18 16:58:42 -0700105 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) {
Jason Sams516c3192009-10-06 13:58:47 -0700109 if (mInShutdown) {
110 return;
111 }
Jason Sams3b9c52a2010-10-14 17:48:46 -0700112
113 //char buf[1024];
114 //sprintf(buf, "RenderScript LocklessCommandFifo::commitSync %p %i %i", this, command, sizeInBytes);
115 //StopWatch compileTimer(buf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 commit(command, sizeInBytes);
117 flush();
118}
119
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800120void LocklessCommandFifo::flush() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700121 //dumpState("flush 1");
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800122 while (mPut != mGet) {
Jason Sams5f7fc272009-06-18 16:58:42 -0700123 mSignalToControl.wait();
Jason Samsd19f10d2009-05-22 14:03:28 -0700124 }
125 //dumpState("flush 2");
126}
127
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800128void LocklessCommandFifo::wait() {
129 while (isEmpty() && !mInShutdown) {
Jason Samsc1d726c2010-03-18 11:39:44 -0700130 mSignalToControl.set();
131 mSignalToWorker.wait();
132 }
133}
134
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800135const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) {
136 while (1) {
Jason Sams5f7fc272009-06-18 16:58:42 -0700137 //dumpState("get");
Jason Samsc1d726c2010-03-18 11:39:44 -0700138 wait();
Jason Samsa9e7a052009-09-25 14:51:22 -0700139 if (mInShutdown) {
140 *command = 0;
141 *bytesData = 0;
142 return 0;
143 }
144
Jason Samsd19f10d2009-05-22 14:03:28 -0700145 *command = reinterpret_cast<const uint16_t *>(mGet)[0];
146 *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Samsd19f10d2009-05-22 14:03:28 -0700147 if (*command) {
148 // non-zero command is valid
149 return mGet+4;
150 }
Jason Samsf5b45962009-08-25 14:49:07 -0700151
Jason Samsd19f10d2009-05-22 14:03:28 -0700152 // zero command means reset to beginning.
153 mGet = mBuffer;
154 }
155}
156
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800157void LocklessCommandFifo::next() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700158 uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Samsfcecc242011-01-24 17:33:21 -0800159
160 android_atomic_add(((bytes + 3) & ~3) + 4, (int32_t *)&mGet);
161 //mGet += ((bytes + 3) & ~3) + 4;
Jason Sams5f7fc272009-06-18 16:58:42 -0700162 if (isEmpty()) {
163 mSignalToControl.set();
164 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700165 //dumpState("next");
166}
167
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800168bool LocklessCommandFifo::makeSpaceNonBlocking(uint32_t bytes) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700169 //dumpState("make space non-blocking");
170 if ((mPut+bytes) > mEnd) {
171 // Need to loop regardless of where get is.
Bryan Mawhinney8ea061b2010-12-05 17:21:07 +0000172 if ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700173 return false;
174 }
175
176 // Toss in a reset then the normal wait for space will do the rest.
177 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
178 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
179 mPut = mBuffer;
180 mSignalToWorker.set();
181 }
182
183 // it will fit here so we just need to wait for space.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800184 if (getFreeSpace() < bytes) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700185 return false;
186 }
187
188 return true;
189}
190
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800191void LocklessCommandFifo::makeSpace(uint32_t bytes) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700192 //dumpState("make space");
Jason Samsd19f10d2009-05-22 14:03:28 -0700193 if ((mPut+bytes) > mEnd) {
194 // Need to loop regardless of where get is.
Bryan Mawhinney8ea061b2010-12-05 17:21:07 +0000195 while ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Samse60446b2009-09-24 14:55:38 -0700196 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700197 }
198
199 // Toss in a reset then the normal wait for space will do the rest.
200 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
201 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
Jason Samse2ae85f2009-06-03 16:04:54 -0700202 mPut = mBuffer;
Jason Samsd0cb1062010-08-18 12:38:03 -0700203 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -0700204 }
205
206 // it will fit here so we just need to wait for space.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800207 while (getFreeSpace() < bytes) {
Jason Samse60446b2009-09-24 14:55:38 -0700208 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700209 }
Jason Samsf5b45962009-08-25 14:49:07 -0700210
Jason Samsd19f10d2009-05-22 14:03:28 -0700211}
212
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800213void LocklessCommandFifo::dumpState(const char *s) const {
Jason Samsd0cb1062010-08-18 12:38:03 -0700214 LOGV("%s %p put %p, get %p, buf %p, end %p", s, this, mPut, mGet, mBuffer, mEnd);
Jason Samsd19f10d2009-05-22 14:03:28 -0700215}
Jason Samsd1ac9812011-01-18 18:12:26 -0800216
217void LocklessCommandFifo::printDebugData() const {
218 dumpState("printing fifo debug");
219 const uint32_t *pptr = (const uint32_t *)mGet;
220 pptr -= 8 * 4;
221 if (mGet < mBuffer) {
222 pptr = (const uint32_t *)mBuffer;
223 }
224
225
226 for (int ct=0; ct < 16; ct++) {
227 LOGV("fifo %p = 0x%08x 0x%08x 0x%08x 0x%08x", pptr, pptr[0], pptr[1], pptr[2], pptr[3]);
228 pptr += 4;
229 }
230
231}