[core][ios][android][macos] Use premultiplied image directly for RasterTile and ImageSource, un-premultiply in the shader for blending

This commit is contained in:
Asheem Mamoowala
2017-07-11 14:31:05 -07:00
parent 1d15ed64dc
commit 8ae7010546
15 changed files with 53 additions and 34 deletions

View File

@@ -9,11 +9,11 @@ namespace mbgl {
using namespace style;
RasterBucket::RasterBucket(UnassociatedImage&& image_) {
image = std::make_shared<UnassociatedImage>(std::move(image_));
RasterBucket::RasterBucket(PremultipliedImage&& image_) {
image = std::make_shared<PremultipliedImage>(std::move(image_));
}
RasterBucket::RasterBucket(std::shared_ptr<UnassociatedImage> image_): image(image_) {
RasterBucket::RasterBucket(std::shared_ptr<PremultipliedImage> image_): image(image_) {
}
void RasterBucket::upload(gl::Context& context) {
@@ -40,7 +40,7 @@ void RasterBucket::clear() {
uploaded = false;
}
void RasterBucket::setImage(std::shared_ptr<UnassociatedImage> image_) {
void RasterBucket::setImage(std::shared_ptr<PremultipliedImage> image_) {
image = std::move(image_);
texture = {};
uploaded = false;

View File

@@ -13,8 +13,8 @@ namespace mbgl {
class RasterBucket : public Bucket {
public:
RasterBucket(UnassociatedImage&&);
RasterBucket(std::shared_ptr<UnassociatedImage>);
RasterBucket(PremultipliedImage&&);
RasterBucket(std::shared_ptr<PremultipliedImage>);
void upload(gl::Context&) override;
void render(Painter&, PaintParameters&, const RenderLayer&, const RenderTile&) override;
void render(Painter& painter,
@@ -24,8 +24,8 @@ public:
bool hasData() const override;
void clear();
void setImage(std::shared_ptr<UnassociatedImage>);
std::shared_ptr<UnassociatedImage> image;
void setImage(std::shared_ptr<PremultipliedImage>);
std::shared_ptr<PremultipliedImage> image;
optional<gl::Texture> texture;
// Bucket specific vertices are used for Image Sources only

View File

@@ -82,7 +82,7 @@ void RenderImageSource::update(Immutable<style::Source::Impl> baseImpl_,
std::swap(baseImpl, baseImpl_);
auto coords = impl().getCoordinates();
std::shared_ptr<UnassociatedImage> image = impl().getImage();
std::shared_ptr<PremultipliedImage> image = impl().getImage();
if (!image || !image->valid()) {
enabled = false;

View File

@@ -45,6 +45,12 @@ void main() {
// read and cross-fade colors from the main and parent tiles
vec4 color0 = texture2D(u_image0, v_pos0);
vec4 color1 = texture2D(u_image1, v_pos1);
if (color0.a > 0.0) {
color0.rgb = color0.rgb / color0.a;
}
if (color1.a > 0.0) {
color1.rgb = color1.rgb / color1.a;
}
vec4 color = mix(color0, color1, u_fade_t);
color.a *= u_opacity;
vec3 rgb = color.rgb;

View File

@@ -37,7 +37,7 @@ void ImageSource::setURL(const std::string& url_) {
}
}
void ImageSource::setImage(UnassociatedImage&& image_) {
void ImageSource::setImage(PremultipliedImage&& image_) {
url = {};
if (req) {
req.reset();
@@ -70,8 +70,7 @@ void ImageSource::loadDescription(FileSource& fileSource) {
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty image url")));
} else {
try {
UnassociatedImage image = util::unpremultiply(decodeImage(*res.data));
baseImpl = makeMutable<Impl>(impl(), std::move(image));
baseImpl = makeMutable<Impl>(impl(), decodeImage(*res.data));
} catch (...) {
observer->onSourceError(*this, std::current_exception());
}

View File

@@ -15,14 +15,14 @@ ImageSource::Impl::Impl(const Impl& other, std::array<LatLng, 4> coords_)
image(other.image) {
}
ImageSource::Impl::Impl(const Impl& rhs, UnassociatedImage&& image_)
ImageSource::Impl::Impl(const Impl& rhs, PremultipliedImage&& image_)
: Source::Impl(rhs),
coords(rhs.coords),
image(std::make_shared<UnassociatedImage>(std::move(image_))) {
image(std::make_shared<PremultipliedImage>(std::move(image_))) {
}
ImageSource::Impl::~Impl() = default;
std::shared_ptr<UnassociatedImage> ImageSource::Impl::getImage() const {
std::shared_ptr<PremultipliedImage> ImageSource::Impl::getImage() const {
return image;
}

View File

@@ -13,17 +13,17 @@ class ImageSource::Impl : public Source::Impl {
public:
Impl(std::string id, std::array<LatLng, 4> coords);
Impl(const Impl& rhs, std::array<LatLng, 4> coords);
Impl(const Impl& rhs, UnassociatedImage&& image);
Impl(const Impl& rhs, PremultipliedImage&& image);
~Impl() final;
std::shared_ptr<UnassociatedImage> getImage() const;
std::shared_ptr<PremultipliedImage> getImage() const;
std::array<LatLng, 4> getCoordinates() const;
optional<std::string> getAttribution() const final;
private:
std::array<LatLng, 4> coords;
std::shared_ptr<UnassociatedImage> image;
std::shared_ptr<PremultipliedImage> image;
};
} // namespace style

View File

@@ -17,7 +17,7 @@ void RasterTileWorker::parse(std::shared_ptr<const std::string> data) {
}
try {
auto bucket = std::make_unique<RasterBucket>(util::unpremultiply(decodeImage(*data)));
auto bucket = std::make_unique<RasterBucket>(decodeImage(*data));
parent.invoke(&RasterTile::onParsed, std::move(bucket));
} catch (...) {
parent.invoke(&RasterTile::onError, std::current_exception());