Use DwmAPI, if available, to minimize queueing of presents.

Review URL: http://codereview.appspot.com/4559065

git-svn-id: https://angleproject.googlecode.com/svn/trunk@673 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/build_angle.gyp b/src/build_angle.gyp
index 1f427f0..dca692b 100644
--- a/src/build_angle.gyp
+++ b/src/build_angle.gyp
@@ -228,7 +228,11 @@
               'AdditionalDependencies': [
                 'd3d9.lib',
                 'dxguid.lib',
+                'dwmapi.lib',
               ],
+              'DelayLoadDLLs': [
+                'dwmapi.dll',
+              ]
             }
           },
         },
diff --git a/src/common/version.h b/src/common/version.h
index 0d522d6..3049300 100644
--- a/src/common/version.h
+++ b/src/common/version.h
@@ -1,7 +1,7 @@
 #define MAJOR_VERSION 0
 #define MINOR_VERSION 0
 #define BUILD_VERSION 0
-#define BUILD_REVISION 672
+#define BUILD_REVISION 673
 
 #define STRINGIFY(x) #x
 #define MACRO_STRINGIFY(x) STRINGIFY(x)
diff --git a/src/libEGL/Surface.cpp b/src/libEGL/Surface.cpp
index e5a8c6b..d49d67f 100644
--- a/src/libEGL/Surface.cpp
+++ b/src/libEGL/Surface.cpp
@@ -18,6 +18,8 @@
 #include "libEGL/main.h"
 #include "libEGL/Display.h"
 
+#include <dwmapi.h>
+
 namespace egl
 {
 Surface::Surface(Display *display, const Config *config, HWND window) 
@@ -69,7 +71,29 @@
 bool Surface::initialize()
 {
     ASSERT(!mSwapChain && !mOffscreenTexture && !mDepthStencil);
-    return resetSwapChain();
+
+    if (!resetSwapChain())
+      return false;
+
+    // Modify present parameters for this window, if we are composited,
+    // to minimize the amount of queuing done by DWM between our calls to
+    // present and the actual screen.
+    if (mWindow && (LOWORD(GetVersion()) >= 0x60)) {
+      BOOL isComposited;
+      HRESULT result = DwmIsCompositionEnabled(&isComposited);
+      if (SUCCEEDED(result) && isComposited) {
+        DWM_PRESENT_PARAMETERS presentParams;
+        memset(&presentParams, 0, sizeof(presentParams));
+        presentParams.cbSize = sizeof(DWM_PRESENT_PARAMETERS);
+        presentParams.cBuffer = 2;
+
+        result = DwmSetPresentParameters(mWindow, &presentParams);
+        if (FAILED(result))
+          ERR("Unable to set present parameters: %081X", result);
+      }
+    }
+
+    return true;
 }
 
 void Surface::release()
@@ -143,16 +167,6 @@
 
     bool useFlipEx = (LOWORD(GetVersion()) >= 0x61) && mDisplay->isD3d9ExDevice();
 
-    // FlipEx causes unseemly stretching when resizing windows AND when one
-    // draws outside of the WM_PAINT callback. While this is seldom a problem in
-    // single process applications, it is particuarly noticeable in multi-process
-    // applications. Therefore, if we find that the creator process of our window
-    // is not the current process, disable use of FlipEx.
-    DWORD windowPID;
-    GetWindowThreadProcessId(mWindow, &windowPID);
-    if(windowPID != GetCurrentProcessId())
-      useFlipEx = false;
-
     presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat;
     // We set BackBufferCount = 1 even when we use D3DSWAPEFFECT_FLIPEX.
     // We do this because DirectX docs are a bit vague whether to set this to 1
diff --git a/src/libEGL/libEGL.vcproj b/src/libEGL/libEGL.vcproj
index 7a7b2f9..2dc0284 100644
--- a/src/libEGL/libEGL.vcproj
+++ b/src/libEGL/libEGL.vcproj
@@ -62,9 +62,10 @@
 			/>

 			<Tool

 				Name="VCLinkerTool"

-				AdditionalDependencies="d3d9.lib dxguid.lib"

+				AdditionalDependencies="d3d9.lib dxguid.lib dwmapi.lib"

 				LinkIncremental="2"

 				ModuleDefinitionFile="libEGL.def"

+				DelayLoadDLLs="dwmapi.dll"

 				GenerateDebugInformation="true"

 				SubSystem="2"

 				RandomizedBaseAddress="1"