Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core, node, darwin, android, qt] Make image ID part of Image
Browse files Browse the repository at this point in the history
More like Source and Layer.
  • Loading branch information
jfirebaugh committed May 16, 2017
1 parent c0f6e5c commit cc1ea75
Show file tree
Hide file tree
Showing 41 changed files with 176 additions and 185 deletions.
2 changes: 1 addition & 1 deletion benchmark/api/query.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryBenchmark {

map.setStyleJSON(util::read_file("benchmark/fixtures/api/query_style.json"));
map.setLatLngZoom({ 40.726989, -73.992857 }, 15); // Manhattan
map.addImage("test-icon", std::make_unique<style::Image>(
map.addImage(std::make_unique<style::Image>("test-icon",
decodeImage(util::read_file("benchmark/fixtures/api/default_marker.png")), 1.0));

mbgl::benchmark::render(map, view);
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Map : private util::noncopyable {
LatLng latLngForPixel(const ScreenCoordinate&) const;

// Annotations
void addAnnotationImage(const std::string&, std::unique_ptr<style::Image>);
void addAnnotationImage(std::unique_ptr<style::Image>);
void removeAnnotationImage(const std::string&);
double getTopOffsetPixelsForAnnotationImage(const std::string&);

Expand All @@ -169,7 +169,7 @@ class Map : private util::noncopyable {
std::unique_ptr<style::Layer> removeLayer(const std::string& layerID);

// Images
void addImage(const std::string&, std::unique_ptr<style::Image>);
void addImage(std::unique_ptr<style::Image>);
void removeImage(const std::string&);
const style::Image* getImage(const std::string&);

Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/style/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#include <mbgl/util/image.hpp>
#include <mbgl/util/immutable.hpp>

#include <string>

namespace mbgl {
namespace style {

class Image {
public:
Image(PremultipliedImage&&, float pixelRatio, bool sdf = false);
Image(std::string id, PremultipliedImage&&, float pixelRatio, bool sdf = false);

std::string getID() const;

const PremultipliedImage& getImage() const;

Expand Down
10 changes: 6 additions & 4 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,8 @@ void NativeMapView::addAnnotationIcon(JNIEnv& env, jni::String symbol, jint w, j
}

jni::GetArrayRegion(env, *jpixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
map->addAnnotationImage(symbolName,
std::make_unique<mbgl::style::Image>(std::move(premultipliedImage), float(scale)));
map->addAnnotationImage(std::make_unique<mbgl::style::Image>(
symbolName, std::move(premultipliedImage), float(scale)));
}

jdouble NativeMapView::getTopOffsetPixelsForAnnotationSymbol(JNIEnv& env, jni::String symbolName) {
Expand Down Expand Up @@ -1037,8 +1037,10 @@ void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::jint w, jni::ji

jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));

map->addImage(jni::Make<std::string>(env, name),
std::make_unique<mbgl::style::Image>(std::move(premultipliedImage), float(scale)));
map->addImage(std::make_unique<mbgl::style::Image>(
jni::Make<std::string>(env, name),
std::move(premultipliedImage),
float(scale)));
}

void NativeMapView::removeImage(JNIEnv& env, jni::String name) {
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ - (void)setImage:(MGLImage *)image forName:(NSString *)name
format:@"Cannot assign image %@ to a nil name.", image];
}

self.mapView.mbglMap->addImage([name UTF8String], image.mgl_styleImage);
self.mapView.mbglMap->addImage([image mgl_styleImageWithIdentifier:name]);
}

- (void)removeImageForName:(NSString *)name
Expand Down
8 changes: 4 additions & 4 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ GLFWView::~GLFWView() {

void GLFWView::setMap(mbgl::Map *map_) {
map = map_;
map->addAnnotationImage("default_marker", makeImage(22, 22, 1));
map->addAnnotationImage(makeImage("default_marker", 22, 22, 1));
}

void GLFWView::updateAssumedState() {
Expand Down Expand Up @@ -255,7 +255,7 @@ mbgl::Point<double> GLFWView::makeRandomPoint() const {
}

std::unique_ptr<mbgl::style::Image>
GLFWView::makeImage(int width, int height, float pixelRatio) {
GLFWView::makeImage(const std::string& id, int width, int height, float pixelRatio) {
const int r = 255 * (double(std::rand()) / RAND_MAX);
const int g = 255 * (double(std::rand()) / RAND_MAX);
const int b = 255 * (double(std::rand()) / RAND_MAX);
Expand All @@ -280,7 +280,7 @@ GLFWView::makeImage(int width, int height, float pixelRatio) {
}
}

return std::make_unique<mbgl::style::Image>(std::move(image), pixelRatio);
return std::make_unique<mbgl::style::Image>(id, std::move(image), pixelRatio);
}

void GLFWView::nextOrientation() {
Expand All @@ -297,7 +297,7 @@ void GLFWView::addRandomCustomPointAnnotations(int count) {
for (int i = 0; i < count; i++) {
static int spriteID = 1;
const auto name = std::string{ "marker-" } + mbgl::util::toString(spriteID++);
map->addAnnotationImage(name, makeImage(22, 22, 1));
map->addAnnotationImage(makeImage(name, 22, 22, 1));
spriteIDs.push_back(name);
annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation { makeRandomPoint(), name }));
}
Expand Down
2 changes: 1 addition & 1 deletion platform/glfw/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GLFWView : public mbgl::View, public mbgl::Backend {

mbgl::Color makeRandomColor() const;
mbgl::Point<double> makeRandomPoint() const;
static std::unique_ptr<mbgl::style::Image> makeImage(int width, int height, float pixelRatio);
static std::unique_ptr<mbgl::style::Image> makeImage(const std::string& id, int width, int height, float pixelRatio);

void nextOrientation();

Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3449,7 +3449,7 @@ - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage
annotationImage.delegate = self;

// add sprite
_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);
_mbglMap->addAnnotationImage([annotationImage.image mgl_styleImageWithIdentifier:iconIdentifier]);

// Create a slop area with a “radius” equal in size to the annotation
// image’s alignment rect, allowing the eventual tap to be on any point
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/UIImage+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage;

- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier;

@end

Expand Down
5 changes: 3 additions & 2 deletions platform/ios/src/UIImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ - (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)style
return self;
}

- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier {
BOOL isTemplate = self.renderingMode == UIImageRenderingModeAlwaysTemplate;
return std::make_unique<mbgl::style::Image>(MGLPremultipliedImageFromCGImage(self.CGImage),
return std::make_unique<mbgl::style::Image>([identifier UTF8String],
MGLPremultipliedImageFromCGImage(self.CGImage),
float(self.scale), isTemplate);
}

Expand Down
2 changes: 1 addition & 1 deletion platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,7 @@ - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage {
return;
}

_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);
_mbglMap->addAnnotationImage([annotationImage.image mgl_styleImageWithIdentifier:iconIdentifier]);

// Create a slop area with a “radius” equal to the annotation image’s entire
// size, allowing the eventual click to be on any point within this image.
Expand Down
2 changes: 1 addition & 1 deletion platform/macos/src/NSImage+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)image;

- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier;

@end

Expand Down
9 changes: 5 additions & 4 deletions platform/macos/src/NSImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)style
return self;
}

- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier {
// Create a bitmap image representation from the image, respecting backing
// scale factor and any resizing done on the image at runtime.
// http://www.cocoabuilder.com/archive/cocoa/82430-nsimage-getting-raw-bitmap-data.html#82431
Expand All @@ -40,9 +40,10 @@ - (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)style

mbgl::PremultipliedImage cPremultipliedImage({ static_cast<uint32_t>(rep.pixelsWide), static_cast<uint32_t>(rep.pixelsHigh) });
std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.bytes(), cPremultipliedImage.data.get());
return std::make_unique<mbgl::style::Image>(std::move(cPremultipliedImage),
(float)(rep.pixelsWide / self.size.width),
[self isTemplate]);
return std::make_unique<mbgl::style::Image>([identifier UTF8String],
std::move(cPremultipliedImage),
(float)(rep.pixelsWide / self.size.width),
[self isTemplate]);
}

@end
2 changes: 1 addition & 1 deletion platform/node/src/node_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {

mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data));
mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage));
nodeMap->map->addImage(*Nan::Utf8String(info[0]), std::make_unique<mbgl::style::Image>(std::move(cPremultipliedImage), pixelRatio));
nodeMap->map->addImage(std::make_unique<mbgl::style::Image>(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio));
}

void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Expand Down
7 changes: 4 additions & 3 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mbgl::Size sanitizedSize(const QSize& size) {
};
};

std::unique_ptr<mbgl::style::Image> toStyleImage(const QImage &sprite) {
std::unique_ptr<mbgl::style::Image> toStyleImage(const QString &id, const QImage &sprite) {
const QImage swapped = sprite
.rgbSwapped()
.convertToFormat(QImage::Format_ARGB32_Premultiplied);
Expand All @@ -87,6 +87,7 @@ std::unique_ptr<mbgl::style::Image> toStyleImage(const QImage &sprite) {
memcpy(img.get(), swapped.constBits(), swapped.byteCount());

return std::make_unique<mbgl::style::Image>(
id.toStdString(),
mbgl::PremultipliedImage(
{ static_cast<uint32_t>(swapped.width()), static_cast<uint32_t>(swapped.height()) },
std::move(img)),
Expand Down Expand Up @@ -1027,7 +1028,7 @@ void QMapboxGL::addAnnotationIcon(const QString &name, const QImage &icon)
{
if (icon.isNull()) return;

d_ptr->mapObj->addAnnotationImage(name.toStdString(), toStyleImage(icon));
d_ptr->mapObj->addAnnotationImage(toStyleImage(name, icon));
}

/*!
Expand Down Expand Up @@ -1329,7 +1330,7 @@ void QMapboxGL::addImage(const QString &id, const QImage &image)
{
if (image.isNull()) return;

d_ptr->mapObj->addImage(id.toStdString(), toStyleImage(image));
d_ptr->mapObj->addImage(toStyleImage(id, image));
}

/*!
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ void AnnotationManager::removeTile(AnnotationTile& tile) {
tiles.erase(&tile);
}

void AnnotationManager::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) {
spriteAtlas.addImage(id, added.impl);
void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
addSpriteImage(spriteImages, std::move(image), [&](style::Image& added) {
spriteAtlas.addImage(added.impl);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/annotation_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AnnotationManager : private util::noncopyable {
Update updateAnnotation(const AnnotationID&, const Annotation&, const uint8_t maxZoom);
void removeAnnotation(const AnnotationID&);

void addImage(const std::string&, std::unique_ptr<style::Image>);
void addImage(std::unique_ptr<style::Image>);
void removeImage(const std::string&);
double getTopOffsetPixelsForImage(const std::string&);
SpriteAtlas& getSpriteAtlas() { return spriteAtlas; }
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {

#pragma mark - Annotations

void Map::addAnnotationImage(const std::string& id, std::unique_ptr<style::Image> image) {
impl->annotationManager->addImage(id, std::move(image));
void Map::addAnnotationImage(std::unique_ptr<style::Image> image) {
impl->annotationManager->addImage(std::move(image));
}

void Map::removeAnnotationImage(const std::string& id) {
Expand Down Expand Up @@ -936,13 +936,13 @@ std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
return removedLayer;
}

void Map::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
void Map::addImage(std::unique_ptr<style::Image> image) {
if (!impl->style) {
return;
}

impl->styleMutated = true;
impl->style->addImage(id, std::move(image));
impl->style->addImage(std::move(image));
}

void Map::removeImage(const std::string& id) {
Expand Down
13 changes: 4 additions & 9 deletions src/mbgl/sprite/sprite_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,20 @@ SpriteAtlas::SpriteAtlas(Size size_, float pixelRatio_)

SpriteAtlas::~SpriteAtlas() = default;

void SpriteAtlas::onSpriteLoaded(Images&& result) {
void SpriteAtlas::onSpriteLoaded() {
markAsLoaded();

for (auto& pair : result) {
addImage(pair.first, pair.second->impl);
}

for (auto requestor : requestors) {
requestor->onIconsAvailable(buildIconMap());
}
requestors.clear();
}

void SpriteAtlas::addImage(const std::string& id, Immutable<style::Image::Impl> image_) {
void SpriteAtlas::addImage(Immutable<style::Image::Impl> image_) {
icons.clear();

auto it = entries.find(id);
auto it = entries.find(image_->id);
if (it == entries.end()) {
entries.emplace(id, Entry { std::move(image_), {}, {} });
entries.emplace(image_->id, Entry { image_, {}, {} });
return;
}

Expand Down
6 changes: 2 additions & 4 deletions src/mbgl/sprite/sprite_atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ class IconRequestor {

class SpriteAtlas : public util::noncopyable {
public:
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;

SpriteAtlas(Size, float pixelRatio);
~SpriteAtlas();

void onSpriteLoaded(Images&&);
void onSpriteLoaded();

void markAsLoaded() {
loaded = true;
Expand All @@ -61,7 +59,7 @@ class SpriteAtlas : public util::noncopyable {
void dumpDebugLogs() const;

const style::Image::Impl* getImage(const std::string&) const;
void addImage(const std::string&, Immutable<style::Image::Impl>);
void addImage(Immutable<style::Image::Impl>);
void removeImage(const std::string&);

void getIcons(IconRequestor& requestor);
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/sprite/sprite_image_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace mbgl {

void addSpriteImage(Images& images, const std::string& id,
void addSpriteImage(Images& images,
std::unique_ptr<style::Image> image_,
std::function<void (style::Image&)> onAdded) {

std::string id = image_->getID();
auto it = images.find(id);
if (it == images.end()) {
// Add new
Expand Down
1 change: 0 additions & 1 deletion src/mbgl/sprite/sprite_image_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace mbgl {
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;

void addSpriteImage(Images&,
const std::string&,
std::unique_ptr<style::Image>,
std::function<void (style::Image&)> onAdded = [] (style::Image&){});

Expand Down
5 changes: 1 addition & 4 deletions src/mbgl/sprite/sprite_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ void SpriteLoader::emitSpriteLoadedIfComplete() {
loader->worker.invoke(&SpriteLoaderWorker::parse, loader->image, loader->json);
}

void SpriteLoader::onParsed(Images&& result) {



void SpriteLoader::onParsed(std::vector<std::unique_ptr<style::Image>>&& result) {
observer->onSpriteLoaded(std::move(result));
}

Expand Down
Loading

0 comments on commit cc1ea75

Please sign in to comment.