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

Commit

Permalink
[all] Rationalize style::Image
Browse files Browse the repository at this point in the history
A style has a collection of images, just as it has collections of sources and layers.

* Name things appropriately
* Use std::unique_ptr
  • Loading branch information
jfirebaugh committed Apr 24, 2017
1 parent 6f708ac commit 5dd98df
Show file tree
Hide file tree
Showing 34 changed files with 304 additions and 381 deletions.
8 changes: 3 additions & 5 deletions benchmark/api/query.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/image.hpp>
Expand All @@ -25,10 +25,8 @@ class QueryBenchmark {

map.setStyleJSON(util::read_file("benchmark/fixtures/api/query_style.json"));
map.setLatLngZoom({ 40.726989, -73.992857 }, 15); // Manhattan

auto decoded = decodeImage(util::read_file("benchmark/fixtures/api/default_marker.png"));
auto image = std::make_unique<SpriteImage>(std::move(decoded), 1.0);
map.addImage("test-icon", std::move(image));
map.addImage("test-icon", std::make_unique<style::Image>(
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 cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,11 @@ set(MBGL_CORE_FILES
src/mbgl/shaders/symbol_sdf.hpp

# sprite
include/mbgl/sprite/sprite_image.hpp
src/mbgl/sprite/sprite_atlas.cpp
src/mbgl/sprite/sprite_atlas.hpp
src/mbgl/sprite/sprite_atlas_observer.hpp
src/mbgl/sprite/sprite_atlas_worker.cpp
src/mbgl/sprite/sprite_atlas_worker.hpp
src/mbgl/sprite/sprite_image.cpp
src/mbgl/sprite/sprite_parser.cpp
src/mbgl/sprite/sprite_parser.hpp

Expand All @@ -247,6 +245,7 @@ set(MBGL_CORE_FILES
include/mbgl/style/data_driven_property_value.hpp
include/mbgl/style/filter.hpp
include/mbgl/style/filter_evaluator.hpp
include/mbgl/style/image.hpp
include/mbgl/style/layer.hpp
include/mbgl/style/property_value.hpp
include/mbgl/style/query.hpp
Expand All @@ -264,6 +263,7 @@ set(MBGL_CORE_FILES
src/mbgl/style/data_driven_property_evaluator.hpp
src/mbgl/style/group_by_layout.cpp
src/mbgl/style/group_by_layout.hpp
src/mbgl/style/image.cpp
src/mbgl/style/layer.cpp
src/mbgl/style/layer_impl.cpp
src/mbgl/style/layer_impl.hpp
Expand Down
2 changes: 1 addition & 1 deletion cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ set(MBGL_TEST_FILES

# sprite
test/sprite/sprite_atlas.test.cpp
test/sprite/sprite_image.test.cpp
test/sprite/sprite_parser.test.cpp

# src/mbgl/test
Expand Down Expand Up @@ -95,6 +94,7 @@ set(MBGL_TEST_FILES
test/style/paint_property.test.cpp
test/style/source.test.cpp
test/style/style.test.cpp
test/style/style_image.test.cpp
test/style/style_layer.test.cpp
test/style/style_parser.test.cpp
test/style/tile_source.test.cpp
Expand Down
14 changes: 7 additions & 7 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Backend;
class View;
class FileSource;
class Scheduler;
class SpriteImage;

namespace style {
class Image;
class Source;
class Layer;
} // namespace style
Expand Down Expand Up @@ -154,9 +154,9 @@ class Map : private util::noncopyable {
LatLng latLngForPixel(const ScreenCoordinate&) const;

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

AnnotationID addAnnotation(const Annotation&);
void updateAnnotation(AnnotationID, const Annotation&);
Expand All @@ -174,10 +174,10 @@ class Map : private util::noncopyable {
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
std::unique_ptr<style::Layer> removeLayer(const std::string& layerID);

// Add image, bound to the style
void addImage(const std::string&, std::unique_ptr<const SpriteImage>);
// Images
void addImage(const std::string&, std::unique_ptr<style::Image>);
void removeImage(const std::string&);
const SpriteImage* getImage(const std::string&);
const style::Image* getImage(const std::string&);

// Defaults
std::string getStyleName() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#pragma once

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/image.hpp>

#include <string>
#include <memory>
#include <cstdint>

namespace mbgl {
namespace style {

class SpriteImage : private util::noncopyable {
class Image {
public:
SpriteImage(PremultipliedImage&&, float pixelRatio, bool sdf = false);
Image(PremultipliedImage&&, float pixelRatio, bool sdf = false);

PremultipliedImage image;

Expand All @@ -26,4 +21,5 @@ class SpriteImage : private util::noncopyable {
float getHeight() const { return image.size.height / pixelRatio; }
};

} // namespace style
} // namespace mbgl
12 changes: 6 additions & 6 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/projection.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/filter.hpp>

// Java -> C++ conversion
Expand Down Expand Up @@ -735,12 +735,12 @@ void NativeMapView::addAnnotationIcon(JNIEnv& env, jni::String symbol, jint w, j
}

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

jdouble NativeMapView::getTopOffsetPixelsForAnnotationSymbol(JNIEnv& env, jni::String symbolName) {
return map->getTopOffsetPixelsForAnnotationIcon(jni::Make<std::string>(env, symbolName));
return map->getTopOffsetPixelsForAnnotationImage(jni::Make<std::string>(env, symbolName));
}

jlong NativeMapView::getTransitionDuration(JNIEnv&) {
Expand Down Expand Up @@ -1036,9 +1036,9 @@ 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()));
auto spriteImage = std::make_unique<mbgl::SpriteImage>(std::move(premultipliedImage), float(scale));

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

void NativeMapView::removeImage(JNIEnv& env, jni::String name) {
Expand Down
8 changes: 4 additions & 4 deletions platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <mbgl/map/map.hpp>
#include <mbgl/util/default_styles.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
Expand Down Expand Up @@ -538,7 +538,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_spriteImage);
self.mapView.mbglMap->addImage([name UTF8String], image.mgl_styleImage);
}

- (void)removeImageForName:(NSString *)name
Expand All @@ -558,8 +558,8 @@ - (MGLImage *)imageForName:(NSString *)name
format:@"Cannot get image with nil name."];
}

auto spriteImage = self.mapView.mbglMap->getImage([name UTF8String]);
return spriteImage ? [[MGLImage alloc] initWithMGLSpriteImage:spriteImage] : nil;
auto styleImage = self.mapView.mbglMap->getImage([name UTF8String]);
return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:styleImage] : nil;
}

#pragma mark Style transitions
Expand Down
12 changes: 6 additions & 6 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "glfw_view.hpp"

#include <mbgl/annotation/annotation.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
Expand Down Expand Up @@ -124,7 +124,7 @@ GLFWView::~GLFWView() {

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

void GLFWView::updateAssumedState() {
Expand Down Expand Up @@ -258,8 +258,8 @@ mbgl::Point<double> GLFWView::makeRandomPoint() const {
return { latLng.longitude(), latLng.latitude() };
}

std::shared_ptr<const mbgl::SpriteImage>
GLFWView::makeSpriteImage(int width, int height, float pixelRatio) {
std::unique_ptr<mbgl::style::Image>
GLFWView::makeImage(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 @@ -284,7 +284,7 @@ GLFWView::makeSpriteImage(int width, int height, float pixelRatio) {
}
}

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

void GLFWView::nextOrientation() {
Expand All @@ -301,7 +301,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->addAnnotationIcon(name, makeSpriteImage(22, 22, 1));
map->addAnnotationImage(name, makeImage(22, 22, 1));
spriteIDs.push_back(name);
annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation { makeRandomPoint(), name }));
}
Expand Down
3 changes: 1 addition & 2 deletions platform/glfw/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class GLFWView : public mbgl::View, public mbgl::Backend {

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

void nextOrientation();

Expand Down
7 changes: 3 additions & 4 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/map/mode.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/storage/reachability.h>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/map/backend.hpp>
Expand Down Expand Up @@ -3451,8 +3451,7 @@ - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage
annotationImage.delegate = self;

// add sprite
std::shared_ptr<mbgl::SpriteImage> sprite(annotationImage.image.mgl_spriteImage);
_mbglMap->addAnnotationIcon(iconIdentifier.UTF8String, sprite);
_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);

// 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 Expand Up @@ -4096,7 +4095,7 @@ - (void)annotationImageNeedsRedisplay:(MGLAnnotationImage *)annotationImage

// Remove the old icon from the style.
if ( ! [iconIdentifier isEqualToString:fallbackIconIdentifier]) {
_mbglMap->removeAnnotationIcon(iconIdentifier.UTF8String);
_mbglMap->removeAnnotationImage(iconIdentifier.UTF8String);
}

if (annotationImage.image)
Expand Down
6 changes: 3 additions & 3 deletions platform/ios/src/UIImage+MGLAdditions.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#import <UIKit/UIKit.h>

#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (MGLAdditions)

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

- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage;
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;

@end

Expand Down
14 changes: 7 additions & 7 deletions platform/ios/src/UIImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

@implementation UIImage (MGLAdditions)

- (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)spriteImage
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage
{
CGImageRef image = CGImageFromMGLPremultipliedImage(spriteImage->image.clone());
CGImageRef image = CGImageFromMGLPremultipliedImage(styleImage->image.clone());
if (!image) {
return nil;
}

if (self = [self initWithCGImage:image scale:spriteImage->pixelRatio orientation:UIImageOrientationUp])
if (self = [self initWithCGImage:image scale:styleImage->pixelRatio orientation:UIImageOrientationUp])
{
if (spriteImage->sdf)
if (styleImage->sdf)
{
self = [self imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
Expand All @@ -22,10 +22,10 @@ - (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)sprit
return self;
}

- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage {
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
BOOL isTemplate = self.renderingMode == UIImageRenderingModeAlwaysTemplate;
return std::make_unique<mbgl::SpriteImage>(MGLPremultipliedImageFromCGImage(self.CGImage),
float(self.scale), isTemplate);
return std::make_unique<mbgl::style::Image>(MGLPremultipliedImageFromCGImage(self.CGImage),
float(self.scale), isTemplate);
}

@end
5 changes: 2 additions & 3 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#import <mbgl/util/default_thread_pool.hpp>
#import <mbgl/map/backend.hpp>
#import <mbgl/map/backend_scope.hpp>
#import <mbgl/sprite/sprite_image.hpp>
#import <mbgl/style/image.hpp>
#import <mbgl/storage/default_file_source.hpp>
#import <mbgl/storage/network_status.hpp>
#import <mbgl/math/wrap.hpp>
Expand Down Expand Up @@ -1949,8 +1949,7 @@ - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage {
return;
}

std::shared_ptr<mbgl::SpriteImage> sprite(annotationImage.image.mgl_spriteImage);
_mbglMap->addAnnotationIcon(iconIdentifier.UTF8String, sprite);
_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);

// 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
6 changes: 3 additions & 3 deletions platform/macos/src/NSImage+MGLAdditions.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#import <Cocoa/Cocoa.h>

#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/image.hpp>

NS_ASSUME_NONNULL_BEGIN

@interface NSImage (MGLAdditions)

- (nullable instancetype)initWithMGLPremultipliedImage:(mbgl::PremultipliedImage&&)image;

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

- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage;
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;

@end

Expand Down
Loading

0 comments on commit 5dd98df

Please sign in to comment.