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
This commit is contained in:
Mathias Agopian 2013-08-19 17:26:18 -07:00
parent 8b308ed70e
commit 4f4f094348
6 changed files with 48 additions and 28 deletions

View File

@ -75,11 +75,14 @@ include $(BUILD_SHARED_LIBRARY)
# build surfaceflinger's executable # build surfaceflinger's executable
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_CFLAGS:= -DLOG_TAG=\"SurfaceFlinger\"
LOCAL_SRC_FILES:= \ LOCAL_SRC_FILES:= \
main_surfaceflinger.cpp main_surfaceflinger.cpp
LOCAL_SHARED_LIBRARIES := \ LOCAL_SHARED_LIBRARIES := \
libsurfaceflinger \ libsurfaceflinger \
liblog \
libbinder \ libbinder \
libutils libutils

View File

@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <binder/PermissionCache.h> #include <binder/PermissionCache.h>
#include <binder/IPCThreadState.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>

View File

@ -26,6 +26,7 @@
#include <EGL/eglext.h> #include <EGL/eglext.h>
#include <utils/Mutex.h> #include <utils/Mutex.h>
#include <utils/String8.h>
#include <utils/Timers.h> #include <utils/Timers.h>
#include <hardware/hwcomposer_defs.h> #include <hardware/hwcomposer_defs.h>

View File

@ -22,6 +22,10 @@
#include <math.h> #include <math.h>
#include <dlfcn.h> #include <dlfcn.h>
#if defined(HAVE_PTHREADS)
#include <sys/resource.h>
#endif
#include <EGL/egl.h> #include <EGL/egl.h>
#include <cutils/log.h> #include <cutils/log.h>
@ -70,7 +74,6 @@
#include "RenderEngine/RenderEngine.h" #include "RenderEngine/RenderEngine.h"
#define DISPLAY_COUNT 1 #define DISPLAY_COUNT 1
/* /*
@ -92,7 +95,7 @@ const String16 sDump("android.permission.DUMP");
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
SurfaceFlinger::SurfaceFlinger() SurfaceFlinger::SurfaceFlinger()
: BnSurfaceComposer(), Thread(false), : BnSurfaceComposer(),
mTransactionFlags(0), mTransactionFlags(0),
mTransactionPending(false), mTransactionPending(false),
mAnimTransactionPending(false), mAnimTransactionPending(false),
@ -139,14 +142,8 @@ SurfaceFlinger::SurfaceFlinger()
void SurfaceFlinger::onFirstRef() void SurfaceFlinger::onFirstRef()
{ {
mEventQueue.init(this); mEventQueue.init(this);
run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
// Wait for the main thread to be done with its initialization
mReadyToRunBarrier.wait();
} }
SurfaceFlinger::~SurfaceFlinger() SurfaceFlinger::~SurfaceFlinger()
{ {
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
@ -414,9 +411,8 @@ success:
return config; return config;
} }
void SurfaceFlinger::init() {
status_t SurfaceFlinger::readyToRun()
{
ALOGI( "SurfaceFlinger's main thread ready to run. " ALOGI( "SurfaceFlinger's main thread ready to run. "
"Initializing graphics H/W..."); "Initializing graphics H/W...");
@ -498,16 +494,11 @@ status_t SurfaceFlinger::readyToRun()
// initialize our drawing state // initialize our drawing state
mDrawingState = mCurrentState; mDrawingState = mCurrentState;
// We're now ready to accept clients...
mReadyToRunBarrier.open();
// set initial conditions (e.g. unblank default device) // set initial conditions (e.g. unblank default device)
initializeDisplays(); initializeDisplays();
// start boot animation // start boot animation
startBootAnim(); startBootAnim();
return NO_ERROR;
} }
int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) { int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
@ -648,9 +639,13 @@ status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
return res; return res;
} }
bool SurfaceFlinger::threadLoop() { void SurfaceFlinger::run() {
waitForEvent(); #if defined(HAVE_PTHREADS)
return true; setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
#endif
do {
waitForEvent();
} while (true);
} }
void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) { void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {

View File

@ -35,7 +35,6 @@
#include <utils/SortedVector.h> #include <utils/SortedVector.h>
#include <utils/threads.h> #include <utils/threads.h>
#include <binder/BinderService.h>
#include <binder/IMemory.h> #include <binder/IMemory.h>
#include <ui/PixelFormat.h> #include <ui/PixelFormat.h>
@ -76,10 +75,8 @@ enum {
eTransactionMask = 0x07 eTransactionMask = 0x07
}; };
class SurfaceFlinger : public BinderService<SurfaceFlinger>, class SurfaceFlinger : public BnSurfaceComposer,
public BnSurfaceComposer,
private IBinder::DeathRecipient, private IBinder::DeathRecipient,
private Thread,
private HWComposer::EventHandler private HWComposer::EventHandler
{ {
public: public:
@ -89,6 +86,12 @@ public:
SurfaceFlinger() ANDROID_API; 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 { enum {
EVENT_VSYNC = HWC_EVENT_VSYNC EVENT_VSYNC = HWC_EVENT_VSYNC
}; };
@ -209,10 +212,8 @@ private:
virtual void binderDied(const wp<IBinder>& who); virtual void binderDied(const wp<IBinder>& who);
/* ------------------------------------------------------------------------ /* ------------------------------------------------------------------------
* Thread interface * RefBase interface
*/ */
virtual bool threadLoop();
virtual status_t readyToRun();
virtual void onFirstRef(); virtual void onFirstRef();
/* ------------------------------------------------------------------------ /* ------------------------------------------------------------------------
@ -447,7 +448,6 @@ private:
// these are thread safe // these are thread safe
mutable MessageQueue mEventQueue; mutable MessageQueue mEventQueue;
mutable Barrier mReadyToRunBarrier;
FrameTracker mAnimFrameTracker; FrameTracker mAnimFrameTracker;
// protected by mDestroyedLayerLock; // protected by mDestroyedLayerLock;

View File

@ -14,7 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
#include <binder/BinderService.h> #include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include "SurfaceFlinger.h" #include "SurfaceFlinger.h"
using namespace android; 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 // When SF is launched in its own process, limit the number of
// binder threads to 4. // binder threads to 4.
ProcessState::self()->setThreadPoolMaxThreadCount(4); ProcessState::self()->setThreadPoolMaxThreadCount(4);
SurfaceFlinger::publishAndJoinThreadPool(true);
// instantiate surfaceflinger
sp<SurfaceFlinger> flinger = new SurfaceFlinger();
// initialize before clients can connect
flinger->init();
// start the thread pool
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
// publish surface flinger
sp<IServiceManager> sm(defaultServiceManager());
sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);
// run in this thread
flinger->run();
return 0; return 0;
} }