drm_hwcomposer: Check against gem handles for popped buffer

Add check before closing gem handle for most recently popped buffer in
addition to buffers in queue.

Change-Id: Icb096bd4db439b1195a123e28468142829fc61ea
Reviewed-on: https://chrome-internal-review.googlesource.com/200725
Reviewed-by: Stéphane Marchesin <marcheu@google.com>
Commit-Queue: Stéphane Marchesin <marcheu@google.com>
Tested-by: Stéphane Marchesin <marcheu@google.com>
diff --git a/hwcomposer.cpp b/hwcomposer.cpp
index 38616ff..77caf90 100644
--- a/hwcomposer.cpp
+++ b/hwcomposer.cpp
@@ -318,8 +318,7 @@
 			continue;
 
 		/* check for duplicate handle in buf_queue */
-		bool found = false;
-		std::list<struct hwc_drm_bo>::iterator bi;
+		bool found;
 
 		ret = pthread_mutex_lock(&hd->set_worker.lock);
 		if (ret) {
@@ -328,13 +327,17 @@
 		}
 
 		found = false;
-		for (bi = hd->buf_queue.begin();
+		for (std::list<struct hwc_drm_bo>::iterator bi = hd->buf_queue.begin();
 		     bi != hd->buf_queue.end();
 		     ++bi)
 			for (int j = 0; j < ARRAY_SIZE(bi->gem_handles); j++)
-				if (hd->front.gem_handles[i] == bi->gem_handles[j])
+				if (hd->front.gem_handles[i] == bi->gem_handles[j] )
 					found = true;
 
+		for (int j = 0; j < ARRAY_SIZE(buf->gem_handles); j++)
+			if (hd->front.gem_handles[i] == buf->gem_handles[j])
+				found = true;
+
 		if (!found) {
 			args.handle = hd->front.gem_handles[i];
 			drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);