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>
This commit is contained in:
Mike Reed 2020-09-17 13:58:26 -04:00 committed by Skia Commit-Bot
parent 7e129b8b89
commit b4aa639f23
5 changed files with 23 additions and 19 deletions

View File

@ -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 {

View File

@ -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<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
@ -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<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.
@ -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<SkImage> withMipmaps(sk_sp<SkMipmap>) const;
using INHERITED = SkRefCnt;
};

View File

@ -408,7 +408,7 @@ sk_sp<SkImage> 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
}
}

View File

@ -662,10 +662,6 @@ SkPixmap SkMipmapBuilder::level(int index) const {
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::withMipmaps(sk_sp<SkMipmap> mips) const {
sk_sp<SkImage> SkImage::withDefaultMipmaps() const {
return this->withMipmaps(nullptr);
}
sk_sp<SkImage> SkMipmapBuilder::attachTo(const SkImage* src) {
return src->withMipmaps(fMM);
}

View File

@ -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<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());
};