Refactor API for mipmap-builder,

now the builder returns the new image.

Change-Id: Ie56256390b96d3fdbe39f89784276947047df656
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/316442
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/gm/showmiplevels.cpp b/gm/showmiplevels.cpp
index 8d81c9b..169e3cb 100644
--- a/gm/showmiplevels.cpp
+++ b/gm/showmiplevels.cpp
@@ -333,7 +333,7 @@
             auto surf = SkSurface::MakeRasterDirect(builder.level(i));
             surf->getCanvas()->drawColor(colors[i % SK_ARRAY_COUNT(colors)]);
         }
-        fImg = fImg->withMipmaps(builder.detach());
+        fImg = builder.attachTo(fImg.get());
     }
 
     DrawResult onDraw(SkCanvas* canvas, SkString*) override {
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index b5dd49f..ba711e5 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -25,6 +25,7 @@
 
 class SkData;
 class SkCanvas;
+class SkImage;
 class SkImageFilter;
 class SkImageGenerator;
 class SkMipmap;
@@ -64,10 +65,20 @@
     int countLevels() const;
     SkPixmap level(int index) const;
 
-    sk_sp<SkMipmap> detach();
+    /**
+     *  If these levels are compatible with src, return a new Image that combines src's base level
+     *  with these levels as mip levels. If not compatible, this returns nullptr.
+     */
+    sk_sp<SkImage> attachTo(const SkImage* src);
+
+    sk_sp<SkImage> attachTo(sk_sp<SkImage> src) {
+        return this->attachTo(src.get());
+    }
 
 private:
     sk_sp<SkMipmap> fMM;
+
+    friend class SkImage;
 };
 
 /** \class SkImage
@@ -1197,15 +1208,6 @@
 
     /**
      *  Returns an image with the same "base" pixels as the this image, but with mipmap levels
-     *  as well. If this image already has mipmap levels, they will be replaced with new ones.
-     *
-     *  If data == nullptr, the mipmap levels are computed automatically.
-     *  If data != nullptr, then the caller has provided the data for each level.
-     */
-    sk_sp<SkImage> withMipmaps(sk_sp<SkMipmap> data) const;
-
-    /**
-     *  Returns an image with the same "base" pixels as the this image, but with mipmap levels
      *  automatically generated and attached.
      */
     sk_sp<SkImage> withDefaultMipmaps() const;
@@ -1396,10 +1398,13 @@
 private:
     SkImage(const SkImageInfo& info, uint32_t uniqueID);
     friend class SkImage_Base;
+    friend class SkMipmapBuilder;
 
     SkImageInfo     fInfo;
     const uint32_t  fUniqueID;
 
+    sk_sp<SkImage> withMipmaps(sk_sp<SkMipmap>) const;
+
     using INHERITED = SkRefCnt;
 };
 
diff --git a/src/core/SkReadBuffer.cpp b/src/core/SkReadBuffer.cpp
index dbb2d13..0f328a5 100644
--- a/src/core/SkReadBuffer.cpp
+++ b/src/core/SkReadBuffer.cpp
@@ -408,7 +408,7 @@
                 if (auto ri = image->makeRasterImage()) {
                     image = ri;
                 }
-                image = image->withMipmaps(builder.detach());
+                image = builder.attachTo(image);
                 SkASSERT(image);    // withMipmaps should never return null
             }
         }
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 8674de4..8042bf5 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -662,10 +662,6 @@
     return pm;
 }
 
-sk_sp<SkMipmap> SkMipmapBuilder::detach() {
-    return std::move(fMM);
-}
-
 bool SkImage::hasMipmaps() const {
     return as_IB(this)->onPeekMips() != nullptr;
 }
@@ -682,3 +678,7 @@
 sk_sp<SkImage> SkImage::withDefaultMipmaps() const {
     return this->withMipmaps(nullptr);
 }
+
+sk_sp<SkImage> SkMipmapBuilder::attachTo(const SkImage* src) {
+    return src->withMipmaps(fMM);
+}
diff --git a/tests/MipMapTest.cpp b/tests/MipMapTest.cpp
index 8beaba3..fe6d234 100644
--- a/tests/MipMapTest.cpp
+++ b/tests/MipMapTest.cpp
@@ -235,8 +235,7 @@
     SkMipmapBuilder builder(img->imageInfo());
     fill_in_mips(&builder, img);
 
-    auto img2 = img->withMipmaps(builder.detach());
-    REPORTER_ASSERT(reporter, !builder.detach());
+    auto img2 = builder.attachTo(img);
     REPORTER_ASSERT(reporter, img.get()  != img2.get());
     REPORTER_ASSERT(reporter, img1.get() != img2.get());
     REPORTER_ASSERT(reporter, img2->hasMipmaps());
@@ -248,7 +247,7 @@
     auto check_fails = [reporter](sk_sp<SkImage> img, const SkImageInfo& info) {
         SkMipmapBuilder builder(info);
         fill_in_mips(&builder, img);
-        auto img2 = img->withMipmaps(builder.detach());
+        auto img2 = builder.attachTo(img);
         // if withMipmaps() succeeds, it returns a new image, otherwise it returns the original
         REPORTER_ASSERT(reporter, img.get() == img2.get());
     };