Looper add get/set ForThread

This commit is contained in:
houzh 2023-11-11 15:35:01 +08:00
parent 48b02123c3
commit c023361b1f
5 changed files with 51 additions and 10 deletions

View File

@ -60,7 +60,7 @@ App::App(int argc,const char*argv[],const std::vector<CLA::Argument>&extoptions)
std::cout<<"params.count="<<getParamCount()<<std::endl;
exit(0);
}
Looper::prepare(0);
Looper::prepare(false);
Choreographer & chograph = Choreographer::getInstance();
chograph.setFrameDelay(getArgAsInt("framedelay",chograph.getFrameDelay()));
WindowManager::getInstance().setDisplayRotation((getArgAsInt("rotate",0)/90)%4);

View File

@ -26,6 +26,8 @@ namespace cdroid{
static constexpr int EPOLL_SIZE_HINT = 8;
// Maximum number of file descriptors for which to retrieve poll events each iteration.
static constexpr int EPOLL_MAX_EVENTS = 16;
static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT;
static pthread_key_t gTLSKey = 0;
static int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime){
nsecs_t timeoutDelayMillis;
@ -72,13 +74,47 @@ Looper::~Looper() {
delete hdl;
}mEventHandlers.clear();
}
Looper*Looper::getDefault(){
static thread_local Looper*mInst=nullptr;
if(mInst==nullptr){
mInst=new Looper(false);
//LOGI("*Looper::getDefault=%p",mInst);
void Looper::initTLSKey() {
int error = pthread_key_create(&gTLSKey, threadDestructor);
LOGE_IF(error != 0, "Could not allocate TLS key: %s", strerror(error));
}
void Looper::threadDestructor(void *st) {
Looper* const self = static_cast<Looper*>(st);
if (self != nullptr) {
//self->decStrong((void*)threadDestructor);
delete self;
}
return mInst;
}
Looper*Looper::getDefault(){
return getForThread();
}
Looper*Looper::prepare(int opts){
Looper* looper = getForThread();
const bool allowNonCallbacks = opts & PREPARE_ALLOW_NON_CALLBACKS;
if(looper==nullptr){
looper = new Looper(allowNonCallbacks);
setForThread(looper);
}
LOGW_IF(looper->getAllowNonCallbacks()!=allowNonCallbacks,"Looper already prepared for this thread with a different"
" value for the LOOPER_PREPARE_ALLOW_NON_CALLBACKS option.");
return looper;
}
void Looper::setForThread(Looper* looper){
Looper*old = getForThread();
delete old;
pthread_setspecific(gTLSKey,looper);
}
Looper*Looper::getForThread(){
int result = pthread_once(&gTLSOnce,initTLSKey);
LOGW_IF(result!=0,"pthread_once failed");
Looper*looper =(Looper*)pthread_getspecific(gTLSKey);
return looper;
}
bool Looper::getAllowNonCallbacks() const {

View File

@ -134,6 +134,8 @@ private:
void scheduleEpollRebuildLocked();
void doIdleHandlers();
void removeEventHandlers();
static void initTLSKey();
static void threadDestructor(void*);
protected:
public:
enum {
@ -157,6 +159,9 @@ public:
Looper(bool allowNonCallbacks=false);
virtual ~Looper();
static Looper*getDefault();
static Looper*prepare(int opts);
static void setForThread(Looper* looper);
static Looper* getForThread();
bool getAllowNonCallbacks() const;
int pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
inline int pollOnce(int timeoutMillis) {

View File

@ -92,10 +92,10 @@ void ImageView::resolveUri(){
setImageBitmap(bitmap);
loaded ++;
}
}
}else updateDrawable(d);
LOGW_IF((loaded==0)&&bNotNull,"Unable to find resource: %s",mResource.c_str());
}
updateDrawable(d);
//updateDrawable(d);
}
int ImageView::resolveAdjustedSize(int desiredSize, int maxSize,int measureSpec){

View File

@ -61,7 +61,6 @@ INT GFXInit() {
if(x11Display)return E_OK;
XInitThreads();
x11Display=XOpenDisplay(NULL);
LOGI("x11Display init =%p",x11Display);
if(x11Display) {
pthread_t tid;
XSetWindowAttributes winattrs;
@ -76,6 +75,7 @@ INT GFXInit() {
x11Window=XCreateSimpleWindow(x11Display, RootWindow(x11Display, screen), 0, 0,width,height,
1, BlackPixel(x11Display, screen), WhitePixel(x11Display, screen));
LOGI("x11Display init =%p screenSize=%dx%d",x11Display,width,height);
sizehints.flags = PMinSize | PMaxSize;
sizehints.min_width = width;
sizehints.max_width = width;