cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / cc / resources / picture_pile_base.h
blob07540cbf37f508d4ad23ac037ce6def09f641aab
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 #ifndef CC_RESOURCES_PICTURE_PILE_BASE_H_
6 #define CC_RESOURCES_PICTURE_PILE_BASE_H_
8 #include <bitset>
9 #include <list>
10 #include <utility>
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "cc/base/cc_export.h"
15 #include "cc/base/region.h"
16 #include "cc/base/tiling_data.h"
17 #include "cc/resources/picture.h"
18 #include "ui/gfx/geometry/size.h"
20 namespace base {
21 namespace debug {
22 class TracedValue;
24 class Value;
27 namespace cc {
29 class CC_EXPORT PicturePileBase
30 : public base::RefCountedThreadSafe<PicturePileBase> {
31 public:
32 PicturePileBase();
33 explicit PicturePileBase(const PicturePileBase* other);
35 gfx::Size tiling_size() const { return tiling_.tiling_size(); }
36 void SetMinContentsScale(float min_contents_scale);
38 // If non-empty, all pictures tiles inside this rect are recorded. There may
39 // be recordings outside this rect, but everything inside the rect is
40 // recorded.
41 gfx::Rect recorded_viewport() const { return recorded_viewport_; }
43 int num_tiles_x() const { return tiling_.num_tiles_x(); }
44 int num_tiles_y() const { return tiling_.num_tiles_y(); }
45 gfx::Rect tile_bounds(int x, int y) const { return tiling_.TileBounds(x, y); }
46 bool HasRecordingAt(int x, int y);
47 bool CanRaster(float contents_scale, const gfx::Rect& content_rect);
49 // If this pile contains any valid recordings. May have false positives.
50 bool HasRecordings() const { return has_any_recordings_; }
52 // If this pile has ever contained any recordings with text.
53 bool has_text() const { return has_text_; }
55 bool is_solid_color() const { return is_solid_color_; }
56 SkColor solid_color() const { return solid_color_; }
58 void set_is_mask(bool is_mask) { is_mask_ = is_mask; }
59 bool is_mask() const { return is_mask_; }
61 static void ComputeTileGridInfo(const gfx::Size& tile_grid_size,
62 SkTileGridFactory::TileGridInfo* info);
64 void SetTileGridSize(const gfx::Size& tile_grid_size);
65 TilingData& tiling() { return tiling_; }
67 void AsValueInto(base::debug::TracedValue* array) const;
69 SkTileGridFactory::TileGridInfo GetTileGridInfoForTesting() const {
70 return tile_grid_info_;
73 protected:
74 class CC_EXPORT PictureInfo {
75 public:
76 enum {
77 INVALIDATION_FRAMES_TRACKED = 32
80 PictureInfo();
81 ~PictureInfo();
83 bool Invalidate(int frame_number);
84 bool NeedsRecording(int frame_number, int distance_to_visible);
85 void SetPicture(scoped_refptr<Picture> picture);
86 const Picture* GetPicture() const;
88 float GetInvalidationFrequencyForTesting() const {
89 return GetInvalidationFrequency();
92 private:
93 void AdvanceInvalidationHistory(int frame_number);
94 float GetInvalidationFrequency() const;
96 int last_frame_number_;
97 scoped_refptr<const Picture> picture_;
98 std::bitset<INVALIDATION_FRAMES_TRACKED> invalidation_history_;
101 typedef std::pair<int, int> PictureMapKey;
102 typedef base::hash_map<PictureMapKey, PictureInfo> PictureMap;
104 virtual ~PicturePileBase();
106 int buffer_pixels() const { return tiling_.border_texels(); }
107 void Clear();
109 gfx::Rect PaddedRect(const PictureMapKey& key) const;
110 gfx::Rect PadRect(const gfx::Rect& rect) const;
112 // An internal CanRaster check that goes to the picture_map rather than
113 // using the recorded_viewport hint.
114 bool CanRasterSlowTileCheck(const gfx::Rect& layer_rect) const;
116 // A picture pile is a tiled set of pictures. The picture map is a map of tile
117 // indices to picture infos.
118 PictureMap picture_map_;
119 TilingData tiling_;
120 gfx::Rect recorded_viewport_;
121 float min_contents_scale_;
122 SkTileGridFactory::TileGridInfo tile_grid_info_;
123 SkColor background_color_;
124 int slow_down_raster_scale_factor_for_debug_;
125 bool contents_opaque_;
126 bool contents_fill_bounds_completely_;
127 bool show_debug_picture_borders_;
128 bool clear_canvas_with_debug_color_;
129 // A hint about whether there are any recordings. This may be a false
130 // positive.
131 bool has_any_recordings_;
132 bool has_text_;
133 bool is_mask_;
134 bool is_solid_color_;
135 SkColor solid_color_;
137 private:
138 void SetBufferPixels(int buffer_pixels);
140 friend class base::RefCountedThreadSafe<PicturePileBase>;
141 DISALLOW_COPY_AND_ASSIGN(PicturePileBase);
144 } // namespace cc
146 #endif // CC_RESOURCES_PICTURE_PILE_BASE_H_