1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cc/paint/element_id.h"
6 
7 #include <inttypes.h>
8 #include <limits>
9 #include <ostream>
10 #include <string>
11 
12 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/traced_value.h"
14 
15 namespace cc {
16 
17 const ElementIdType ElementId::kInvalidElementId = 0;
18 const ElementIdType ElementId::kReservedElementId =
19     std::numeric_limits<ElementIdType>::max();
20 
21 // static
IsValid(ElementIdType id)22 bool ElementId::IsValid(ElementIdType id) {
23   return id != kInvalidElementId;
24 }
25 
LayerIdToElementIdForTesting(int layer_id)26 ElementId LayerIdToElementIdForTesting(int layer_id) {
27   return ElementId(std::numeric_limits<int>::max() - layer_id);
28 }
29 
AddToTracedValue(base::trace_event::TracedValue * res) const30 void ElementId::AddToTracedValue(base::trace_event::TracedValue* res) const {
31   res->BeginDictionary("element_id");
32   res->SetInteger("id_", id_);
33   res->EndDictionary();
34 }
35 
GetStableId() const36 ElementIdType ElementId::GetStableId() const {
37   return id_;
38 }
39 
ToString() const40 std::string ElementId::ToString() const {
41   return base::StringPrintf("(%" PRIu64 ")", id_);
42 }
43 
operator ()(ElementId key) const44 size_t ElementIdHash::operator()(ElementId key) const {
45   return std::hash<int>()(key.id_);
46 }
47 
operator <<(std::ostream & out,const ElementId & id)48 std::ostream& operator<<(std::ostream& out, const ElementId& id) {
49   return out << id.ToString();
50 }
51 
52 }  // namespace cc
53