diff --git a/gm/showmiplevels.cpp b/gm/showmiplevels.cpp index 8d81c9bf62..169e3cb059 100644 --- a/gm/showmiplevels.cpp +++ b/gm/showmiplevels.cpp @@ -333,7 +333,7 @@ class ShowMipLevels3 : public skiagm::GM { 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 b5dd49fe5e..ba711e5fb6 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 @@ public: int countLevels() const; SkPixmap level(int index) const; - sk_sp 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 attachTo(const SkImage* src); + + sk_sp attachTo(sk_sp src) { + return this->attachTo(src.get()); + } private: sk_sp fMM; + + friend class SkImage; }; /** \class SkImage @@ -1195,15 +1206,6 @@ public: */ bool hasMipmaps() const; - /** - * 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 withMipmaps(sk_sp data) const; - /** * Returns an image with the same "base" pixels as the this image, but with mipmap levels * automatically generated and attached. @@ -1396,10 +1398,13 @@ public: private: SkImage(const SkImageInfo& info, uint32_t uniqueID); friend class SkImage_Base; + friend class SkMipmapBuilder; SkImageInfo fInfo; const uint32_t fUniqueID; + sk_sp withMipmaps(sk_sp) const; + using INHERITED = SkRefCnt; }; diff --git a/src/core/SkReadBuffer.cpp b/src/core/SkReadBuffer.cpp index dbb2d13bc6..0f328a5cf7 100644 --- a/src/core/SkReadBuffer.cpp +++ b/src/core/SkReadBuffer.cpp @@ -408,7 +408,7 @@ sk_sp SkReadBuffer::readImage() { 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 8674de4b1b..8042bf5019 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -662,10 +662,6 @@ SkPixmap SkMipmapBuilder::level(int index) const { return pm; } -sk_sp SkMipmapBuilder::detach() { - return std::move(fMM); -} - bool SkImage::hasMipmaps() const { return as_IB(this)->onPeekMips() != nullptr; } @@ -682,3 +678,7 @@ sk_sp SkImage::withMipmaps(sk_sp mips) const { sk_sp SkImage::withDefaultMipmaps() const { return this->withMipmaps(nullptr); } + +sk_sp SkMipmapBuilder::attachTo(const SkImage* src) { + return src->withMipmaps(fMM); +} diff --git a/tests/MipMapTest.cpp b/tests/MipMapTest.cpp index 8beaba3abd..fe6d234777 100644 --- a/tests/MipMapTest.cpp +++ b/tests/MipMapTest.cpp @@ -235,8 +235,7 @@ DEF_TEST(image_mip_factory, reporter) { 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 @@ DEF_TEST(image_mip_mismatch, reporter) { auto check_fails = [reporter](sk_sp 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()); };