From 4f4f0943489d9113c66ac22b58cfba8c21dfa879 Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Mon, 19 Aug 2013 17:26:18 -0700 Subject: [PATCH] SurfaceFlinger now runs in the process's main thread it used to spawn its own thread and return the main thread to the binder thread pool -- this was confusing the naming of things in the kernel. Bug: 10331839 Change-Id: I2d13a6d73409a38109300fcbe6a04b4c41cb5d00 --- services/surfaceflinger/Android.mk | 3 +++ services/surfaceflinger/Client.cpp | 1 + services/surfaceflinger/DisplayDevice.h | 1 + services/surfaceflinger/SurfaceFlinger.cpp | 31 +++++++++++-------------- services/surfaceflinger/SurfaceFlinger.h | 16 ++++++------- services/surfaceflinger/main_surfaceflinger.cpp | 24 +++++++++++++++++-- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk index 873bd5d843..81b0d14b05 100644 --- a/services/surfaceflinger/Android.mk +++ b/services/surfaceflinger/Android.mk @@ -75,11 +75,14 @@ include $(BUILD_SHARED_LIBRARY) # build surfaceflinger's executable include $(CLEAR_VARS) +LOCAL_CFLAGS:= -DLOG_TAG=\"SurfaceFlinger\" + LOCAL_SRC_FILES:= \ main_surfaceflinger.cpp LOCAL_SHARED_LIBRARIES := \ libsurfaceflinger \ + liblog \ libbinder \ libutils diff --git a/services/surfaceflinger/Client.cpp b/services/surfaceflinger/Client.cpp index dd65348ea0..975631c5e2 100644 --- a/services/surfaceflinger/Client.cpp +++ b/services/surfaceflinger/Client.cpp @@ -18,6 +18,7 @@ #include #include +#include #include diff --git a/services/surfaceflinger/DisplayDevice.h b/services/surfaceflinger/DisplayDevice.h index 8e24df757b..eec0396cbb 100644 --- a/services/surfaceflinger/DisplayDevice.h +++ b/services/surfaceflinger/DisplayDevice.h @@ -26,6 +26,7 @@ #include #include +#include #include #include diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp index dcdd1d0f93..7e62dae9df 100644 --- a/services/surfaceflinger/SurfaceFlinger.cpp +++ b/services/surfaceflinger/SurfaceFlinger.cpp @@ -22,6 +22,10 @@ #include #include +#if defined(HAVE_PTHREADS) +#include +#endif + #include #include @@ -70,7 +74,6 @@ #include "RenderEngine/RenderEngine.h" - #define DISPLAY_COUNT 1 /* @@ -92,7 +95,7 @@ const String16 sDump("android.permission.DUMP"); // --------------------------------------------------------------------------- SurfaceFlinger::SurfaceFlinger() - : BnSurfaceComposer(), Thread(false), + : BnSurfaceComposer(), mTransactionFlags(0), mTransactionPending(false), mAnimTransactionPending(false), @@ -139,14 +142,8 @@ SurfaceFlinger::SurfaceFlinger() void SurfaceFlinger::onFirstRef() { mEventQueue.init(this); - - run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); - - // Wait for the main thread to be done with its initialization - mReadyToRunBarrier.wait(); } - SurfaceFlinger::~SurfaceFlinger() { EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); @@ -414,9 +411,8 @@ success: return config; } +void SurfaceFlinger::init() { -status_t SurfaceFlinger::readyToRun() -{ ALOGI( "SurfaceFlinger's main thread ready to run. " "Initializing graphics H/W..."); @@ -498,16 +494,11 @@ status_t SurfaceFlinger::readyToRun() // initialize our drawing state mDrawingState = mCurrentState; - // We're now ready to accept clients... - mReadyToRunBarrier.open(); - // set initial conditions (e.g. unblank default device) initializeDisplays(); // start boot animation startBootAnim(); - - return NO_ERROR; } int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) { @@ -648,9 +639,13 @@ status_t SurfaceFlinger::postMessageSync(const sp& msg, return res; } -bool SurfaceFlinger::threadLoop() { - waitForEvent(); - return true; +void SurfaceFlinger::run() { +#if defined(HAVE_PTHREADS) + setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY); +#endif + do { + waitForEvent(); + } while (true); } void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) { diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h index 0906f8029a..f746e6b919 100644 --- a/services/surfaceflinger/SurfaceFlinger.h +++ b/services/surfaceflinger/SurfaceFlinger.h @@ -35,7 +35,6 @@ #include #include -#include #include #include @@ -76,10 +75,8 @@ enum { eTransactionMask = 0x07 }; -class SurfaceFlinger : public BinderService, - public BnSurfaceComposer, +class SurfaceFlinger : public BnSurfaceComposer, private IBinder::DeathRecipient, - private Thread, private HWComposer::EventHandler { public: @@ -89,6 +86,12 @@ public: SurfaceFlinger() ANDROID_API; + // must be called before clients can connect + void init() ANDROID_API; + + // starts SurfaceFlinger main loop in the current thread + void run() ANDROID_API; + enum { EVENT_VSYNC = HWC_EVENT_VSYNC }; @@ -209,10 +212,8 @@ private: virtual void binderDied(const wp& who); /* ------------------------------------------------------------------------ - * Thread interface + * RefBase interface */ - virtual bool threadLoop(); - virtual status_t readyToRun(); virtual void onFirstRef(); /* ------------------------------------------------------------------------ @@ -447,7 +448,6 @@ private: // these are thread safe mutable MessageQueue mEventQueue; - mutable Barrier mReadyToRunBarrier; FrameTracker mAnimFrameTracker; // protected by mDestroyedLayerLock; diff --git a/services/surfaceflinger/main_surfaceflinger.cpp b/services/surfaceflinger/main_surfaceflinger.cpp index 8503d4e234..a609b6fd48 100644 --- a/services/surfaceflinger/main_surfaceflinger.cpp +++ b/services/surfaceflinger/main_surfaceflinger.cpp @@ -14,7 +14,10 @@ * limitations under the License. */ -#include +#include +#include +#include +#include #include "SurfaceFlinger.h" using namespace android; @@ -23,6 +26,23 @@ int main(int argc, char** argv) { // When SF is launched in its own process, limit the number of // binder threads to 4. ProcessState::self()->setThreadPoolMaxThreadCount(4); - SurfaceFlinger::publishAndJoinThreadPool(true); + + // instantiate surfaceflinger + sp flinger = new SurfaceFlinger(); + + // initialize before clients can connect + flinger->init(); + + // start the thread pool + sp ps(ProcessState::self()); + ps->startThreadPool(); + + // publish surface flinger + sp sm(defaultServiceManager()); + sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false); + + // run in this thread + flinger->run(); + return 0; } -- 2.11.0