blob: 48a434affe1309ba155534537d63b381d938bff8 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_SURFACES_SURFACE_ID_H_
#define CC_SURFACES_SURFACE_ID_H_
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include "base/format_macros.h"
#include "base/hash.h"
#include "base/strings/stringprintf.h"
namespace cc {
class SurfaceId {
public:
SurfaceId() : client_id_(0), local_id_(0), nonce_(0) {}
SurfaceId(const SurfaceId& other)
: client_id_(other.client_id_),
local_id_(other.local_id_),
nonce_(other.nonce_) {}
// A SurfaceId consists of three components: client Id, local Id, and nonce.
// A |client_id| is a display compositor service allocated ID that
// uniquely identifies a client.
// A |local_id| is a sequentially allocated ID generated by the display
// compositor client that uniquely identifies a surface.
// A |nonce| is a cryptographically secure random int that makes a SurfaceId
// unguessable by other clients.
SurfaceId(uint32_t client_id, uint32_t local_id, uint64_t nonce)
: client_id_(client_id), local_id_(local_id), nonce_(nonce) {}
bool is_null() const {
return client_id_ == 0 && nonce_ == 0 && local_id_ == 0;
}
size_t hash() const {
size_t interim = base::HashInts(client_id_, local_id_);
return base::HashInts(static_cast<uint64_t>(interim), nonce_);
}
uint32_t client_id() const { return client_id_; }
uint32_t local_id() const { return local_id_; }
uint64_t nonce() const { return nonce_; }
std::string ToString() const {
return base::StringPrintf("%d:%d:%" PRIu64, client_id_, local_id_, nonce_);
}
bool operator==(const SurfaceId& other) const {
return client_id_ == other.client_id_ && local_id_ == other.local_id_ &&
nonce_ == other.nonce_;
}
bool operator!=(const SurfaceId& other) const { return !(*this == other); }
bool operator<(const SurfaceId& other) const {
return std::tie(client_id_, local_id_, nonce_) <
std::tie(other.client_id_, other.local_id_, other.nonce_);
}
private:
// See SurfaceIdAllocator::GenerateId.
uint32_t client_id_;
uint32_t local_id_;
uint64_t nonce_;
};
struct SurfaceIdHash {
size_t operator()(const SurfaceId& key) const { return key.hash(); }
};
} // namespace cc
#endif // CC_SURFACES_SURFACE_ID_H_