Use scheduling policy service

Change-Id: I3c09da1dc0de5039d0c15ce7fb2bc373fa398712
diff --git a/services/audioflinger/Android.mk b/services/audioflinger/Android.mk
index 4fbc67d..48b35cf 100644
--- a/services/audioflinger/Android.mk
+++ b/services/audioflinger/Android.mk
@@ -3,6 +3,17 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
+    ISchedulingPolicyService.cpp \
+    SchedulingPolicyService.cpp
+
+# FIXME Move this library to frameworks/native
+LOCAL_MODULE := libscheduling_policy
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
     AudioBufferProviderSource.cpp   \
     AudioStreamOutSink.cpp          \
     AudioStreamInSource.cpp         \
@@ -56,6 +67,7 @@
     libpowermanager
 
 LOCAL_STATIC_LIBRARIES := \
+    libscheduling_policy \
     libnbaio \
     libcpustats \
     libmedia_helper
@@ -68,6 +80,6 @@
 
 LOCAL_CFLAGS += -DSTATE_QUEUE_INSTANTIATIONS='"StateQueueInstantiations.cpp"'
 
-LOCAL_CFLAGS += -UHAVE_REQUEST_PRIORITY -UFAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE -USOAKER
+LOCAL_CFLAGS += -DHAVE_REQUEST_PRIORITY -UFAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE -USOAKER
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index de60942..6eeda9a 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -80,6 +80,10 @@
 #include "MonoPipeReader.h"
 #include "SourceAudioBufferProvider.h"
 
+#ifdef HAVE_REQUEST_PRIORITY
+#include "SchedulingPolicyService.h"
+#endif
+
 #ifdef SOAKER
 #include "Soaker.h"
 #endif
diff --git a/services/audioflinger/ISchedulingPolicyService.cpp b/services/audioflinger/ISchedulingPolicyService.cpp
new file mode 100644
index 0000000..909b77e
--- /dev/null
+++ b/services/audioflinger/ISchedulingPolicyService.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#define LOG_TAG "SchedulingPolicyService"
+//#define LOG_NDEBUG 0
+
+#include <binder/Parcel.h>
+#include "ISchedulingPolicyService.h"
+
+namespace android {
+
+// Keep in sync with frameworks/base/core/java/android/os/ISchedulingPolicyService.aidl
+enum {
+    REQUEST_PRIORITY_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
+};
+
+// ----------------------------------------------------------------------
+
+class BpSchedulingPolicyService : public BpInterface<ISchedulingPolicyService>
+{
+public:
+    BpSchedulingPolicyService(const sp<IBinder>& impl)
+        : BpInterface<ISchedulingPolicyService>(impl)
+    {
+    }
+
+    virtual int requestPriority(int32_t pid, int32_t tid, int32_t prio)
+    {
+        Parcel data, reply;
+        data.writeInterfaceToken(ISchedulingPolicyService::getInterfaceDescriptor());
+        data.writeInt32(pid);
+        data.writeInt32(tid);
+        data.writeInt32(prio);
+        remote()->transact(REQUEST_PRIORITY_TRANSACTION, data, &reply);
+        // fail on exception
+        if (reply.readExceptionCode() != 0) return -1;
+        return reply.readInt32();
+    }
+};
+
+IMPLEMENT_META_INTERFACE(SchedulingPolicyService, "android.os.ISchedulingPolicyService");
+
+// ----------------------------------------------------------------------
+
+status_t BnSchedulingPolicyService::onTransact(
+    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
+{
+    switch (code) {
+    case REQUEST_PRIORITY_TRANSACTION:
+        // Not reached
+        return NO_ERROR;
+        break;
+    default:
+        return BBinder::onTransact(code, data, reply, flags);
+    }
+}
+
+}   // namespace android
diff --git a/services/audioflinger/ISchedulingPolicyService.h b/services/audioflinger/ISchedulingPolicyService.h
new file mode 100644
index 0000000..a38e67e
--- /dev/null
+++ b/services/audioflinger/ISchedulingPolicyService.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2012 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_ISCHEDULING_POLICY_SERVICE_H
+#define ANDROID_ISCHEDULING_POLICY_SERVICE_H
+
+#include <binder/IInterface.h>
+
+namespace android {
+
+class ISchedulingPolicyService : public IInterface
+{
+public:
+    DECLARE_META_INTERFACE(SchedulingPolicyService);
+
+    virtual int         requestPriority(/*pid_t*/int32_t pid, /*pid_t*/int32_t tid,
+                                                int32_t prio) = 0;
+
+};
+
+class BnSchedulingPolicyService : public BnInterface<ISchedulingPolicyService>
+{
+public:
+    virtual status_t    onTransact( uint32_t code,
+                                    const Parcel& data,
+                                    Parcel* reply,
+                                    uint32_t flags = 0);
+};
+
+}   // namespace android
+
+#endif  // ANDROID_ISCHEDULING_POLICY_SERVICE_H
diff --git a/services/audioflinger/SchedulingPolicyService.cpp b/services/audioflinger/SchedulingPolicyService.cpp
new file mode 100644
index 0000000..59cc99a
--- /dev/null
+++ b/services/audioflinger/SchedulingPolicyService.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2012 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 <binder/IServiceManager.h>
+#include <utils/Mutex.h>
+#include "ISchedulingPolicyService.h"
+#include "SchedulingPolicyService.h"
+
+namespace android {
+
+static sp<ISchedulingPolicyService> sSchedulingPolicyService;
+static const String16 _scheduling_policy("scheduling_policy");
+static Mutex sMutex;
+
+int requestPriority(pid_t pid, pid_t tid, int32_t prio)
+{
+    // FIXME merge duplicated code related to service lookup, caching, and error recovery
+    sp<ISchedulingPolicyService> sps;
+    for (;;) {
+        sMutex.lock();
+        sps = sSchedulingPolicyService;
+        sMutex.unlock();
+        if (sps != 0) {
+            break;
+        }
+        sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
+        if (binder != 0) {
+            sps = interface_cast<ISchedulingPolicyService>(binder);
+            sMutex.lock();
+            sSchedulingPolicyService = sps;
+            sMutex.unlock();
+            break;
+        }
+        sleep(1);
+    }
+    return sps->requestPriority(pid, tid, prio);
+}
+
+}   // namespace android
diff --git a/services/audioflinger/SchedulingPolicyService.h b/services/audioflinger/SchedulingPolicyService.h
new file mode 100644
index 0000000..7ac8454
--- /dev/null
+++ b/services/audioflinger/SchedulingPolicyService.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2012 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_SCHEDULING_POLICY_SERVICE_H
+#define _ANDROID_SCHEDULING_POLICY_SERVICE_H
+
+namespace android {
+
+// Request elevated priority for thread tid, whose thread group leader must be pid.
+// The priority parameter is currently restricted to either 1 or 2.
+int requestPriority(pid_t pid, pid_t tid, int32_t prio);
+
+}   // namespace android
+
+#endif  // _ANDROID_SCHEDULING_POLICY_SERVICE_H