• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CC_RESOURCES_SHARED_BITMAP_H_
6 #define CC_RESOURCES_SHARED_BITMAP_H_
7 
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/shared_memory.h"
11 #include "cc/base/cc_export.h"
12 #include "gpu/command_buffer/common/mailbox.h"
13 #include "ui/gfx/size.h"
14 
15 namespace base { class SharedMemory; }
16 
17 namespace cc {
18 typedef gpu::Mailbox SharedBitmapId;
19 
20 class CC_EXPORT SharedBitmap {
21  public:
22   SharedBitmap(base::SharedMemory* memory,
23                const SharedBitmapId& id,
24                const base::Callback<void(SharedBitmap* bitmap)>& free_callback);
25 
26   SharedBitmap(uint8* pixels,
27                const SharedBitmapId& id,
28                const base::Callback<void(SharedBitmap* bitmap)>& free_callback);
29 
30   ~SharedBitmap();
31 
32   bool operator<(const SharedBitmap& right) const {
33     if (memory_ < right.memory_)
34       return true;
35     if (memory_ > right.memory_)
36       return false;
37     return id_ < right.id_;
38   }
39 
pixels()40   uint8* pixels() { return pixels_; }
41 
memory()42   base::SharedMemory* memory() { return memory_; }
43 
id()44   SharedBitmapId id() { return id_; }
45 
46   // Returns true if the size is valid and false otherwise.
47   static bool SizeInBytes(const gfx::Size& size, size_t* size_in_bytes);
48   // Dies with a CRASH() if the size can not be represented as a positive number
49   // of bytes.
50   static size_t CheckedSizeInBytes(const gfx::Size& size);
51   // Returns the size in bytes but may overflow or return 0. Only do this for
52   // sizes that have already been checked.
53   static size_t UncheckedSizeInBytes(const gfx::Size& size);
54   // Returns true if the size is valid and false otherwise.
55   static bool VerifySizeInBytes(const gfx::Size& size);
56 
57   static SharedBitmapId GenerateId();
58 
59  private:
60   base::SharedMemory* memory_;
61   uint8* pixels_;
62   SharedBitmapId id_;
63   base::Callback<void(SharedBitmap* bitmap)> free_callback_;
64 
65   DISALLOW_COPY_AND_ASSIGN(SharedBitmap);
66 };
67 
68 }  // namespace cc
69 
70 #endif  // CC_RESOURCES_SHARED_BITMAP_H_
71