blob: 7023a1ff2863dfea2ce574b0b66e5800313023b3 [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 Sakhartchouk1bfccea2011-07-13 17:32:05 -070024LocklessCommandFifo::LocklessCommandFifo() : mBuffer(0) {
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 }
Alex Sakhartchouk1bfccea2011-07-13 17:32:05 -070031 if (mBuffer) {
32 free(mBuffer);
33 }
Jason Samsf5b45962009-08-25 14:49:07 -070034}
35
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080036void LocklessCommandFifo::shutdown() {
Jason Samsf5b45962009-08-25 14:49:07 -070037 mInShutdown = true;
38 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -070039}
40
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080041bool LocklessCommandFifo::init(uint32_t sizeInBytes) {
Jason Samsd19f10d2009-05-22 14:03:28 -070042 // Add room for a buffer reset command
43 mBuffer = static_cast<uint8_t *>(malloc(sizeInBytes + 4));
44 if (!mBuffer) {
45 LOGE("LocklessFifo allocation failure");
46 return false;
47 }
48
Jason Sams5f7fc272009-06-18 16:58:42 -070049 if (!mSignalToControl.init() || !mSignalToWorker.init()) {
50 LOGE("Signal setup failed");
Jason Samsd19f10d2009-05-22 14:03:28 -070051 free(mBuffer);
52 return false;
53 }
54
Jason Samsf5b45962009-08-25 14:49:07 -070055 mInShutdown = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070056 mSize = sizeInBytes;
57 mPut = mBuffer;
58 mGet = mBuffer;
59 mEnd = mBuffer + (sizeInBytes) - 1;
Jason Sams3bc47d42009-11-12 15:10:25 -080060 //dumpState("init");
Jason Samsd19f10d2009-05-22 14:03:28 -070061 return true;
62}
63
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080064uint32_t LocklessCommandFifo::getFreeSpace() const {
Jason Samsd19f10d2009-05-22 14:03:28 -070065 int32_t freeSpace = 0;
66 //dumpState("getFreeSpace");
67
68 if (mPut >= mGet) {
69 freeSpace = mEnd - mPut;
70 } else {
71 freeSpace = mGet - mPut;
72 }
73
74 if (freeSpace < 0) {
75 freeSpace = 0;
76 }
Jason Samsd19f10d2009-05-22 14:03:28 -070077 return freeSpace;
78}
79
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080080bool LocklessCommandFifo::isEmpty() const {
Jason Samsfcecc242011-01-24 17:33:21 -080081 uint32_t p = android_atomic_acquire_load((int32_t *)&mPut);
82 return ((uint8_t *)p) == mGet;
Jason Samsd19f10d2009-05-22 14:03:28 -070083}
84
85
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080086void * LocklessCommandFifo::reserve(uint32_t sizeInBytes) {
Jason Samse2ae85f2009-06-03 16:04:54 -070087 // Add space for command header and loop token;
88 sizeInBytes += 8;
Jason Samsd19f10d2009-05-22 14:03:28 -070089
90 //dumpState("reserve");
91 if (getFreeSpace() < sizeInBytes) {
92 makeSpace(sizeInBytes);
93 }
94
95 return mPut + 4;
96}
97
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080098void LocklessCommandFifo::commit(uint32_t command, uint32_t sizeInBytes) {
Jason Sams516c3192009-10-06 13:58:47 -070099 if (mInShutdown) {
100 return;
101 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700102 //dumpState("commit 1");
103 reinterpret_cast<uint16_t *>(mPut)[0] = command;
104 reinterpret_cast<uint16_t *>(mPut)[1] = sizeInBytes;
Jason Sams5012a8f2011-01-27 18:07:06 -0800105
106 int32_t s = ((sizeInBytes + 3) & ~3) + 4;
107 android_atomic_add(s, (int32_t *)&mPut);
Jason Samsd19f10d2009-05-22 14:03:28 -0700108 //dumpState("commit 2");
Jason Sams5f7fc272009-06-18 16:58:42 -0700109 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -0700110}
111
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800112void LocklessCommandFifo::commitSync(uint32_t command, uint32_t sizeInBytes) {
Jason Sams516c3192009-10-06 13:58:47 -0700113 if (mInShutdown) {
114 return;
115 }
Jason Sams3b9c52a2010-10-14 17:48:46 -0700116
117 //char buf[1024];
118 //sprintf(buf, "RenderScript LocklessCommandFifo::commitSync %p %i %i", this, command, sizeInBytes);
119 //StopWatch compileTimer(buf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700120 commit(command, sizeInBytes);
121 flush();
122}
123
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800124void LocklessCommandFifo::flush() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700125 //dumpState("flush 1");
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800126 while (mPut != mGet) {
Jason Sams5f7fc272009-06-18 16:58:42 -0700127 mSignalToControl.wait();
Jason Samsd19f10d2009-05-22 14:03:28 -0700128 }
129 //dumpState("flush 2");
130}
131
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800132void LocklessCommandFifo::wait() {
133 while (isEmpty() && !mInShutdown) {
Jason Samsc1d726c2010-03-18 11:39:44 -0700134 mSignalToControl.set();
135 mSignalToWorker.wait();
136 }
137}
138
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800139const void * LocklessCommandFifo::get(uint32_t *command, uint32_t *bytesData) {
140 while (1) {
Jason Sams5f7fc272009-06-18 16:58:42 -0700141 //dumpState("get");
Jason Samsc1d726c2010-03-18 11:39:44 -0700142 wait();
Jason Samsa9e7a052009-09-25 14:51:22 -0700143 if (mInShutdown) {
144 *command = 0;
145 *bytesData = 0;
146 return 0;
147 }
148
Jason Samsd19f10d2009-05-22 14:03:28 -0700149 *command = reinterpret_cast<const uint16_t *>(mGet)[0];
150 *bytesData = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Samsd19f10d2009-05-22 14:03:28 -0700151 if (*command) {
152 // non-zero command is valid
153 return mGet+4;
154 }
Jason Samsf5b45962009-08-25 14:49:07 -0700155
Jason Samsd19f10d2009-05-22 14:03:28 -0700156 // zero command means reset to beginning.
157 mGet = mBuffer;
158 }
159}
160
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800161void LocklessCommandFifo::next() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700162 uint32_t bytes = reinterpret_cast<const uint16_t *>(mGet)[1];
Jason Samsfcecc242011-01-24 17:33:21 -0800163
164 android_atomic_add(((bytes + 3) & ~3) + 4, (int32_t *)&mGet);
165 //mGet += ((bytes + 3) & ~3) + 4;
Jason Sams5f7fc272009-06-18 16:58:42 -0700166 if (isEmpty()) {
167 mSignalToControl.set();
168 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700169 //dumpState("next");
170}
171
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800172bool LocklessCommandFifo::makeSpaceNonBlocking(uint32_t bytes) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700173 //dumpState("make space non-blocking");
174 if ((mPut+bytes) > mEnd) {
175 // Need to loop regardless of where get is.
Bryan Mawhinney8ea061b2010-12-05 17:21:07 +0000176 if ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700177 return false;
178 }
179
180 // Toss in a reset then the normal wait for space will do the rest.
181 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
182 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
183 mPut = mBuffer;
184 mSignalToWorker.set();
185 }
186
187 // it will fit here so we just need to wait for space.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800188 if (getFreeSpace() < bytes) {
Jason Samsd0cb1062010-08-18 12:38:03 -0700189 return false;
190 }
191
192 return true;
193}
194
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800195void LocklessCommandFifo::makeSpace(uint32_t bytes) {
Jason Samse2ae85f2009-06-03 16:04:54 -0700196 //dumpState("make space");
Jason Samsd19f10d2009-05-22 14:03:28 -0700197 if ((mPut+bytes) > mEnd) {
198 // Need to loop regardless of where get is.
Bryan Mawhinney8ea061b2010-12-05 17:21:07 +0000199 while ((mGet > mPut) || (mBuffer+4 >= mGet)) {
Jason Samse60446b2009-09-24 14:55:38 -0700200 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700201 }
202
203 // Toss in a reset then the normal wait for space will do the rest.
204 reinterpret_cast<uint16_t *>(mPut)[0] = 0;
205 reinterpret_cast<uint16_t *>(mPut)[1] = 0;
Jason Samse2ae85f2009-06-03 16:04:54 -0700206 mPut = mBuffer;
Jason Samsd0cb1062010-08-18 12:38:03 -0700207 mSignalToWorker.set();
Jason Samsd19f10d2009-05-22 14:03:28 -0700208 }
209
210 // it will fit here so we just need to wait for space.
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800211 while (getFreeSpace() < bytes) {
Jason Samse60446b2009-09-24 14:55:38 -0700212 usleep(100);
Jason Samsd19f10d2009-05-22 14:03:28 -0700213 }
Jason Samsf5b45962009-08-25 14:49:07 -0700214
Jason Samsd19f10d2009-05-22 14:03:28 -0700215}
216
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800217void LocklessCommandFifo::dumpState(const char *s) const {
Jason Samsd0cb1062010-08-18 12:38:03 -0700218 LOGV("%s %p put %p, get %p, buf %p, end %p", s, this, mPut, mGet, mBuffer, mEnd);
Jason Samsd19f10d2009-05-22 14:03:28 -0700219}
Jason Samsd1ac9812011-01-18 18:12:26 -0800220
221void LocklessCommandFifo::printDebugData() const {
222 dumpState("printing fifo debug");
223 const uint32_t *pptr = (const uint32_t *)mGet;
224 pptr -= 8 * 4;
225 if (mGet < mBuffer) {
226 pptr = (const uint32_t *)mBuffer;
227 }
228
229
230 for (int ct=0; ct < 16; ct++) {
231 LOGV("fifo %p = 0x%08x 0x%08x 0x%08x 0x%08x", pptr, pptr[0], pptr[1], pptr[2], pptr[3]);
232 pptr += 4;
233 }
234
235}