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

Optimize animated annotations #5385

Merged
merged 4 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/mbgl/map/update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ enum class Update : uint8_t {
RecalculateStyle = 1 << 3,
RenderStill = 1 << 4,
Repaint = 1 << 5,
Annotations = 1 << 6,
AnnotationStyle = 1 << 6,
AnnotationData = 1 << 7,
};

inline Update operator| (const Update& lhs, const Update& rhs) {
Expand Down
14 changes: 10 additions & 4 deletions platform/macos/app/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@
<action selector="drawPolygonAndPolyLineAnnotations:" target="-1" id="EhT-CB-gee"/>
</connections>
</menuItem>
<menuItem title="Add Animated Annotation" id="Etf-JN-Aoc">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="drawAnimatedAnnotation:" target="-1" id="CYM-WB-s97"/>
</connections>
</menuItem>
<menuItem title="Remove All Annotations" id="6rC-68-vk0">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down Expand Up @@ -585,7 +591,7 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="109" y="131" width="350" height="84"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="777"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1057"/>
<view key="contentView" id="eA4-n3-qPe">
<rect key="frame" x="0.0" y="0.0" width="350" height="84"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down Expand Up @@ -660,19 +666,19 @@
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES" utility="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="830" y="430" width="400" height="300"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="777"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1057"/>
<view key="contentView" id="8ha-hw-zOD">
<rect key="frame" x="0.0" y="0.0" width="400" height="300"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<scrollView autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Q8b-0e-dLv">
<rect key="frame" x="-1" y="20" width="402" height="281"/>
<clipView key="contentView" id="J9U-Yx-o2S">
<rect key="frame" x="1" y="0.0" width="400" height="280"/>
<rect key="frame" x="1" y="0.0" width="400" height="265"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" autosaveColumns="NO" headerView="MAZ-Iq-hBi" id="Ato-Vu-HYT">
<rect key="frame" x="0.0" y="0.0" width="423" height="257"/>
<rect key="frame" x="0.0" y="0.0" width="423" height="242"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
Expand Down
24 changes: 23 additions & 1 deletion platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ - (IBAction)dropManyPins:(id)sender {
}
}

[NSTimer scheduledTimerWithTimeInterval:1/60
[NSTimer scheduledTimerWithTimeInterval:1.0/60.0
target:self
selector:@selector(dropOneOfManyPins:)
userInfo:annotations
Expand Down Expand Up @@ -405,6 +405,25 @@ - (IBAction)drawPolygonAndPolyLineAnnotations:(id)sender {
[self.mapView addAnnotation:line];
}

- (IBAction)drawAnimatedAnnotation:(id)sender {
DroppedPinAnnotation *annotation = [[DroppedPinAnnotation alloc] init];
[self.mapView addAnnotation:annotation];

[NSTimer scheduledTimerWithTimeInterval:1.0/60.0
target:self
selector:@selector(updateAnimatedAnnotation:)
userInfo:annotation
repeats:YES];
}

- (void)updateAnimatedAnnotation:(NSTimer *)timer {
DroppedPinAnnotation *annotation = timer.userInfo;
double angle = timer.fireDate.timeIntervalSinceReferenceDate;
annotation.coordinate = CLLocationCoordinate2DMake(
sin(angle) * 20,
cos(angle) * 20);
}

#pragma mark Offline packs

- (IBAction)addOfflinePack:(id)sender {
Expand Down Expand Up @@ -613,6 +632,9 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem.action == @selector(dropManyPins:)) {
return YES;
}
if (menuItem.action == @selector(drawAnimatedAnnotation:)) {
return YES;
}
if (menuItem.action == @selector(removeAllAnnotations:)) {
return self.mapView.annotations.count > 0;
}
Expand Down
56 changes: 52 additions & 4 deletions src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation, cons
return id;
}

void AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) {
removeAnnotation(id);
Annotation::visit(annotation, [&] (const auto& annotation_) {
this->add(id, annotation_, maxZoom);
Update AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) {
return Annotation::visit(annotation, [&] (const auto& annotation_) {
return this->update(id, annotation_, maxZoom);
});
}

Expand Down Expand Up @@ -69,6 +68,53 @@ void AnnotationManager::add(const AnnotationID& id, const StyleSourcedAnnotation
std::make_unique<StyleSourcedAnnotationImpl>(id, annotation, maxZoom));
}

Update AnnotationManager::update(const AnnotationID& id, const SymbolAnnotation& annotation, const uint8_t maxZoom) {
auto it = symbolAnnotations.find(id);
if (it == symbolAnnotations.end()) {
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}

Update result = Update::Nothing;
const SymbolAnnotation& existing = it->second->annotation;

if (existing.geometry != annotation.geometry) {
result |= Update::AnnotationData;
}

if (existing.icon != annotation.icon) {
result |= Update::AnnotationData | Update::AnnotationStyle;
}

if (result != Update::Nothing) {
removeAndAdd(id, annotation, maxZoom);
}

return result;
}

Update AnnotationManager::update(const AnnotationID& id, const LineAnnotation& annotation, const uint8_t maxZoom) {
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}

Update AnnotationManager::update(const AnnotationID& id, const FillAnnotation& annotation, const uint8_t maxZoom) {
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}

Update AnnotationManager::update(const AnnotationID& id, const StyleSourcedAnnotation& annotation, const uint8_t maxZoom) {
removeAndAdd(id, annotation, maxZoom);
return Update::AnnotationData | Update::AnnotationStyle;
}

void AnnotationManager::removeAndAdd(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) {
removeAnnotation(id);
Annotation::visit(annotation, [&] (const auto& annotation_) {
this->add(id, annotation_, maxZoom);
});
}

AnnotationIDs AnnotationManager::getPointAnnotationsInBounds(const LatLngBounds& bounds) const {
AnnotationIDs result;

Expand Down Expand Up @@ -133,7 +179,9 @@ void AnnotationManager::updateStyle(Style& style) {
}

obsoleteShapeAnnotationLayers.clear();
}

void AnnotationManager::updateData() {
for (auto& tile : tiles) {
tile->setData(getTileData(tile->id.canonical));
}
Expand Down
13 changes: 11 additions & 2 deletions src/mbgl/annotation/annotation_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <mbgl/annotation/symbol_annotation_impl.hpp>
#include <mbgl/sprite/sprite_store.hpp>
#include <mbgl/sprite/sprite_atlas.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/map/update.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <string>
Expand All @@ -14,6 +14,7 @@

namespace mbgl {

class LatLngBounds;
class AnnotationTile;
class AnnotationTileData;
class SymbolAnnotationImpl;
Expand All @@ -29,7 +30,7 @@ class AnnotationManager : private util::noncopyable {
~AnnotationManager();

AnnotationID addAnnotation(const Annotation&, const uint8_t maxZoom);
void updateAnnotation(const AnnotationID&, const Annotation&, const uint8_t maxZoom);
Update updateAnnotation(const AnnotationID&, const Annotation&, const uint8_t maxZoom);
void removeAnnotation(const AnnotationID&);

AnnotationIDs getPointAnnotationsInBounds(const LatLngBounds&) const;
Expand All @@ -40,6 +41,7 @@ class AnnotationManager : private util::noncopyable {
SpriteAtlas& getSpriteAtlas() { return spriteAtlas; }

void updateStyle(style::Style&);
void updateData();

void addTile(AnnotationTile&);
void removeTile(AnnotationTile&);
Expand All @@ -53,6 +55,13 @@ class AnnotationManager : private util::noncopyable {
void add(const AnnotationID&, const FillAnnotation&, const uint8_t);
void add(const AnnotationID&, const StyleSourcedAnnotation&, const uint8_t);

Update update(const AnnotationID&, const SymbolAnnotation&, const uint8_t);
Update update(const AnnotationID&, const LineAnnotation&, const uint8_t);
Update update(const AnnotationID&, const FillAnnotation&, const uint8_t);
Update update(const AnnotationID&, const StyleSourcedAnnotation&, const uint8_t);

void removeAndAdd(const AnnotationID&, const Annotation&, const uint8_t);

std::unique_ptr<AnnotationTileData> getTileData(const CanonicalTileID&);

AnnotationID nextID = 0;
Expand Down
15 changes: 9 additions & 6 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ void Map::Impl::update() {
// - Hint style sources to notify when all its tiles are loaded;
timePoint = Clock::now();

if (style->loaded && updateFlags & Update::Annotations) {
if (style->loaded && updateFlags & Update::AnnotationStyle) {
annotationManager->updateStyle(*style);
updateFlags |= Update::Classes;
}

if (updateFlags & Update::AnnotationData) {
annotationManager->updateData();
}

if (updateFlags & Update::Classes) {
style->cascade(timePoint, mode);
}
Expand Down Expand Up @@ -338,7 +342,7 @@ void Map::Impl::loadStyleJSON(const std::string& json) {
// force style cascade, causing all pending transitions to complete.
style->cascade(Clock::now(), mode);

updateFlags |= Update::Classes | Update::RecalculateStyle | Update::Annotations;
updateFlags |= Update::Classes | Update::RecalculateStyle | Update::AnnotationStyle;
asyncUpdate.send();
}

Expand Down Expand Up @@ -689,18 +693,17 @@ double Map::getTopOffsetPixelsForAnnotationIcon(const std::string& name) {

AnnotationID Map::addAnnotation(const Annotation& annotation) {
auto result = impl->annotationManager->addAnnotation(annotation, getMaxZoom());
update(Update::Annotations);
update(Update::AnnotationStyle | Update::AnnotationData);
return result;
}

void Map::updateAnnotation(AnnotationID id, const Annotation& annotation) {
impl->annotationManager->updateAnnotation(id, annotation, getMaxZoom());
update(Update::Annotations);
update(impl->annotationManager->updateAnnotation(id, annotation, getMaxZoom()));
}

void Map::removeAnnotation(AnnotationID annotation) {
impl->annotationManager->removeAnnotation(annotation);
update(Update::Annotations);
update(Update::AnnotationStyle | Update::AnnotationData);
}

AnnotationIDs Map::getPointAnnotationsInBounds(const LatLngBounds& bounds) {
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ void Style::removeSource(const std::string& id) {
sources.erase(it);
}

std::vector<std::unique_ptr<Layer>> Style::getLayers() const {
std::vector<std::unique_ptr<Layer>> result;
std::vector<const Layer*> Style::getLayers() const {
std::vector<const Layer*> result;
result.reserve(layers.size());
for (const auto& layer : layers) {
result.push_back(layer->baseImpl->clone());
result.push_back(layer.get());
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Style : public GlyphStoreObserver,
void addSource(std::unique_ptr<Source>);
void removeSource(const std::string& sourceID);

std::vector<std::unique_ptr<Layer>> getLayers() const;
std::vector<const Layer*> getLayers() const;
Layer* getLayer(const std::string& id) const;
void addLayer(std::unique_ptr<Layer>,
optional<std::string> beforeLayerID = {});
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/tile/geojson_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ std::unique_ptr<GeoJSONTileData> convertTile(const mapbox::geojsonvt::Tile& tile
}

GeoJSONTile::GeoJSONTile(const OverscaledTileID& overscaledTileID,
std::string sourceID,
std::string sourceID_,
const style::UpdateParameters& parameters,
mapbox::geojsonvt::GeoJSONVT& geojsonvt)
: GeometryTile(overscaledTileID, sourceID, parameters.style, parameters.mode) {
: GeometryTile(overscaledTileID, sourceID_, parameters.style, parameters.mode) {
setData(convertTile(geojsonvt.getTile(id.canonical.z, id.canonical.x, id.canonical.y)));
}

Expand Down
Loading