diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-05-22 20:44:12 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-05-22 20:44:12 +0000 |
commit | 671cd656785de5e84564b6ffe4831625d7016ded (patch) | |
tree | eea4b9e21a459d2b5c183f24b2eeb47cfe0be294 /src/utils | |
parent | 149e2f6159a797989f6f0fa93ecfaa66cdd55c40 (diff) |
add SDL support
git-svn-id: http://skia.googlecode.com/svn/trunk@182 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/SDL/SkOSWindow_SDL.cpp | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/utils/SDL/SkOSWindow_SDL.cpp b/src/utils/SDL/SkOSWindow_SDL.cpp new file mode 100644 index 0000000000..13bea72afa --- /dev/null +++ b/src/utils/SDL/SkOSWindow_SDL.cpp @@ -0,0 +1,175 @@ +#include "SkOSWindow_SDL.h" +#include "SkCanvas.h" +#include "SkOSMenu.h" +#include "SkTime.h" + +static void post_SkEvent_event() { + SDL_Event evt; + evt.type = SDL_USEREVENT; + evt.user.type = SDL_USEREVENT; + evt.user.code = 0; + evt.user.data1 = NULL; + evt.user.data2 = NULL; + SDL_PushEvent(&evt); +} + +static bool skia_setBitmapFromSurface(SkBitmap* dst, SDL_Surface* src) { + SkBitmap::Config config; + + switch (src->format->BytesPerPixel) { + case 2: + config = SkBitmap::kRGB_565_Config; + break; + case 4: + config = SkBitmap::kARGB_8888_Config; + break; + default: + return false; + } + + dst->setConfig(config, src->w, src->h, src->pitch); + dst->setPixels(src->pixels); + return true; +} + +SkOSWindow::SkOSWindow(void* surface) { + fSurface = reinterpret_cast<SDL_Surface*>(surface); + this->resize(fSurface->w, fSurface->h); +} + +void SkOSWindow::doDraw() { + if ( SDL_MUSTLOCK(fSurface) ) { + if ( SDL_LockSurface(fSurface) < 0 ) { + return; + } + } + + SkBitmap bitmap; + + if (skia_setBitmapFromSurface(&bitmap, fSurface)) { + SkCanvas canvas(bitmap); + this->draw(&canvas); + } + + if ( SDL_MUSTLOCK(fSurface) ) { + SDL_UnlockSurface(fSurface); + } + SDL_UpdateRect(fSurface, 0, 0, fSurface->w, fSurface->h); +} + +static SkKey find_skkey(SDLKey src) { + // this array must match the enum order in SkKey.h + static const SDLKey gKeys[] = { + SDLK_UNKNOWN, + SDLK_UNKNOWN, // left softkey + SDLK_UNKNOWN, // right softkey + SDLK_UNKNOWN, // home + SDLK_UNKNOWN, // back + SDLK_UNKNOWN, // send + SDLK_UNKNOWN, // end + SDLK_0, + SDLK_1, + SDLK_2, + SDLK_3, + SDLK_4, + SDLK_5, + SDLK_6, + SDLK_7, + SDLK_8, + SDLK_9, + SDLK_ASTERISK, + SDLK_HASH, + SDLK_UP, + SDLK_DOWN, + SDLK_LEFT, + SDLK_RIGHT, + SDLK_RETURN, // OK + SDLK_UNKNOWN, // volume up + SDLK_UNKNOWN, // volume down + SDLK_UNKNOWN, // power + SDLK_UNKNOWN, // camera + }; + + const SDLKey* array = gKeys; + for (size_t i = 0; i < SK_ARRAY_COUNT(gKeys); i++) { + if (array[i] == src) { + return static_cast<SkKey>(i); + } + } + return kNONE_SkKey; +} + +void SkOSWindow::handleSDLEvent(const SDL_Event& event) { + switch (event.type) { + case SDL_VIDEORESIZE: + this->resize(event.resize.w, event.resize.h); + break; + case SDL_VIDEOEXPOSE: + this->doDraw(); + break; + case SDL_MOUSEMOTION: + if (event.motion.state == SDL_PRESSED) { + this->handleClick(event.motion.x, event.motion.y, + SkView::Click::kMoved_State); + } + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + this->handleClick(event.button.x, event.button.y, + event.button.state == SDL_PRESSED ? + SkView::Click::kDown_State : + SkView::Click::kUp_State); + break; + case SDL_KEYDOWN: { + SkKey sk = find_skkey(event.key.keysym.sym); + if (kNONE_SkKey != sk) { + if (event.key.state == SDL_PRESSED) { + this->handleKey(sk); + } else { + this->handleKeyUp(sk); + } + } + break; + } + case SDL_USEREVENT: + if (SkEvent::ProcessEvent()) { + post_SkEvent_event(); + } + break; + } +} + +void SkOSWindow::onHandleInval(const SkIRect& r) { + SDL_Event evt; + evt.type = SDL_VIDEOEXPOSE; + evt.expose.type = SDL_VIDEOEXPOSE; + SDL_PushEvent(&evt); +} + +void SkOSWindow::onSetTitle(const char title[]) { + SDL_WM_SetCaption(title, NULL); +} + +void SkOSWindow::onAddMenu(const SkOSMenu* sk_menu) {} + +/////////////////////////////////////////////////////////////////////////////////////// + +void SkEvent::SignalNonEmptyQueue() { + SkDebugf("-------- signal nonempty\n"); + post_SkEvent_event(); +} + +static Uint32 timer_callback(Uint32 interval) { +// SkDebugf("-------- timercallback %d\n", interval); + SkEvent::ServiceQueueTimer(); + return 0; +} + +void SkEvent::SignalQueueTimer(SkMSec delay) +{ + SDL_SetTimer(0, NULL); + if (delay) { + SDL_SetTimer(delay, timer_callback); + } +} + |