Skip to content

Commit

Permalink
[cc] Use base::Contains() instead of std::find() in //cc
Browse files Browse the repository at this point in the history
Use base::Contains() instead of std::find() where appropriate in
//cc.

Bug: 561800
Change-Id: If593e0566fbbc89b8268c61577d2404fb8546c42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4788388
Commit-Queue: Ho Cheung <uioptt24@gmail.com>
Reviewed-by: ccameron chromium <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1185500}
  • Loading branch information
gz83 authored and Chromium LUCI CQ committed Aug 19, 2023
1 parent ea194a4 commit 2f5fb03
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 54 deletions.
2 changes: 1 addition & 1 deletion cc/layers/picture_layer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ PictureLayerImpl::InvalidateRegionForImages(
void PictureLayerImpl::SetPaintWorkletRecord(
scoped_refptr<const PaintWorkletInput> input,
PaintRecord record) {
DCHECK(paint_worklet_records_.find(input) != paint_worklet_records_.end());
DCHECK(base::Contains(paint_worklet_records_, input));
paint_worklet_records_[input].second = std::move(record);
}

Expand Down
18 changes: 9 additions & 9 deletions cc/layers/texture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/containers/cxx20_erase.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
Expand Down Expand Up @@ -255,17 +256,16 @@ void TextureLayer::PushPropertiesTo(
SharedBitmapIdRegistration TextureLayer::RegisterSharedBitmapId(
const viz::SharedBitmapId& id,
scoped_refptr<CrossThreadSharedBitmap> bitmap) {
DCHECK(to_register_bitmaps_.Read(*this).find(id) ==
to_register_bitmaps_.Read(*this).end());
DCHECK(registered_bitmaps_.Read(*this).find(id) ==
registered_bitmaps_.Read(*this).end());
DCHECK(!base::Contains(to_register_bitmaps_.Read(*this), id));
DCHECK(!base::Contains(registered_bitmaps_.Read(*this), id));
to_register_bitmaps_.Write(*this)[id] = std::move(bitmap);
base::Erase(to_unregister_bitmap_ids_.Write(*this), id);
// This does not SetNeedsCommit() to be as lazy as possible. Notifying a
// SharedBitmapId is not needed until it is used, and using it will require
// a commit, so we can wait for that commit before forwarding the
// notification instead of forcing it to happen as a side effect of this
// method.

// This does not SetNeedsCommit() to be as lazy as possible.
// Notifying a SharedBitmapId is not needed until it is used,
// and using it will require a commit, so we can wait for that commit
// before forwarding the notification instead of forcing it to happen
// as a side effect of this method.
SetNeedsPushProperties();
return SharedBitmapIdRegistration(weak_ptr_factory_.GetMutableWeakPtr(), id);
}
Expand Down
12 changes: 7 additions & 5 deletions cc/layers/texture_layer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/cxx20_erase.h"
#include "base/logging.h"
#include "cc/trees/layer_tree_frame_sink.h"
Expand Down Expand Up @@ -254,10 +255,10 @@ void TextureLayerImpl::RegisterSharedBitmapId(
// If a TextureLayer leaves and rejoins a tree without the TextureLayerImpl
// being destroyed, then it will re-request registration of ids that are still
// registered on the impl side, so we can just ignore these requests.
if (registered_bitmaps_.find(id) == registered_bitmaps_.end()) {
// If this is a pending layer, these will be moved to the active layer when
// we PushPropertiesTo(). Otherwise, we don't need to notify these to the
// LayerTreeFrameSink until we're going to use them, so defer it until
if (!base::Contains(registered_bitmaps_, id)) {
// If this is a pending layer, these will be moved to the active layer
// when we PushPropertiesTo(). Otherwise, we don't need to notify these to
// the LayerTreeFrameSink until we're going to use them, so defer it until
// AppendQuads().
to_register_bitmaps_[id] = std::move(bitmap);
}
Expand All @@ -267,8 +268,9 @@ void TextureLayerImpl::RegisterSharedBitmapId(
void TextureLayerImpl::UnregisterSharedBitmapId(viz::SharedBitmapId id) {
if (IsActive()) {
LayerTreeFrameSink* sink = layer_tree_impl()->layer_tree_frame_sink();
if (sink && registered_bitmaps_.find(id) != registered_bitmaps_.end())
if (sink && base::Contains(registered_bitmaps_, id)) {
sink->DidDeleteSharedBitmap(id);
}
to_register_bitmaps_.erase(id);
registered_bitmaps_.erase(id);
} else {
Expand Down
17 changes: 9 additions & 8 deletions cc/paint/discardable_image_map_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <memory>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/memory/ref_counted.h"
#include "base/test/gtest_util.h"
#include "base/values.h"
Expand Down Expand Up @@ -1011,10 +1012,10 @@ TEST_F(DiscardableImageMapTest, DecodingModeHintsBasic) {
content_layer_client.PaintContentsToDisplayList();
display_list->GenerateDiscardableImagesMetadata();
auto decode_hints = display_list->TakeDecodingModeMap();
ASSERT_EQ(decode_hints.size(), 3u);
ASSERT_TRUE(decode_hints.find(1) != decode_hints.end());
ASSERT_TRUE(decode_hints.find(2) != decode_hints.end());
ASSERT_TRUE(decode_hints.find(3) != decode_hints.end());
EXPECT_EQ(decode_hints.size(), 3u);
EXPECT_TRUE(base::Contains(decode_hints, 1));
EXPECT_TRUE(base::Contains(decode_hints, 2));
EXPECT_TRUE(base::Contains(decode_hints, 3));
EXPECT_EQ(decode_hints[1], PaintImage::DecodingMode::kUnspecified);
EXPECT_EQ(decode_hints[2], PaintImage::DecodingMode::kAsync);
EXPECT_EQ(decode_hints[3], PaintImage::DecodingMode::kSync);
Expand Down Expand Up @@ -1077,10 +1078,10 @@ TEST_F(DiscardableImageMapTest, DecodingModeHintsDuplicates) {
display_list->GenerateDiscardableImagesMetadata();

auto decode_hints = display_list->TakeDecodingModeMap();
ASSERT_EQ(decode_hints.size(), 3u);
ASSERT_TRUE(decode_hints.find(1) != decode_hints.end());
ASSERT_TRUE(decode_hints.find(2) != decode_hints.end());
ASSERT_TRUE(decode_hints.find(3) != decode_hints.end());
EXPECT_EQ(decode_hints.size(), 3u);
EXPECT_TRUE(base::Contains(decode_hints, 1));
EXPECT_TRUE(base::Contains(decode_hints, 2));
EXPECT_TRUE(base::Contains(decode_hints, 3));
// 1 was unspecified and async, so the result should be unspecified.
EXPECT_EQ(decode_hints[1], PaintImage::DecodingMode::kUnspecified);
// 2 was unspecified and sync, so the result should be sync.
Expand Down
4 changes: 2 additions & 2 deletions cc/raster/staging_buffer_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ bool StagingBufferPool::OnMemoryDump(

void StagingBufferPool::AddStagingBuffer(const StagingBuffer* staging_buffer,
viz::SharedImageFormat format) {
DCHECK(buffers_.find(staging_buffer) == buffers_.end());
DCHECK(!base::Contains(buffers_, staging_buffer));
buffers_.insert(staging_buffer);
int buffer_usage_in_bytes = format.EstimatedSizeInBytes(staging_buffer->size);
staging_buffer_usage_in_bytes_ += buffer_usage_in_bytes;
}

void StagingBufferPool::RemoveStagingBuffer(
const StagingBuffer* staging_buffer) {
DCHECK(buffers_.find(staging_buffer) != buffers_.end());
DCHECK(base::Contains(buffers_, staging_buffer));
buffers_.erase(staging_buffer);
int buffer_usage_in_bytes =
staging_buffer->format.EstimatedSizeInBytes(staging_buffer->size);
Expand Down
2 changes: 1 addition & 1 deletion cc/raster/task_graph_work_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TaskGraphWorkQueue::PrioritizedTask::~PrioritizedTask() = default;

NamespaceToken TaskGraphWorkQueue::GenerateNamespaceToken() {
NamespaceToken token(next_namespace_id_++);
DCHECK(namespaces_.find(token) == namespaces_.end());
DCHECK(!base::Contains(namespaces_, token));
return token;
}

Expand Down
2 changes: 1 addition & 1 deletion cc/resources/resource_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void ResourcePool::OnResourceReleased(size_t unique_id,
// while it was still in use by the ResourcePool client. That would prevent
// the client from being able to use the ResourceId on the InUsePoolResource,
// which would be problematic!
DCHECK(in_use_resources_.find(unique_id) == in_use_resources_.end());
DCHECK(!base::Contains(in_use_resources_, unique_id));

// TODO(danakj): Should busy_resources be a map?
auto busy_it =
Expand Down
3 changes: 1 addition & 2 deletions cc/resources/ui_resource_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ UIResourceId UIResourceManager::CreateUIResource(UIResourceClient* client) {
DCHECK(client);

UIResourceId next_id = next_ui_resource_id_++;
DCHECK(ui_resource_client_map_.find(next_id) ==
ui_resource_client_map_.end());
DCHECK(!base::Contains(ui_resource_client_map_, next_id));

bool resource_lost = false;
UIResourceRequest request(UIResourceRequest::UI_RESOURCE_CREATE, next_id,
Expand Down
3 changes: 2 additions & 1 deletion cc/test/animation_timelines_test_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <utility>

#include "base/containers/contains.h"
#include "base/memory/ptr_util.h"
#include "cc/animation/animation.h"
#include "cc/animation/animation_events.h"
Expand Down Expand Up @@ -196,7 +197,7 @@ void TestHostClient::RegisterElementId(ElementId element_id,
ElementIdToTestLayer& layers_in_tree = list_type == ElementListType::ACTIVE
? layers_in_active_tree_
: layers_in_pending_tree_;
DCHECK(layers_in_tree.find(element_id) == layers_in_tree.end());
DCHECK(!base::Contains(layers_in_tree, element_id));
layers_in_tree[element_id] = TestLayer::Create();
}

Expand Down
12 changes: 8 additions & 4 deletions cc/test/fake_paint_image_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <utility>

#include "base/containers/contains.h"

namespace cc {

FakePaintImageGenerator::FakePaintImageGenerator(
Expand Down Expand Up @@ -54,10 +56,11 @@ bool FakePaintImageGenerator::GetPixels(SkPixmap dst_pixmap,
image_pixmap_ = SkPixmap(dst_info, image_backing_memory_.data(),
dst_info.minRowBytes());
}
if (frames_decoded_count_.find(frame_index) == frames_decoded_count_.end())
if (!base::Contains(frames_decoded_count_, frame_index)) {
frames_decoded_count_[frame_index] = 1;
else
} else {
frames_decoded_count_[frame_index]++;
}
CHECK(image_pixmap_.scalePixels(
dst_pixmap, {SkFilterMode::kLinear, SkMipmapMode::kNearest}));
decode_infos_.push_back(dst_info);
Expand Down Expand Up @@ -91,10 +94,11 @@ bool FakePaintImageGenerator::GetYUVAPlanes(
memcpy(pixmaps.plane(i).writable_addr(), src_plane_memory, plane_sizes[i]);
src_plane_memory += plane_sizes[i];
}
if (frames_decoded_count_.find(frame_index) == frames_decoded_count_.end())
if (!base::Contains(frames_decoded_count_, frame_index)) {
frames_decoded_count_[frame_index] = 1;
else
} else {
frames_decoded_count_[frame_index]++;
}
return true;
}

Expand Down
13 changes: 9 additions & 4 deletions cc/test/transfer_cache_test_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/span.h"

namespace cc {
Expand Down Expand Up @@ -85,16 +86,20 @@ ServiceTransferCacheEntry* TransferCacheTestHelper::GetEntryInternal(
TransferCacheEntryType type,
uint32_t id) {
auto key = std::make_pair(type, id);
if (locked_entries_.count(key) + local_entries_.count(key) == 0)
if (!base::Contains(locked_entries_, key) &&
!base::Contains(local_entries_, key)) {
return nullptr;
if (entries_.find(key) == entries_.end())
}
if (!base::Contains(entries_, key)) {
return nullptr;
}
return entries_[key].get();
}

bool TransferCacheTestHelper::LockEntryInternal(const EntryKey& key) {
if (entries_.find(key) == entries_.end())
if (!base::Contains(entries_, key)) {
return false;
}

locked_entries_.insert(key);
EnforceLimits();
Expand All @@ -105,7 +110,7 @@ uint32_t TransferCacheTestHelper::CreateEntryInternal(
const ClientTransferCacheEntry& client_entry,
char* memory) {
auto key = std::make_pair(client_entry.Type(), client_entry.Id());
DCHECK(entries_.find(key) == entries_.end());
DCHECK(!base::Contains(entries_, key));

// Serialize data.
uint32_t size = client_entry.SerializedSize();
Expand Down
4 changes: 2 additions & 2 deletions cc/tiles/checker_image_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <utility>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
Expand Down Expand Up @@ -289,8 +290,7 @@ bool CheckerImageTracker::ShouldCheckerImage(const DrawImage& draw_image,

// If the image is pending invalidation, continue checkering it. All tiles
// for these images will be invalidated on the next pending tree.
if (images_pending_invalidation_.find(image_id) !=
images_pending_invalidation_.end()) {
if (base::Contains(images_pending_invalidation_, image_id)) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion cc/tiles/checker_image_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unordered_map>
#include <vector>

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "cc/cc_export.h"
#include "cc/paint/image_id.h"
Expand Down Expand Up @@ -117,7 +118,7 @@ class CC_EXPORT CheckerImageTracker {
}
PaintImage::DecodingMode get_decoding_mode_hint_for_testing(
PaintImage::Id id) {
CHECK(decoding_mode_map_.find(id) != decoding_mode_map_.end());
DCHECK(base::Contains(decoding_mode_map_, id));
return decoding_mode_map_[id];
}

Expand Down
7 changes: 4 additions & 3 deletions cc/tiles/gpu_image_decode_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/span.h"
#include "base/debug/alias.h"
#include "base/feature_list.h"
Expand Down Expand Up @@ -2376,10 +2377,10 @@ bool GpuImageDecodeCache::NeedsDarkModeFilter(const DrawImage& draw_image,
}

// Dark mode filter is already generated and cached.
if (image_data->decode.dark_mode_color_filter_cache.find(
draw_image.src_rect()) !=
image_data->decode.dark_mode_color_filter_cache.end())
if (base::Contains(image_data->decode.dark_mode_color_filter_cache,
draw_image.src_rect())) {
return false;
}

return true;
}
Expand Down
12 changes: 7 additions & 5 deletions cc/tiles/gpu_image_decode_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
Expand Down Expand Up @@ -64,19 +65,19 @@ class FakeDiscardableManager {
public:
void SetGLES2Interface(viz::TestGLES2Interface* gl) { gl_ = gl; }
void Initialize(GLuint texture_id) {
EXPECT_EQ(textures_.end(), textures_.find(texture_id));
DCHECK(!base::Contains(textures_, texture_id));
textures_[texture_id] = kHandleLockedStart;
live_textures_count_++;
}
void Unlock(GLuint texture_id) {
EXPECT_NE(textures_.end(), textures_.find(texture_id));
DCHECK(base::Contains(textures_, texture_id));
ExpectLocked(texture_id);
textures_[texture_id]--;
}
bool Lock(GLuint texture_id) {
EnforceLimit();

EXPECT_NE(textures_.end(), textures_.find(texture_id));
DCHECK(base::Contains(textures_, texture_id));
if (textures_[texture_id] >= kHandleUnlocked) {
textures_[texture_id]++;
return true;
Expand All @@ -85,8 +86,9 @@ class FakeDiscardableManager {
}

void DeleteTexture(GLuint texture_id) {
if (textures_.end() == textures_.find(texture_id))
if (!base::Contains(textures_, texture_id)) {
return;
}

ExpectLocked(texture_id);
textures_[texture_id] = kHandleDeleted;
Expand All @@ -100,7 +102,7 @@ class FakeDiscardableManager {
size_t live_textures_count() const { return live_textures_count_; }

void ExpectLocked(GLuint texture_id) {
EXPECT_TRUE(textures_.end() != textures_.find(texture_id));
EXPECT_TRUE(base::Contains(textures_, texture_id));

// Any value > kHandleLockedStart represents a locked texture. As we
// increment this value with each lock, we need the entire range and can't
Expand Down
3 changes: 2 additions & 1 deletion cc/tiles/picture_layer_tiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <set>

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/numerics/safe_conversions.h"
#include "base/trace_event/trace_event.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ Tile* PictureLayerTiling::CreateTile(const Tile::CreateInfo& info) {
const int i = info.tiling_i_index;
const int j = info.tiling_j_index;
TileMapKey key(i, j);
DCHECK(tiles_.find(key) == tiles_.end());
DCHECK(!base::Contains(tiles_, key));

if (!raster_source_->IntersectsRect(info.enclosing_layer_rect, *client_))
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion cc/tiles/tile_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ std::unique_ptr<Tile> TileManager::CreateTile(const Tile::CreateInfo& info,
DCHECK(tile_task_manager_);
std::unique_ptr<Tile> tile(
new Tile(this, info, layer_id, source_frame_number, flags));
DCHECK(tiles_.find(tile->id()) == tiles_.end());
DCHECK(!base::Contains(tiles_, tile->id()));

tiles_[tile->id()] = tile.get();
return tile;
Expand Down

0 comments on commit 2f5fb03

Please sign in to comment.