Merge third_party/angle_dx11 from https://chromium.googlesource.com/external/angle.git at af640efbfdbe80909016fc623bb65f010a2e551f

This commit was generated by merge_from_chromium.py.

Change-Id: I45af795ac7ab674108b79ea2d867643e3dde1f1f
diff --git a/src/libGLESv2/renderer/Renderer9.cpp b/src/libGLESv2/renderer/Renderer9.cpp
index 59ae6c5..c46d82b 100644
--- a/src/libGLESv2/renderer/Renderer9.cpp
+++ b/src/libGLESv2/renderer/Renderer9.cpp
@@ -2115,7 +2115,7 @@
         return (mDeviceEx != NULL);
       case D3DERR_DEVICEREMOVED:
         ASSERT(mDeviceEx != NULL);
-        return true;
+        return isRemovedDeviceResettable();
       default:
         return false;
     }
@@ -2129,23 +2129,26 @@
 
     HRESULT result = D3D_OK;
     bool lost = testDeviceLost(false);
-    int attempts = 3;
     bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED);
 
-    while (lost && attempts > 0)
+    // Device Removed is a feature which is only present with D3D9Ex
+    ASSERT(mDeviceEx != NULL || !removedDevice);
+
+    for (int attempts = 3; lost && attempts > 0; attempts--)
     {
-        if (mDeviceEx)
+        if (removedDevice)
+        {
+            // Device removed, which may trigger on driver reinstallation,
+            // may cause a longer wait other reset attempts before the
+            // system is ready to handle creating a new device.
+            Sleep(800);
+            lost = !resetRemovedDevice();
+        }
+        else if (mDeviceEx)
         {
             Sleep(500);   // Give the graphics driver some CPU time
-
-            if (removedDevice)
-            {
-                result = resetRemovedDevice();
-            }
-            else
-            {
-                result = mDeviceEx->ResetEx(&presentParameters, NULL);
-            }
+            result = mDeviceEx->ResetEx(&presentParameters, NULL);
+            lost = testDeviceLost(false);
         }
         else
         {
@@ -2160,10 +2163,8 @@
             {
                 result = mDevice->Reset(&presentParameters);
             }
+            lost = testDeviceLost(false);
         }
-
-        lost = testDeviceLost(false);
-        attempts --;
     }
 
     if (FAILED(result))
@@ -2172,6 +2173,12 @@
         return false;
     }
 
+    if (removedDevice && lost)
+    {
+        ERR("Device lost reset failed multiple times");
+        return false;
+    }
+
     // If the device was removed, we already finished re-initialization in resetRemovedDevice
     if (!removedDevice)
     {
@@ -2184,15 +2191,38 @@
     return true;
 }
 
-HRESULT Renderer9::resetRemovedDevice()
+bool Renderer9::isRemovedDeviceResettable() const
+{
+    bool success = false;
+
+#ifdef ANGLE_ENABLE_D3D9EX
+    IDirect3D9Ex *d3d9Ex = NULL;
+    typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
+    Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
+
+    if (Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &d3d9Ex)))
+    {
+        D3DCAPS9 deviceCaps;
+        HRESULT result = d3d9Ex->GetDeviceCaps(mAdapter, mDeviceType, &deviceCaps);
+        success = SUCCEEDED(result);
+    }
+
+    SafeRelease(d3d9Ex);
+#else
+    ASSERT(UNREACHABLE());
+#endif
+
+    return success;
+}
+
+bool Renderer9::resetRemovedDevice()
 {
     // From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx:
     // The hardware adapter has been removed. Application must destroy the device, do enumeration of
     // adapters and create another Direct3D device. If application continues rendering without
     // calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only.
-    ASSERT(mDeviceEx != NULL);
     deinitialize();
-    return (initialize() == EGL_SUCCESS ? S_OK : S_FALSE);
+    return (initialize() == EGL_SUCCESS);
 }
 
 DWORD Renderer9::getAdapterVendor() const
diff --git a/src/libGLESv2/renderer/Renderer9.h b/src/libGLESv2/renderer/Renderer9.h
index 65e8d73..1fac9b7 100644
--- a/src/libGLESv2/renderer/Renderer9.h
+++ b/src/libGLESv2/renderer/Renderer9.h
@@ -221,7 +221,8 @@
     void releaseDeviceResources();
 
     HRESULT getDeviceStatusCode();
-    HRESULT resetRemovedDevice();
+    bool isRemovedDeviceResettable() const;
+    bool resetRemovedDevice();
 
     UINT mAdapter;
     D3DDEVTYPE mDeviceType;
diff --git a/src/libGLESv2/renderer/SwapChain9.cpp b/src/libGLESv2/renderer/SwapChain9.cpp
index 757ce1c..7b9b32b 100644
--- a/src/libGLESv2/renderer/SwapChain9.cpp
+++ b/src/libGLESv2/renderer/SwapChain9.cpp
@@ -312,6 +312,11 @@
     device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
     device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1);
 
+    for (UINT streamIndex = 0; streamIndex < gl::MAX_VERTEX_ATTRIBS; streamIndex++)
+    {
+        device->SetStreamSourceFreq(streamIndex, 1);
+    }
+
     D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f};
     device->SetViewport(&viewport);