Add ICU message format support
[chromium-blink-merge.git] / cc / resources / shared_bitmap.cc
blobfc15736315b756f438ebd8bc3cb4afbba17458c8
1 // Copyright 2013 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.
5 #include "cc/resources/shared_bitmap.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "base/rand_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
13 namespace cc {
15 SharedBitmap::SharedBitmap(uint8* pixels, const SharedBitmapId& id)
16 : pixels_(pixels), id_(id) {
19 SharedBitmap::~SharedBitmap() {
22 // static
23 bool SharedBitmap::SizeInBytes(const gfx::Size& size, size_t* size_in_bytes) {
24 if (size.IsEmpty())
25 return false;
26 base::CheckedNumeric<size_t> s = 4;
27 s *= size.width();
28 s *= size.height();
29 if (!s.IsValid())
30 return false;
31 *size_in_bytes = s.ValueOrDie();
32 return true;
35 // static
36 size_t SharedBitmap::CheckedSizeInBytes(const gfx::Size& size) {
37 CHECK(!size.IsEmpty());
38 base::CheckedNumeric<size_t> s = 4;
39 s *= size.width();
40 s *= size.height();
41 return s.ValueOrDie();
44 // static
45 size_t SharedBitmap::UncheckedSizeInBytes(const gfx::Size& size) {
46 DCHECK(VerifySizeInBytes(size));
47 size_t s = 4;
48 s *= size.width();
49 s *= size.height();
50 return s;
53 // static
54 bool SharedBitmap::VerifySizeInBytes(const gfx::Size& size) {
55 if (size.IsEmpty())
56 return false;
57 base::CheckedNumeric<size_t> s = 4;
58 s *= size.width();
59 s *= size.height();
60 return s.IsValid();
63 // static
64 SharedBitmapId SharedBitmap::GenerateId() {
65 SharedBitmapId id;
66 // Needs cryptographically-secure random numbers.
67 base::RandBytes(id.name, sizeof(id.name));
68 return id;
71 base::trace_event::MemoryAllocatorDumpGuid GetSharedBitmapGUIDForTracing(
72 const SharedBitmapId& bitmap_id) {
73 auto bitmap_id_hex = base::HexEncode(bitmap_id.name, sizeof(bitmap_id.name));
74 return base::trace_event::MemoryAllocatorDumpGuid(
75 base::StringPrintf("sharedbitmap-x-process/%s", bitmap_id_hex.c_str()));
78 } // namespace cc