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"
13 SharedBitmap::SharedBitmap(
14 base::SharedMemory
* memory
,
15 const SharedBitmapId
& id
,
16 const base::Callback
<void(SharedBitmap
* bitmap
)>& free_callback
)
18 pixels_(static_cast<uint8
*>(memory_
->memory())),
20 free_callback_(free_callback
) {
23 SharedBitmap::SharedBitmap(
25 const SharedBitmapId
& id
,
26 const base::Callback
<void(SharedBitmap
* bitmap
)>& free_callback
)
27 : memory_(NULL
), pixels_(pixels
), id_(id
), free_callback_(free_callback
) {
30 SharedBitmap::~SharedBitmap() { free_callback_
.Run(this); }
33 bool SharedBitmap::SizeInBytes(const gfx::Size
& size
, size_t* size_in_bytes
) {
36 base::CheckedNumeric
<size_t> s
= 4;
41 *size_in_bytes
= s
.ValueOrDie();
46 size_t SharedBitmap::CheckedSizeInBytes(const gfx::Size
& size
) {
47 CHECK(!size
.IsEmpty());
48 base::CheckedNumeric
<size_t> s
= 4;
51 return s
.ValueOrDie();
55 size_t SharedBitmap::UncheckedSizeInBytes(const gfx::Size
& size
) {
56 DCHECK(VerifySizeInBytes(size
));
64 bool SharedBitmap::VerifySizeInBytes(const gfx::Size
& size
) {
67 base::CheckedNumeric
<size_t> s
= 4;
74 SharedBitmapId
SharedBitmap::GenerateId() {
76 // Needs cryptographically-secure random numbers.
77 base::RandBytes(id
.name
, sizeof(id
.name
));