Minimize BeginScene / EndScene calls
TRAC #12139
Signed-off-by: Shannon Woods
Signed-off-by: Daniel Koch

Author:    Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/trunk@231 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Blit.cpp b/src/libGLESv2/Blit.cpp
index f056d2b..2cec8c2 100644
--- a/src/libGLESv2/Blit.cpp
+++ b/src/libGLESv2/Blit.cpp
@@ -370,6 +370,7 @@
 
 IDirect3DTexture9 *Blit::copySurfaceToTexture(IDirect3DSurface9 *surface, const RECT &sourceRect)
 {
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
 
     D3DSURFACE_DESC sourceDesc;
@@ -401,6 +402,7 @@
     d3dSourceRect.top = sourceRect.top;
     d3dSourceRect.bottom = sourceRect.bottom;
 
+    display->endScene();
     result = device->StretchRect(surface, &d3dSourceRect, textureSurface, NULL, D3DTEXF_NONE);
 
     textureSurface->Release();
@@ -456,14 +458,14 @@
 
 void Blit::render()
 {
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
 
     HRESULT hr = device->SetStreamSource(0, mQuadVertexBuffer, 0, 2 * sizeof(float));
     hr = device->SetVertexDeclaration(mQuadVertexDeclaration);
 
-    device->BeginScene();
+    display->startScene();
     hr = device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
-    device->EndScene();
 }
 
 }
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index 83a8d90..851b061 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -2014,6 +2014,7 @@
 
 void Context::clear(GLbitfield mask)
 {
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
     DWORD flags = 0;
 
@@ -2153,9 +2154,8 @@
         quad[3].w = 1.0f;
         quad[3].diffuse = color;
 
-        device->BeginScene();
+        display->startScene();
         device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
-        device->EndScene();
 
         if (flags & D3DCLEAR_ZBUFFER)
         {
@@ -2177,6 +2177,7 @@
         return error(GL_INVALID_OPERATION);
     }
 
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
     D3DPRIMITIVETYPE primitiveType;
     int primitiveCount;
@@ -2206,9 +2207,8 @@
 
     if (!cullSkipsDraw(mode))
     {
-        device->BeginScene();
+        display->startScene();
         device->DrawPrimitive(primitiveType, 0, primitiveCount);
-        device->EndScene();
     }
 }
 
@@ -2224,6 +2224,7 @@
         return error(GL_INVALID_OPERATION);
     }
 
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
     D3DPRIMITIVETYPE primitiveType;
     int primitiveCount;
@@ -2254,14 +2255,14 @@
 
     if (!cullSkipsDraw(mode))
     {
-        device->BeginScene();
+        display->startScene();
         device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/sizeof(Index), primitiveCount);
-        device->EndScene();
     }
 }
 
 void Context::finish()
 {
+    egl::Display *display = getDisplay();
     IDirect3DDevice9 *device = getDevice();
     IDirect3DQuery9 *occlusionQuery = NULL;
 
@@ -2283,9 +2284,8 @@
         device->SetVertexShader(NULL);
         device->SetFVF(D3DFVF_XYZRHW);
         float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
-        device->BeginScene();
+        display->startScene();
         device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
-        device->EndScene();
 
         occlusionQuery->Issue(D3DISSUE_END);
 
diff --git a/src/libGLESv2/Texture.cpp b/src/libGLESv2/Texture.cpp
index 86a89dc..2257c03 100644
--- a/src/libGLESv2/Texture.cpp
+++ b/src/libGLESv2/Texture.cpp
@@ -681,6 +681,7 @@
 
     if (mWidth != 0 && mHeight != 0)
     {
+        egl::Display *display = getDisplay();
         IDirect3DDevice9 *device = getDevice();
         D3DFORMAT format = selectFormat(mImageArray[0].format);
 
@@ -722,6 +723,7 @@
                     return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
                 }
 
+                display->endScene();
                 result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
 
                 if (FAILED(result))
@@ -1056,6 +1058,7 @@
 
     if (mWidth != 0)
     {
+        egl::Display *display = getDisplay();
         IDirect3DDevice9 *device = getDevice();
         D3DFORMAT format = selectFormat(mImageArray[0][0].format);
 
@@ -1099,6 +1102,7 @@
                         return error(GL_OUT_OF_MEMORY, (IDirect3DBaseTexture9*)NULL);
                     }
 
+                    display->endScene();
                     result = device->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
 
                     if (FAILED(result))
diff --git a/src/libGLESv2/main.cpp b/src/libGLESv2/main.cpp
index 69b8051..a99ec44 100644
--- a/src/libGLESv2/main.cpp
+++ b/src/libGLESv2/main.cpp
@@ -93,10 +93,16 @@
     return current->context;
 }
 
-IDirect3DDevice9 *getDevice()
+egl::Display *getDisplay()
 {
     Current *current = (Current*)TlsGetValue(currentTLS);
-    egl::Display *display = current->display;
+
+    return current->display;
+}
+
+IDirect3DDevice9 *getDevice()
+{
+    egl::Display *display = getDisplay();
 
     return display->getDevice();
 }