OSDN Git Service

Merge "libalsautils: moved to vendor partition" am: f03c1c09ed am: 1662e1e5c9 am...
authorIsaac Chen <ycchen@google.com>
Fri, 12 May 2017 10:29:57 +0000 (10:29 +0000)
committerandroid-build-merger <android-build-merger@google.com>
Fri, 12 May 2017 10:29:57 +0000 (10:29 +0000)
am: e2d4cdb3d5

Change-Id: Ibf1a24da6cdeaf028195bd4a094acf03d2d283dc

audio_route/audio_route.c
audio_utils/Android.bp
audio_utils/fifo_index.cpp
audio_utils/fifo_writer32.cpp [new file with mode: 0644]
audio_utils/include/audio_utils/fifo.h
audio_utils/include/audio_utils/fifo_writer32.h [new file with mode: 0644]

index 89b076b..123e7c6 100644 (file)
@@ -378,6 +378,7 @@ static int path_apply(struct audio_route *ar, struct mixer_path *path)
     struct mixer_ctl *ctl;
     enum mixer_ctl_type type;
 
+    ALOGD("Apply path: %s", path->name != NULL ? path->name : "none");
     for (i = 0; i < path->length; i++) {
         ctl_index = path->setting[i].ctl_index;
         ctl = index_to_ctl(ar, ctl_index);
@@ -399,6 +400,7 @@ static int path_reset(struct audio_route *ar, struct mixer_path *path)
     struct mixer_ctl *ctl;
     enum mixer_ctl_type type;
 
+    ALOGV("Reset path: %s", path->name != NULL ? path->name : "none");
     for (i = 0; i < path->length; i++) {
         ctl_index = path->setting[i].ctl_index;
         ctl = index_to_ctl(ar, ctl_index);
index 31d25df..c6999e5 100644 (file)
@@ -22,6 +22,7 @@ cc_library {
         "ErrorLog.cpp",
         "fifo.cpp",
         "fifo_index.cpp",
+        "fifo_writer32.cpp",
         "format.c",
         "limiter.c",
         "minifloat.c",
index d9a200a..ebb085d 100644 (file)
@@ -27,6 +27,7 @@ uint32_t audio_utils_fifo_index::loadAcquire()
     return atomic_load_explicit(&mIndex, std::memory_order_acquire);
 }
 
+// FIXME should inline this, so that writer32 can also inline it
 void audio_utils_fifo_index::storeRelease(uint32_t value)
 {
     atomic_store_explicit(&mIndex, value, std::memory_order_release);
diff --git a/audio_utils/fifo_writer32.cpp b/audio_utils/fifo_writer32.cpp
new file mode 100644 (file)
index 0000000..7551e67
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <atomic>
+#include <stdlib.h>
+#include <string.h>
+
+// TODO templatize int32_t
+
+#include <audio_utils/fifo_writer32.h>
+
+static inline void memcpyWords(int32_t *dst, const int32_t *src, uint32_t count)
+{
+    switch (count) {
+    case 0: break;
+// TODO templatize here also, but first confirm no performance regression compared to current
+#define _(n) \
+    case n: { \
+        struct s##n { int32_t a[n]; }; \
+        *(struct s##n *)dst = *(const struct s##n *)src; \
+        break; \
+    }
+    _(1) _(2) _(3) _(4) _(5) _(6) _(7) _(8) _(9) _(10) _(11) _(12) _(13) _(14) _(15) _(16)
+#undef _
+    default:
+        memcpy(dst, src, count * sizeof(int32_t));
+        break;
+    }
+}
+
+audio_utils_fifo_writer32::audio_utils_fifo_writer32(audio_utils_fifo& fifo) :
+    mLocalRear(0), mFrameCountP2(fifo.mFrameCountP2), mBuffer((int32_t *) fifo.mBuffer),
+    mWriterRear(fifo.mWriterRear)
+{
+    if (fifo.mFrameSize != sizeof(int32_t) || fifo.mFudgeFactor != 0 ||
+            ((size_t) mBuffer & ((sizeof(int32_t) - 1))) != 0) {
+        abort();
+    }
+}
+
+audio_utils_fifo_writer32::~audio_utils_fifo_writer32()
+{
+}
+
+void audio_utils_fifo_writer32::write(const int32_t *buffer, uint32_t count)
+        __attribute__((no_sanitize("integer")))     // mLocalRear += can wrap
+{
+    uint32_t availToWrite = mFrameCountP2;
+    if (availToWrite > count) {
+        availToWrite = count;
+    }
+    uint32_t rearOffset = mLocalRear & (mFrameCountP2 - 1);
+    uint32_t part1 = mFrameCountP2 - rearOffset;
+    if (part1 >  availToWrite) {
+        part1 = availToWrite;
+    }
+    memcpyWords(&mBuffer[rearOffset], buffer, part1);
+    // TODO apply this simplification to other copies of the code
+    uint32_t part2 = availToWrite - part1;
+    memcpyWords(&mBuffer[0], &buffer[part1], part2);
+    mLocalRear += availToWrite;
+}
index 14b50da..e933f9b 100644 (file)
@@ -140,6 +140,7 @@ class audio_utils_fifo : public audio_utils_fifo_base {
 
     friend class audio_utils_fifo_reader;
     friend class audio_utils_fifo_writer;
+    friend class audio_utils_fifo_writer32;
 
 public:
 
@@ -253,6 +254,9 @@ public:
      *
      * \return Actual number of frames available, if greater than or equal to zero.
      *         Guaranteed to be <= \p count and == iovec[0].mLength + iovec[1].mLength.
+     *         For a reader this is also guaranteed to be <= capacity.
+     *         For a writer this is also guaranteed to be <= effective buffer size,
+     *         even if there is no reader that throttles writer.
      *
      *  \retval -EIO        corrupted indices, no recovery is possible
      *  \retval -EOVERFLOW  reader doesn't throttle writer, and frames were lost because reader
@@ -361,6 +365,8 @@ public:
      *
      * \return Actual number of frames written, if greater than or equal to zero.
      *         Guaranteed to be <= \p count.
+     *         Also guaranteed to be <= effective buffer size,
+     *         even if there is no reader that throttles writer.
      *         The actual transfer count may be zero if the FIFO is full,
      *         or partial if the FIFO was almost full.
      *  \retval -EIO       corrupted indices, no recovery is possible
@@ -485,6 +491,7 @@ public:
      *
      * \return Actual number of frames read, if greater than or equal to zero.
      *         Guaranteed to be <= \p count.
+     *         Also guaranteed to be <= capacity.
      *         The actual transfer count may be zero if the FIFO is empty,
      *         or partial if the FIFO was almost empty.
      *  \retval -EIO        corrupted indices, no recovery is possible
diff --git a/audio_utils/include/audio_utils/fifo_writer32.h b/audio_utils/include/audio_utils/fifo_writer32.h
new file mode 100644 (file)
index 0000000..d17d58d
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_FIFO_WRITER32_H
+#define ANDROID_AUDIO_FIFO_WRITER32_H
+
+#include <audio_utils/fifo.h>
+
+/**
+ * Optimized FIFO writer for 32-bit words.
+ *
+ * Has these restrictions compared to the ordinary FIFO writer:
+ *  - buffer must be aligned on a 32-bit boundary
+ *  - frame size must be sizeof(int32_t)
+ *  - capacity must be power-of-2
+ *  - effective size must be equal to capacity
+ *  - no support for throttling of writer by one reader, and thus no blocking writes
+ *  - does not implement the provider interface
+ *  - does not implement the ordinary writer interface
+ *  - does not unblock a reader
+ *  - return value from write methods is void
+ *  - no implied store-release; must be done explicitly
+ *  - may not be combined with ordinary writer
+ *
+ * Usage:
+ *  - construct an ordinary FIFO that follows the restrictions above
+ *  - construct an ordinary reader based on that FIFO
+ *  - construct a writer32 using the FIFO
+ *  - use a sequence of write and write1, followed by storeRelease to commit
+ */
+class audio_utils_fifo_writer32 /* : public audio_utils_fifo_provider */ {
+
+public:
+    /**
+     * Construct a writer32 from a FIFO.
+     */
+    explicit audio_utils_fifo_writer32(audio_utils_fifo& fifo);
+    /*virtual*/ ~audio_utils_fifo_writer32();
+
+    /**
+     * Write an array of int32_t to FIFO.
+     * If count is larger than capacity, then only the initial 'capacity' frames will be written.
+     * TODO Instead of a silent truncation, consider adding a size_t or ssize_t return value
+     * to indicate the actual transfer count.
+     */
+    void write(const int32_t *buffer, uint32_t count /* FIXME size_t in writer */);
+
+    /**
+     * Write one int32_t value to FIFO.
+     */
+    void write1(const int32_t value)
+            __attribute__((no_sanitize("integer")))     // mLocalRear ++ can wrap
+    {
+        mBuffer[mLocalRear++ & (mFrameCountP2 - 1)] = value;
+    }
+
+    /**
+     * Commit all previous write and write1 so that they are observable by reader(s).
+     */
+    void storeRelease() {
+        mWriterRear.storeRelease(mLocalRear);
+    }
+
+private:
+    // Accessed by writer only using ordinary operations
+    uint32_t    mLocalRear; // frame index of next frame slot available to write, or write index
+
+    // These fields are copied from fifo for better performance (avoids an extra de-reference)
+    const uint32_t                     mFrameCountP2;
+    int32_t                    * const mBuffer;
+    audio_utils_fifo_index&            mWriterRear;
+};
+
+#endif // ANDROID_AUDIO_FIFO_WRITER32_H