Commit 7d439e09 authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Replace use of std container with WTF's equivalents in css_image_generator_value.cc

Replace the use of std::map of std container with WTF::HashMap and
WTF::HashCountedSet in css_image_generator_value.cc, and add
HashTraits for FloatSize to use it as the HashMap key.

Bug: 952716
Change-Id: I0417570fc3cf375ef3ed9ea6759ad26b64b9d56f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1575315Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Miyoung Shin <myid.shin@igalia.com>
Cr-Commit-Position: refs/heads/master@{#653485}
parent 0376201a
......@@ -45,33 +45,26 @@ Image* GeneratedImageCache::GetImage(const FloatSize& size) const {
GeneratedImageMap::const_iterator image_iter = images_.find(size);
if (image_iter == images_.end())
return nullptr;
return image_iter->second.get();
return image_iter->value.get();
}
void GeneratedImageCache::PutImage(const FloatSize& size,
scoped_refptr<Image> image) {
DCHECK(!size.IsEmpty());
images_.insert(
std::pair<FloatSize, scoped_refptr<Image>>(size, std::move(image)));
images_.insert(size, std::move(image));
}
void GeneratedImageCache::AddSize(const FloatSize& size) {
DCHECK(!size.IsEmpty());
ImageSizeCountMap::iterator size_entry = sizes_.find(size);
if (size_entry == sizes_.end())
sizes_.insert(std::pair<FloatSize, unsigned>(size, 1));
else
size_entry->second++;
sizes_.insert(size);
}
void GeneratedImageCache::RemoveSize(const FloatSize& size) {
DCHECK(!size.IsEmpty());
SECURITY_DCHECK(sizes_.find(size) != sizes_.end());
unsigned& count = sizes_[size];
count--;
if (count == 0) {
bool fully_erased = sizes_.erase(size);
if (fully_erased) {
DCHECK(images_.find(size) != images_.end());
sizes_.erase(sizes_.find(size));
images_.erase(images_.find(size));
}
}
......
......@@ -43,24 +43,11 @@ class Image;
class ComputedStyle;
class ImageResourceObserver;
struct FloatSizeCompare {
bool operator()(const FloatSize& lhs, const FloatSize& rhs) const {
if (lhs.Width() < rhs.Width())
return true;
if (lhs.Width() > rhs.Width())
return false;
return lhs.Height() < rhs.Height();
}
};
// Use std::map because the WTF versions require a hashing function, while
// the stl maps require a weak comparison operator that can be defined for
// FloatSize. These maps do not contain many objects because we do not expect
// any particular CSSGeneratedImageValue to have clients at many different
// These maps do not contain many objects because we do not expect any
// particular CSSGeneratedImageValue to have clients at many different
// sizes at any given time.
using ImageSizeCountMap = std::map<FloatSize, unsigned, FloatSizeCompare>;
using GeneratedImageMap =
std::map<FloatSize, scoped_refptr<Image>, FloatSizeCompare>;
using ImageSizeCountMap = HashCountedSet<FloatSize>;
using GeneratedImageMap = HashMap<FloatSize, scoped_refptr<Image>>;
class GeneratedImageCache {
DISALLOW_NEW();
......
......@@ -35,6 +35,7 @@
#include "third_party/blink/renderer/platform/geometry/int_size.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/hash_traits.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/skia/include/core/SkSize.h"
#include "ui/gfx/geometry/size_f.h"
......@@ -80,6 +81,7 @@ class PLATFORM_EXPORT FloatSize {
-std::numeric_limits<float>::epsilon() < height_ &&
height_ < std::numeric_limits<float>::epsilon();
}
bool IsValid() const { return !std::isnan(width_) && !std::isnan(height_); }
bool IsExpressibleAsIntSize() const;
float AspectRatio() const { return width_ / height_; }
......@@ -150,6 +152,9 @@ class PLATFORM_EXPORT FloatSize {
private:
float width_, height_;
friend struct ::WTF::DefaultHash<blink::FloatSize>;
friend struct ::WTF::HashTraits<blink::FloatSize>;
};
inline FloatSize& operator+=(FloatSize& a, const FloatSize& b) {
......@@ -220,4 +225,37 @@ PLATFORM_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const FloatSize&);
// Allows this class to be stored in a HeapVector.
WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::FloatSize)
namespace WTF {
template <>
struct DefaultHash<blink::FloatSize> {
STATIC_ONLY(DefaultHash);
struct Hash {
STATIC_ONLY(Hash);
static unsigned GetHash(const blink::FloatSize& key) {
return HashInts(key.Width(), key.Height());
}
static bool Equal(const blink::FloatSize& a, const blink::FloatSize& b) {
return a == b;
}
static const bool safe_to_compare_to_empty_or_deleted = true;
};
};
template <>
struct HashTraits<blink::FloatSize> : GenericHashTraits<blink::FloatSize> {
STATIC_ONLY(HashTraits);
static const bool kEmptyValueIsZero = true;
static blink::FloatSize EmptyValue() { return blink::FloatSize(); }
static void ConstructDeletedValue(blink::FloatSize& slot, bool) {
float quiet_nan = std::numeric_limits<float>::quiet_NaN();
slot = blink::FloatSize(quiet_nan, quiet_nan);
}
static bool IsDeletedValue(const blink::FloatSize& value) {
return !value.IsValid();
}
};
} // namespace WTF
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_SIZE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment