/* * Copyright 2003-2006, Haiku Inc. * Authors: * Stefano Ceccherini (burton666@libero.it). * Carwyn Jones (turok2@currantbun.com) * * Distributed under the terms of the MIT License. */ #include #include #include #include #include #include //#define DEBUG 1 #if DEBUG #include #include #endif // We don't need this kind of locking, since the directDaemonFunc // doesn't access critical shared data. #define DW_NEEDS_LOCKING 0 enum dw_status_bits { DW_STATUS_AREA_CLONED = 0x1, DW_STATUS_THREAD_STARTED = 0x2, DW_STATUS_SEM_CREATED = 0x4 }; #if DEBUG static void print_direct_buffer_state(const direct_buffer_state &state) { char string[128]; int modeState = state & B_DIRECT_MODE_MASK; if (modeState == B_DIRECT_START) strcpy(string, "B_DIRECT_START"); else if (modeState == B_DIRECT_MODIFY) strcpy(string, "B_DIRECT_MODIFY"); else if (modeState == B_DIRECT_STOP) strcpy(string, "B_DIRECT_STOP"); if (state & B_CLIPPING_MODIFIED) strcat(string, " | B_CLIPPING_MODIFIED"); if (state & B_BUFFER_RESIZED) strcat(string, " | B_BUFFER_RESIZED"); if (state & B_BUFFER_MOVED) strcat(string, " | B_BUFFER_MOVED"); if (state & B_BUFFER_RESET) strcat(string, " | B_BUFFER_RESET"); printf("direct_buffer_state: %s\n", string); } static void print_direct_driver_state(const direct_driver_state &state) { if (state == 0) return; char string[64]; if (state == B_DRIVER_CHANGED) strcpy(string, "B_DRIVER_CHANGED"); else if (state == B_MODE_CHANGED) strcpy(string, "B_MODE_CHANGED"); printf("direct_driver_state: %s\n", string); } static void print_direct_buffer_info(const direct_buffer_info &info) { print_direct_buffer_state(info.buffer_state); print_direct_driver_state(info.driver_state); } #endif BDirectWindow::BDirectWindow(BRect frame, const char *title, window_type type, uint32 flags, uint32 workspace) : BWindow(frame, title, type, flags, workspace) { InitData(); } BDirectWindow::BDirectWindow(BRect frame, const char *title, window_look look, window_feel feel, uint32 flags, uint32 workspace) : BWindow(frame, title, look, feel, flags, workspace) { InitData(); } BDirectWindow::~BDirectWindow() { DisposeData(); } // start of regular BWindow API BArchivable * BDirectWindow::Instantiate(BMessage *data) { return NULL; } status_t BDirectWindow::Archive(BMessage *data, bool deep) const { return inherited::Archive(data, deep); } void BDirectWindow::Quit() { inherited::Quit(); } void BDirectWindow::DispatchMessage(BMessage *message, BHandler *handler) { inherited::DispatchMessage(message, handler); } void BDirectWindow::MessageReceived(BMessage *message) { inherited::MessageReceived(message); } void BDirectWindow::FrameMoved(BPoint new_position) { inherited::FrameMoved(new_position); } void BDirectWindow::WorkspacesChanged(uint32 old_ws, uint32 new_ws) { inherited::WorkspacesChanged(old_ws, new_ws); } void BDirectWindow::WorkspaceActivated(int32 ws, bool state) { inherited::WorkspaceActivated(ws, state); } void BDirectWindow::FrameResized(float new_width, float new_height) { inherited::FrameResized(new_width, new_height); } void BDirectWindow::Minimize(bool minimize) { inherited::Minimize(minimize); } void BDirectWindow::Zoom(BPoint rec_position, float rec_width, float rec_height) { inherited::Zoom(rec_position, rec_width, rec_height); } void BDirectWindow::ScreenChanged(BRect screen_size, color_space depth) { inherited::ScreenChanged(screen_size, depth); } void BDirectWindow::MenusBeginning() { inherited::MenusBeginning(); } void BDirectWindow::MenusEnded() { inherited::MenusEnded(); } void BDirectWindow::WindowActivated(bool state) { inherited::WindowActivated(state); } void BDirectWindow::Show() { inherited::Show(); } void BDirectWindow::Hide() { inherited::Hide(); } BHandler * BDirectWindow::ResolveSpecifier(BMessage *msg, int32 index, BMessage *specifier, int32 form, const char *property) { return inherited::ResolveSpecifier(msg, index, specifier, form, property); } status_t BDirectWindow::GetSupportedSuites(BMessage *data) { return inherited::GetSupportedSuites(data); } status_t BDirectWindow::Perform(perform_code d, void *arg) { return inherited::Perform(d, arg); } void BDirectWindow::task_looper() { inherited::task_looper(); } BMessage * BDirectWindow::ConvertToMessage(void *raw, int32 code) { return inherited::ConvertToMessage(raw, code); } // #pragma mark - BDirectWindow specific API void BDirectWindow::DirectConnected(direct_buffer_info *info) { // implemented in subclasses } status_t BDirectWindow::GetClippingRegion(BRegion *region, BPoint *origin) const { if (region == NULL) return B_BAD_VALUE; if (IsLocked() || !LockDirect()) return B_ERROR; if (fInDirectConnect) { UnlockDirect(); return B_ERROR; } // BPoint's coordinates are floats. We can only work // with integers._DaemonStarter int32 originX, originY; if (origin == NULL) { originX = 0; originY = 0; } else { originX = (int32)origin->x; originY = (int32)origin->y; } #ifndef HAIKU_TARGET_PLATFORM_DANO // Since we are friend of BRegion, we can access its private members. // Otherwise, we would need to call BRegion::Include(clipping_rect) // for every clipping_rect in our clip_list, and that would be much // more overkill than this (tested ). if (!region->_SetSize(fBufferDesc->clip_list_count)) { UnlockDirect(); return B_NO_MEMORY; } region->fCount = fBufferDesc->clip_list_count; region->fBounds = region->_ConvertToInternal(fBufferDesc->clip_bounds); for (uint32 c = 0; c < fBufferDesc->clip_list_count; c++) { region->fData[c] = region->_ConvertToInternal( fBufferDesc->clip_list[c]); } // adjust bounds by the given origin point region->OffsetBy(-originX, -originY); #endif UnlockDirect(); return B_OK; } status_t BDirectWindow::SetFullScreen(bool enable) { if (fIsFullScreen == enable) return B_OK; status_t status = B_ERROR; if (Lock()) { fLink->StartMessage(AS_DIRECT_WINDOW_SET_FULLSCREEN); fLink->Attach(enable); if (fLink->FlushWithReply(status) == B_OK && status == B_OK) { fIsFullScreen = enable; } Unlock(); } return status; } bool BDirectWindow::IsFullScreen() const { return fIsFullScreen; } /*static*/ bool BDirectWindow::SupportsWindowMode(screen_id id) { /* display_mode mode; status_t status = BScreen(id).GetMode(&mode); if (status == B_OK) return mode.flags & B_PARALLEL_ACCESS; return false;*/ // TODO: Apparently, the above is false for the vesa driver, // but enabling it doesn't do any harm... maybe we should just return always true. // At least, I can't see why window mode shouldn't be supported. // additional NOTE: it probably depends on wether hardware cursor is supported or // not return true; } // #pragma mark - Private methods /* static */ int32 BDirectWindow::_DaemonStarter(void *arg) { return static_cast(arg)->DirectDaemonFunc(); } int32 BDirectWindow::DirectDaemonFunc() { while (!fDaemonKiller) { // This sem is released by the app_server when our // clipping region changes, or when our window is moved, // resized, etc. etc. status_t status; do { status = acquire_sem(fDisableSem); } while (status == B_INTERRUPTED); if (status < B_OK) return -1; #if DEBUG print_direct_buffer_info(*fBufferDesc); #endif if (LockDirect()) { if ((fBufferDesc->buffer_state & B_DIRECT_MODE_MASK) == B_DIRECT_START) fConnectionEnable = true; fInDirectConnect = true; DirectConnected(fBufferDesc); fInDirectConnect = false; if ((fBufferDesc->buffer_state & B_DIRECT_MODE_MASK) == B_DIRECT_STOP) fConnectionEnable = false; UnlockDirect(); } // The app_server then waits (with a timeout) on this sem. // If we aren't quick enough to release this sem, our app // will be terminated by the app_server if (release_sem(fDisableSemAck) != B_OK) return -1; } return 0; } // LockDirect() and UnlockDirect() are no-op on R5. I tried to call (R5's) LockDirect() // repeatedly, from the same thread and from different threads, nothing happened. // I implemented them anyway, as they were the first methods I wrote // in this class (As you can see, I even needed to cast away their constness // to make them do something useful). // They're not needed though, as the direct_daemon_thread doesn't change // any shared data. They are probably here for future enhancements (see also the // comment in DriverSetup() bool BDirectWindow::LockDirect() const { status_t status = B_OK; #if DW_NEEDS_LOCKING BDirectWindow *casted = const_cast(this); if (atomic_add(&casted->fDirectLock, 1) > 0) { do { status = acquire_sem(fDirectSem); } while (status == B_INTERRUPTED); } if (status == B_OK) { casted->fDirectLockOwner = find_thread(NULL); casted->fDirectLockCount++; } #endif return status == B_OK; } void BDirectWindow::UnlockDirect() const { #if DW_NEEDS_LOCKING BDirectWindow *casted = const_cast(this); if (atomic_add(&casted->fDirectLock, -1) > 1) release_sem(casted->fDirectSem); casted->fDirectLockCount--; #endif } void BDirectWindow::InitData() { fConnectionEnable = false; fIsFullScreen = false; fInDirectConnect = false; fInitStatus = 0; fDirectDriverReady = false; fDirectDriverType = 0; fDirectDriverToken = 0; direct_driver = NULL; status_t status = B_ERROR; struct direct_window_sync_data syncData; if (Lock()) { fLink->StartMessage(AS_DIRECT_WINDOW_GET_SYNC_DATA); if (fLink->FlushWithReply(status) == B_OK && status == B_OK) { fLink->Read(&syncData); } Unlock(); } if (status < B_OK) return; #if DW_NEEDS_LOCKING fDirectLock = 0; fDirectLockCount = 0; fDirectLockOwner = -1; fDirectLockStack = NULL; fDirectSem = create_sem(1, "direct sem"); if (fDirectSem > 0) fInitStatus |= DW_STATUS_SEM_CREATED; #endif fSourceClippingArea = syncData.area; fDisableSem = syncData.disable_sem; fDisableSemAck = syncData.disable_sem_ack; fClonedClippingArea = clone_area("Clone direct area", (void**)&fBufferDesc, B_ANY_ADDRESS, B_READ_AREA, fSourceClippingArea); if (fClonedClippingArea > 0) { fInitStatus |= DW_STATUS_AREA_CLONED; fDirectDaemonId = spawn_thread(_DaemonStarter, "direct daemon", B_DISPLAY_PRIORITY, this); if (fDirectDaemonId > 0) { fDaemonKiller = false; if (resume_thread(fDirectDaemonId) == B_OK) fInitStatus |= DW_STATUS_THREAD_STARTED; else kill_thread(fDirectDaemonId); } } } void BDirectWindow::DisposeData() { // wait until the connection terminates: we can't destroy // the object until the client receives the B_DIRECT_STOP // notification, or bad things will happen while (fConnectionEnable) snooze(50000); LockDirect(); if (fInitStatus & DW_STATUS_THREAD_STARTED) { fDaemonKiller = true; // Release this sem, otherwise the Direct daemon thread // will wait forever on it release_sem(fDisableSem); status_t retVal; wait_for_thread(fDirectDaemonId, &retVal); } #if DW_NEEDS_LOCKING if (fInitStatus & DW_STATUS_SEM_CREATED) delete_sem(fDirectSem); #endif if (fInitStatus & DW_STATUS_AREA_CLONED) delete_area(fClonedClippingArea); } status_t BDirectWindow::DriverSetup() const { // Unimplemented in R5. // This function is probably here because they wanted, in a future time, // to implement graphic acceleration within BDirectWindow // (in fact, there is also a BDirectDriver member in BDirectWindow, // though it's not used). return B_OK; } void BDirectWindow::_ReservedDirectWindow1() {} void BDirectWindow::_ReservedDirectWindow2() {} void BDirectWindow::_ReservedDirectWindow3() {} void BDirectWindow::_ReservedDirectWindow4() {}