don't attempt to clip layers anymore using glScissor

this seems to hurt performance on some GPU. this change
might negatively affect performance on other GPUs though, but
probably in less time-sensitive cases. If this becomes a
problem it might become necessary to pre-clip the geometry
(so that we don't have to use glScissor).

This improves the rotation animation quite a bit.

Change-Id: I5dbe1286f7ad858ef2c1e1ad9a07ee3f26c0b1f3
diff --git a/services/surfaceflinger/LayerBase.cpp b/services/surfaceflinger/LayerBase.cpp
index 416fc93..694ecde 100644
--- a/services/surfaceflinger/LayerBase.cpp
+++ b/services/surfaceflinger/LayerBase.cpp
@@ -231,7 +231,9 @@
     const uint32_t hw_h = hw.getHeight();
 
     uint32_t w = s.w;
-    uint32_t h = s.h;    
+    uint32_t h = s.h;
+
+    mNumVertices = 4;
     tr.transform(mVertices[0], 0, 0);
     tr.transform(mVertices[1], 0, h);
     tr.transform(mVertices[2], w, h);
@@ -268,27 +270,6 @@
         const Transform& planeTransform, Region& outDirtyRegion) {
 }
 
-void LayerBase::drawRegion(const Region& reg) const
-{
-    Region::const_iterator it = reg.begin();
-    Region::const_iterator const end = reg.end();
-    if (it != end) {
-        Rect r;
-        const DisplayHardware& hw(graphicPlane(0).displayHardware());
-        const int32_t fbWidth  = hw.getWidth();
-        const int32_t fbHeight = hw.getHeight();
-        const GLshort vertices[][2] = { { 0, 0 }, { fbWidth, 0 }, 
-                { fbWidth, fbHeight }, { 0, fbHeight }  };
-        glVertexPointer(2, GL_SHORT, 0, vertices);
-        while (it != end) {
-            const Rect& r = *it++;
-            const GLint sy = fbHeight - (r.top + r.height());
-            glScissor(r.left, sy, r.width(), r.height());
-            glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
-        }
-    }
-}
-
 void LayerBase::setGeometry(hwc_layer_t* hwcl)
 {
     hwcl->compositionType = HWC_FRAMEBUFFER;
@@ -345,9 +326,6 @@
 
 void LayerBase::draw(const Region& clip) const
 {
-    // reset GL state
-    glEnable(GL_SCISSOR_TEST);
-
     onDraw(clip);
 }
 
@@ -371,16 +349,8 @@
     glDisable(GL_TEXTURE_2D);
     glDisable(GL_BLEND);
 
-    Region::const_iterator it = clip.begin();
-    Region::const_iterator const end = clip.end();
-    glEnable(GL_SCISSOR_TEST);
     glVertexPointer(2, GL_FLOAT, 0, mVertices);
-    while (it != end) {
-        const Rect& r = *it++;
-        const GLint sy = fbHeight - (r.top + r.height());
-        glScissor(r.left, sy, r.width(), r.height());
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
-    }
+    glDrawArrays(GL_TRIANGLE_FAN, 0, mNumVertices);
 }
 
 void LayerBase::clearWithOpenGL(const Region& clip) const
@@ -434,15 +404,8 @@
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     glVertexPointer(2, GL_FLOAT, 0, mVertices);
     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
+    glDrawArrays(GL_TRIANGLE_FAN, 0, mNumVertices);
 
-    Region::const_iterator it = clip.begin();
-    Region::const_iterator const end = clip.end();
-    while (it != end) {
-        const Rect& r = *it++;
-        const GLint sy = fbHeight - (r.top + r.height());
-        glScissor(r.left, sy, r.width(), r.height());
-        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-    }
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     glDisable(GL_BLEND);
 }
diff --git a/services/surfaceflinger/LayerBase.h b/services/surfaceflinger/LayerBase.h
index cecc885..d123d9b 100644
--- a/services/surfaceflinger/LayerBase.h
+++ b/services/surfaceflinger/LayerBase.h
@@ -100,7 +100,6 @@
             uint32_t setTransactionFlags(uint32_t flags);
             
             Rect visibleBounds() const;
-            void drawRegion(const Region& reg) const;
 
     virtual sp<LayerBaseClient> getLayerBaseClient() const { return 0; }
     virtual sp<Layer> getLayer() const { return 0; }
@@ -260,6 +259,7 @@
                 int32_t         mPlaneOrientation;
                 Transform       mTransform;
                 GLfloat         mVertices[4][2];
+                size_t          mNumVertices;
                 Rect            mTransformedBounds;
             
                 // these are protected by an external lock
diff --git a/services/surfaceflinger/LayerDim.cpp b/services/surfaceflinger/LayerDim.cpp
index e665d7a..96a310f 100644
--- a/services/surfaceflinger/LayerDim.cpp
+++ b/services/surfaceflinger/LayerDim.cpp
@@ -43,9 +43,7 @@
 void LayerDim::onDraw(const Region& clip) const
 {
     const State& s(drawingState());
-    Region::const_iterator it = clip.begin();
-    Region::const_iterator const end = clip.end();
-    if (s.alpha>0 && (it != end)) {
+    if (s.alpha>0) {
         const DisplayHardware& hw(graphicPlane(0).displayHardware());
         const GLfloat alpha = s.alpha/255.0f;
         const uint32_t fbHeight = hw.getHeight();
@@ -62,13 +60,8 @@
         glColor4f(0, 0, 0, alpha);
 
         glVertexPointer(2, GL_FLOAT, 0, mVertices);
+        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
-        while (it != end) {
-            const Rect& r = *it++;
-            const GLint sy = fbHeight - (r.top + r.height());
-            glScissor(r.left, sy, r.width(), r.height());
-            glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 
-        }
         glDisable(GL_BLEND);
         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     }
diff --git a/services/surfaceflinger/LayerScreenshot.cpp b/services/surfaceflinger/LayerScreenshot.cpp
index c127fa6..b42353c 100644
--- a/services/surfaceflinger/LayerScreenshot.cpp
+++ b/services/surfaceflinger/LayerScreenshot.cpp
@@ -109,9 +109,7 @@
 void LayerScreenshot::onDraw(const Region& clip) const
 {
     const State& s(drawingState());
-    Region::const_iterator it = clip.begin();
-    Region::const_iterator const end = clip.end();
-    if (s.alpha>0 && (it != end)) {
+    if (s.alpha>0) {
         const DisplayHardware& hw(graphicPlane(0).displayHardware());
         const GLfloat alpha = s.alpha/255.0f;
         const uint32_t fbHeight = hw.getHeight();
@@ -137,13 +135,7 @@
         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
         glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
         glVertexPointer(2, GL_FLOAT, 0, mVertices);
-
-        while (it != end) {
-            const Rect& r = *it++;
-            const GLint sy = fbHeight - (r.top + r.height());
-            glScissor(r.left, sy, r.width(), r.height());
-            glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-        }
+        glDrawArrays(GL_TRIANGLE_FAN, 0, mNumVertices);
 
         glDisable(GL_BLEND);
         glDisable(GL_TEXTURE_2D);
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 5d61460..93dd3a4 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -265,7 +265,6 @@
     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
     glPixelStorei(GL_PACK_ALIGNMENT, 4);
     glEnableClientState(GL_VERTEX_ARRAY);
-    glEnable(GL_SCISSOR_TEST);
     glShadeModel(GL_FLAT);
     glDisable(GL_DITHER);
     glDisable(GL_CULL_FACE);
@@ -980,8 +979,6 @@
 
     if (mDebugRegion > 1)
         usleep(mDebugRegion * 1000);
-
-    glEnable(GL_SCISSOR_TEST);
 }
 
 void SurfaceFlinger::drawWormhole() const
@@ -990,52 +987,48 @@
     if (region.isEmpty())
         return;
 
-    const DisplayHardware& hw(graphicPlane(0).displayHardware());
-    const int32_t width = hw.getWidth();
-    const int32_t height = hw.getHeight();
+    glDisable(GL_TEXTURE_EXTERNAL_OES);
+    glDisable(GL_BLEND);
 
     if (CC_LIKELY(!mDebugBackground)) {
-        glClearColor(0,0,0,0);
-        Region::const_iterator it = region.begin();
-        Region::const_iterator const end = region.end();
-        while (it != end) {
-            const Rect& r = *it++;
-            const GLint sy = height - (r.top + r.height());
-            glScissor(r.left, sy, r.width(), r.height());
-            glClear(GL_COLOR_BUFFER_BIT);
-        }
+        glDisable(GL_TEXTURE_2D);
+        glColor4f(0,0,0,0);
     } else {
-        const GLshort vertices[][2] = { { 0, 0 }, { width, 0 },
-                { width, height }, { 0, height }  };
-        const GLshort tcoords[][2] = { { 0, 0 }, { 1, 0 },  { 1, 1 }, { 0, 1 } };
-
-        glVertexPointer(2, GL_SHORT, 0, vertices);
-        glTexCoordPointer(2, GL_SHORT, 0, tcoords);
+        const DisplayHardware& hw(graphicPlane(0).displayHardware());
+        const int32_t width = hw.getWidth();
+        const int32_t height = hw.getHeight();
+        const GLfloat tcoords[][2] = { { 0, 0 }, { 1, 0 },  { 1, 1 }, { 0, 1 } };
+        glTexCoordPointer(2, GL_FLOAT, 0, tcoords);
         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-
-        glDisable(GL_TEXTURE_EXTERNAL_OES);
         glEnable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, mWormholeTexName);
         glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
         glMatrixMode(GL_TEXTURE);
         glLoadIdentity();
-
-        glDisable(GL_BLEND);
-
         glScalef(width*(1.0f/32.0f), height*(1.0f/32.0f), 1);
-        Region::const_iterator it = region.begin();
-        Region::const_iterator const end = region.end();
-        while (it != end) {
-            const Rect& r = *it++;
-            const GLint sy = height - (r.top + r.height());
-            glScissor(r.left, sy, r.width(), r.height());
-            glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-        }
-        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-        glDisable(GL_TEXTURE_2D);
-        glLoadIdentity();
-        glMatrixMode(GL_MODELVIEW);
     }
+
+    GLfloat vertices[4][2];
+    glVertexPointer(2, GL_FLOAT, 0, vertices);
+    Region::const_iterator it = region.begin();
+    Region::const_iterator const end = region.end();
+    while (it != end) {
+        const Rect& r = *it++;
+        vertices[0][0] = r.left;
+        vertices[0][1] = r.top;
+        vertices[1][0] = r.right;
+        vertices[1][1] = r.top;
+        vertices[2][0] = r.right;
+        vertices[2][1] = r.bottom;
+        vertices[3][0] = r.left;
+        vertices[3][1] = r.bottom;
+        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+    }
+
+    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+    glDisable(GL_TEXTURE_2D);
+    glLoadIdentity();
+    glMatrixMode(GL_MODELVIEW);
 }
 
 status_t SurfaceFlinger::addLayer(const sp<LayerBase>& layer)
@@ -1841,7 +1834,6 @@
     glDisable(GL_SCISSOR_TEST);
     glClearColor(0,0,0,1);
     glClear(GL_COLOR_BUFFER_BIT);
-    glEnable(GL_SCISSOR_TEST);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     const Vector< sp<LayerBase> >& layers(mVisibleLayersSortedByZ);
@@ -2026,11 +2018,11 @@
     }
 
     glColorMask(1,1,1,1);
-    glEnable(GL_SCISSOR_TEST);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     glDeleteTextures(1, &tname);
     glDisable(GL_TEXTURE_2D);
     glDisable(GL_BLEND);
+    glDisable(GL_SCISSOR_TEST);
     return NO_ERROR;
 }
 
@@ -2169,11 +2161,11 @@
     }
 
     glColorMask(1,1,1,1);
-    glEnable(GL_SCISSOR_TEST);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     glDeleteTextures(1, &tname);
     glDisable(GL_TEXTURE_2D);
     glDisable(GL_BLEND);
+    glDisable(GL_SCISSOR_TEST);
 
     return NO_ERROR;
 }
@@ -2203,7 +2195,6 @@
     glClearColor(0,0,0,1);
     glDisable(GL_SCISSOR_TEST);
     glClear(GL_COLOR_BUFFER_BIT);
-    glEnable(GL_SCISSOR_TEST);
     hw.flip( Region(hw.bounds()) );
 
     return NO_ERROR;
@@ -2341,7 +2332,6 @@
         // invert everything, b/c glReadPixel() below will invert the FB
         glViewport(0, 0, sw, sh);
         glScissor(0, 0, sw, sh);
-        glEnable(GL_SCISSOR_TEST);
         glMatrixMode(GL_PROJECTION);
         glPushMatrix();
         glLoadIdentity();
@@ -2394,7 +2384,7 @@
                 result = NO_MEMORY;
             }
         }
-        glEnable(GL_SCISSOR_TEST);
+        glDisable(GL_SCISSOR_TEST);
         glViewport(0, 0, hw_w, hw_h);
         glMatrixMode(GL_PROJECTION);
         glPopMatrix();