From 2d696238c1acdd26b824b80c26942d06c071f451 Mon Sep 17 00:00:00 2001
From: Mike Klein <mtklein@google.com>
Date: Mon, 17 Sep 2018 17:29:39 -0400
Subject: [PATCH] RESTRICT AUTOMERGE: Make listener lists threadsafe with a
 mutex.

Bug: 124232283
Test: Infeasible

Cherry-pick of https://skia-review.googlesource.com/155060 in Skia

There were conflicts due the fact that pi-dev does not have commit
afa11586d782c7cb3e83b8af48023ff227349516 ("Make the SkPathRef
GenIDChangeListener ref counted") or
6c8d242b14355bf66c9137e9e4d6c7861d22168f ("Make atomic lists list for
bitmaps and paths" - an alternate fix for this issue) and some smaller
header file changes.

Change-Id: I7c2c5cd6603007d099169071a1b7d1a230c621bc
Merged-In: I91a8fbdd1b8fb4cf8b124ebdf17212c643058ef3
---
 include/core/SkPixelRef.h | 3 ++-
 src/core/SkPixelRef.cpp   | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/core/SkPixelRef.h b/include/core/SkPixelRef.h
index 4369e5d537..3b2de4eaa8 100644
--- a/include/core/SkPixelRef.h
+++ b/include/core/SkPixelRef.h
@@ -236,7 +236,7 @@ class SK_API SkPixelRef : public SkFlattenable {
         virtual void onChange() = 0;
     };
 
-    // Takes ownership of listener.
+    // Takes ownership of listener. Threadsafe.
     void addGenIDChangeListener(GenIDChangeListener* listener);
 
 protected:
@@ -311,6 +311,7 @@ class SK_API SkPixelRef : public SkFlattenable {
     mutable uint32_t fGenerationID;
     mutable bool     fUniqueGenerationID;
 
+    SkMutex                         fGenIDChangeListenersMutex;
     SkTDArray<GenIDChangeListener*> fGenIDChangeListeners;  // pointers are owned
 
     SkString    fURI;
diff --git a/src/core/SkPixelRef.cpp b/src/core/SkPixelRef.cpp
index 6cc67d89af..290d29d71c 100644
--- a/src/core/SkPixelRef.cpp
+++ b/src/core/SkPixelRef.cpp
@@ -242,10 +242,12 @@ void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) {
         SkDELETE(listener);
         return;
     }
+    SkAutoMutexAcquire lock(fGenIDChangeListenersMutex);
     *fGenIDChangeListeners.append() = listener;
 }
 
 void SkPixelRef::callGenIDChangeListeners() {
+    SkAutoMutexAcquire lock(fGenIDChangeListenersMutex);
     // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
     if (fUniqueGenerationID) {
         for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
