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

[core] Fix image requests for already obtained images #15825

Merged
merged 2 commits into from
Oct 17, 2019
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
9 changes: 6 additions & 3 deletions src/mbgl/renderer/image_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,12 @@ void ImageManager::checkMissingAndNotify(ImageRequestor& requestor, const ImageR

auto existingRequestorsIt = requestedImages.find(missingImage);
if (existingRequestorsIt != requestedImages.end()) { // Already asked client about this image.
if (!existingRequestorsIt->second.empty()) { // Still waiting for the client response.
existingRequestorsIt->second.emplace(requestorPtr);
requestor.addPendingRequest(missingImage);
std::set<ImageRequestor*>& existingRequestors = existingRequestorsIt->second;
if (!existingRequestors.empty() &&
(*existingRequestors.begin())
->hasPendingRequest(missingImage)) { // Still waiting for the client response for this image.
requestorPtr->addPendingRequest(missingImage);
existingRequestors.emplace(requestorPtr);
continue;
}
// Unlike icons, pattern changes are not caught
Expand Down
3 changes: 1 addition & 2 deletions src/mbgl/renderer/image_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ class ImageRequestor {
virtual void onImagesAvailable(ImageMap icons, ImageMap patterns, ImageVersionMap versionMap, uint64_t imageCorrelationID) = 0;

void addPendingRequest(const std::string& imageId) { pendingRequests.insert(imageId); }

bool hasPendingRequest(const std::string& imageId) const { return pendingRequests.count(imageId); }
bool hasPendingRequests() const { return !pendingRequests.empty(); }

void removePendingRequest(const std::string& imageId) { pendingRequests.erase(imageId); }

private:
Expand Down
14 changes: 12 additions & 2 deletions test/renderer/image_manager.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,28 @@ TEST(ImageManager, OnStyleImageMissingBeforeSpriteLoaded) {

// Repeated request of the same image shall not result another
// `ImageManagerObserver.onStyleImageMissing()` call.
imageManager.getImages(requestor, std::make_pair(dependencies, imageCorrelationID));
imageManager.getImages(requestor, std::make_pair(dependencies, ++imageCorrelationID));
runLoop.runOnce();

EXPECT_EQ(observer.count, 1);

// Request for updated dependencies must be dispatched to the
// observer.
dependencies.emplace("post", ImageType::Icon);
imageManager.getImages(requestor, std::make_pair(dependencies, imageCorrelationID));
imageManager.getImages(requestor, std::make_pair(dependencies, ++imageCorrelationID));
runLoop.runOnce();

EXPECT_EQ(observer.count, 2);

// Another requestor shall not have pending requests for already obtained images.
StubImageRequestor anotherRequestor(imageManager);
imageManager.getImages(anotherRequestor, std::make_pair(dependencies, ++imageCorrelationID));
ASSERT_FALSE(anotherRequestor.hasPendingRequests());

dependencies.emplace("unfamiliar", ImageType::Icon);
imageManager.getImages(anotherRequestor, std::make_pair(dependencies, ++imageCorrelationID));
EXPECT_TRUE(anotherRequestor.hasPendingRequests());
EXPECT_TRUE(anotherRequestor.hasPendingRequest("unfamiliar"));
}

TEST(ImageManager, OnStyleImageMissingAfterSpriteLoaded) {
Expand Down