Merge damage region when dropping an app buffer

When we decide to drop a buffer from a buffer queue, make sure to merge
the damage region from the dropped buffer into the current damage
region. Otherwise we'll pass the wrong damage region to hardware
composer and get broken rendering.

Bug: 136158117
Test: Wrote a new test in Transaction_test.cpp to repro the problem and
confirm the fix works.

Change-Id: Icdc61e1be3297450f2869c496dad1453fb6dca6d
diff --git a/services/surfaceflinger/BufferLayerConsumer.cpp b/services/surfaceflinger/BufferLayerConsumer.cpp
index 6709fb4..bd9bd81 100644
--- a/services/surfaceflinger/BufferLayerConsumer.cpp
+++ b/services/surfaceflinger/BufferLayerConsumer.cpp
@@ -369,6 +369,15 @@
     return mCurrentSurfaceDamage;
 }
 
+void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
+    if (damage.bounds() == Rect::INVALID_RECT ||
+        mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
+        mCurrentSurfaceDamage = Region::INVALID_REGION;
+    } else {
+        mCurrentSurfaceDamage |= damage;
+    }
+}
+
 int BufferLayerConsumer::getCurrentApi() const {
     Mutex::Autolock lock(mMutex);
     return mCurrentApi;