 +  return(current);
 +}
 +
-+
++// Called from the browser when the 2D graphics have been flushed out to the device.
++static void flush_callback(void* data, int32_t unused) {
++  SDL_VideoDevice* _this = reinterpret_cast<SDL_VideoDevice*>(data);
++
++  SDL_LockMutex(_this->hidden->flush_mutex);
++
++
++  assert(_this->hidden->flush_pending == true);
++  // image_data was locked since the last flush.
++  assert(_this->hidden->data_ready == false);
++  _this->hidden->flush_pending = false;
++
++  // Now is a good time to update image_data.
++  SDL_CondSignal(_this->hidden->flush_cond);
++
++  // Do another flush after a small delay.
++  pp::Module::Get()->core()->CallOnMainThread(kNaClFlushDelayMs, pp::CompletionCallback(&flush, _this), PP_OK);
++
++  SDL_UnlockMutex(_this->hidden->flush_mutex);
++}
++
++// Called periodically on the main thread.
 +static void flush(void* data, int32_t unused) {
 +  SDL_VideoDevice* _this = reinterpret_cast<SDL_VideoDevice*>(data);
 +
-+  SDL_LockMutex(_this->hidden->image_data_mutex);
-+  _this->hidden->context2d->PaintImageData(*_this->hidden->image_data, pp::Point());
-+  // TODO: This is a busy loop. Replace with CallOnMainThread with a delay when it is supported.
-+  _this->hidden->context2d->Flush(pp::CompletionCallback(&flush, _this));
-+  SDL_UnlockMutex(_this->hidden->image_data_mutex);
++  SDL_LockMutex(_this->hidden->flush_mutex);
++
++  assert(_this->hidden->flush_pending == false);
++  if (_this->hidden->data_ready) {
++    // Flush image_data.
++    _this->hidden->flush_pending = true;
++    _this->hidden->data_ready = false;
++    _this->hidden->context2d->PaintImageData(*_this->hidden->image_data, pp::Point());
++    _this->hidden->context2d->Flush(pp::CompletionCallback(&flush_callback, _this));
++  } else {
++    // Check again after a small delay.
++    pp::Module::Get()->core()->CallOnMainThread(kNaClFlushDelayMs, pp::CompletionCallback(&flush, _this), PP_OK);
++  }
++  SDL_UnlockMutex(_this->hidden->flush_mutex);
 +}
 +
 +static void flip(_THIS) {

+extern void NACL_PumpEvents(_THIS);
diff --git SDL-1.2.14/src/video/nacl/SDL_naclvideo.cc SDL-1.2.14/src/video/nacl/SDL_naclvideo.cc
new file mode 100644
index 0000000..e72075f
--- /dev/null
+++ SDL-1.2.14/src/video/nacl/SDL_naclvideo.cc
@@ -0,0 +1,215 @@
+#include "SDL_config.h"
+
+#include <assert.h>

+  int pitch;
+  void *buffer;
+
+  SDL_mutex* flush_mutex;
+  SDL_cond* flush_cond;
+  int ow, oh; // plugin output dimensions
+  // Whether context2d is being flushed to the device.
+  bool flush_pending;
+  // Whether image_data contains new data.
+  bool data_ready;
+  pp::ImageData* image_data;
+  pp::Graphics2D* context2d;  // The PINPAPI 2D drawing context.
+};
