We want to give SkPixelRef a way to signal over to GrResourceCache that it's become pointless to keep around textures based on that SkPixelRef when its pixels change, so that it can be a good citizen and free those textures.

This adds an invalidation listener mechanism to SkPixelRef to let it send this message while still staying ignorant of who's listening.

These messages are tricky to deliver.  The SkPixelRefs they originates from and the GrResourceCaches they ultimately end up at may be on different threads; neither class is threadsafe; their object lifetimes are totally independent; it's a many-senders-to-many-receivers relation; and neither codebase should really know about the other.

So I've added a per-message-type global message bus to broadcast messages to threadsafe inboxes.  Anyone can post() a message, which will show up in all the inboxes of that type, read whenever the inbox's owner calls poll().  The implementation is _dumb_; it can be improved in several dimensions (inbox size limits, lock-free message delivery) if we find the need.

I took some care to make sure not to send the invalidation message for any SkPixelRef that's sharing a generation ID with another SkPixelRef.

BUG=
R=bsalomon@google.com, scroggo@google.com, reed@google.com

Author: mtklein@google.com

Review URL: https://codereview.chromium.org/26734003

git-svn-id: http://skia.googlecode.com/svn/trunk@11949 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 9cafbf6..18c54ef 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -7,6 +7,9 @@
 
 #include "SkGr.h"
 #include "SkConfig8888.h"
+#include "SkMessageBus.h"
+#include "SkPixelRef.h"
+#include "GrResourceCache.h"
 
 /*  Fill out buffer with the compressed format Ganesh expects from a colortable
  based bitmap. [palette (colortable) + indices].
@@ -86,6 +89,28 @@
     desc->fSampleCnt = 0;
 }
 
+namespace {
+
+// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
+class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener {
+public:
+    explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
+private:
+    GrResourceKey fKey;
+
+    virtual void onChange() SK_OVERRIDE {
+        const GrResourceInvalidatedMessage message = { fKey };
+        SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
+    }
+};
+
+}  // namespace
+
+static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
+    SkASSERT(NULL != pixelRef);
+    pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
+}
+
 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
                                               bool cache,
                                               const GrTextureParams* params,
@@ -112,7 +137,12 @@
             if (cache) {
                 GrCacheID cacheID;
                 generate_bitmap_cache_id(origBitmap, &cacheID);
-                return ctx->createTexture(params, desc, cacheID, storage.get(), bitmap->width());
+
+                GrResourceKey key;
+                GrTexture* result = ctx->createTexture(params, desc, cacheID,
+                                                       storage.get(), bitmap->width(), &key);
+                add_genID_listener(key, origBitmap.pixelRef());
+                return result;
             } else {
                 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
                                                             GrContext::kExact_ScratchTexMatch);
@@ -137,8 +167,13 @@
         // This texture is likely to be used again so leave it in the cache
         GrCacheID cacheID;
         generate_bitmap_cache_id(origBitmap, &cacheID);
-        return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bitmap->rowBytes());
-    } else {
+
+        GrResourceKey key;
+        GrTexture* result = ctx->createTexture(params, desc, cacheID,
+                                               bitmap->getPixels(), bitmap->rowBytes(), &key);
+        add_genID_listener(key, origBitmap.pixelRef());
+        return result;
+   } else {
         // This texture is unlikely to be used again (in its present form) so
         // just use a scratch texture. This will remove the texture from the
         // cache so no one else can find it. Additionally, once unlocked, the