Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / cc / resources / picture.h
1 // Copyright 2012 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_PICTURE_H_
6 #define CC_RESOURCES_PICTURE_H_
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/debug/trace_event.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "cc/base/cc_export.h"
21 #include "cc/base/region.h"
22 #include "skia/ext/refptr.h"
23 #include "third_party/skia/include/core/SkTileGridPicture.h"
24 #include "ui/gfx/rect.h"
25
26 class SkPixelRef;
27
28 namespace base {
29 class Value;
30 }
31
32 namespace skia {
33 class AnalysisCanvas;
34 }
35
36 namespace cc {
37
38 class ContentLayerClient;
39
40 class CC_EXPORT Picture
41     : public base::RefCountedThreadSafe<Picture> {
42  public:
43   typedef std::pair<int, int> PixelRefMapKey;
44   typedef std::vector<SkPixelRef*> PixelRefs;
45   typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap;
46
47   static scoped_refptr<Picture> Create(
48       const gfx::Rect& layer_rect,
49       ContentLayerClient* client,
50       const SkTileGridPicture::TileGridInfo& tile_grid_info,
51       bool gather_pixels_refs,
52       int num_raster_threads);
53   static scoped_refptr<Picture> CreateFromValue(const base::Value* value);
54   static scoped_refptr<Picture> CreateFromSkpValue(const base::Value* value);
55
56   gfx::Rect LayerRect() const { return layer_rect_; }
57   gfx::Rect OpaqueRect() const { return opaque_rect_; }
58
59   // Get thread-safe clone for rasterizing with on a specific thread.
60   Picture* GetCloneForDrawingOnThread(unsigned thread_index);
61
62   // Has Record() been called yet?
63   bool HasRecording() const { return picture_.get() != NULL; }
64
65   // Apply this scale and raster the negated region into the canvas. See comment
66   // in PicturePileImpl::RasterCommon for explanation on negated content region.
67   int Raster(SkCanvas* canvas,
68              SkDrawPictureCallback* callback,
69              const Region& negated_content_region,
70              float contents_scale);
71
72   // Draw the picture directly into the given canvas, without applying any
73   // clip/scale/layer transformations.
74   void Replay(SkCanvas* canvas);
75
76   scoped_ptr<base::Value> AsValue() const;
77
78   class CC_EXPORT PixelRefIterator {
79    public:
80     PixelRefIterator();
81     PixelRefIterator(const gfx::Rect& layer_rect, const Picture* picture);
82     ~PixelRefIterator();
83
84     SkPixelRef* operator->() const {
85       DCHECK_LT(current_index_, current_pixel_refs_->size());
86       return (*current_pixel_refs_)[current_index_];
87     }
88
89     SkPixelRef* operator*() const {
90       DCHECK_LT(current_index_, current_pixel_refs_->size());
91       return (*current_pixel_refs_)[current_index_];
92     }
93
94     PixelRefIterator& operator++();
95     operator bool() const {
96       return current_index_ < current_pixel_refs_->size();
97     }
98
99    private:
100     static base::LazyInstance<PixelRefs> empty_pixel_refs_;
101     const Picture* picture_;
102     const PixelRefs* current_pixel_refs_;
103     unsigned current_index_;
104
105     gfx::Point min_point_;
106     gfx::Point max_point_;
107     int current_x_;
108     int current_y_;
109   };
110
111   void EmitTraceSnapshot() const;
112   void EmitTraceSnapshotAlias(Picture* original) const;
113
114   bool WillPlayBackBitmaps() const { return picture_->willPlayBackBitmaps(); }
115
116  private:
117   explicit Picture(const gfx::Rect& layer_rect);
118   // This constructor assumes SkPicture is already ref'd and transfers
119   // ownership to this picture.
120   Picture(const skia::RefPtr<SkPicture>&,
121           const gfx::Rect& layer_rect,
122           const gfx::Rect& opaque_rect,
123           const PixelRefMap& pixel_refs);
124   // This constructor will call AdoptRef on the SkPicture.
125   Picture(SkPicture*,
126           const gfx::Rect& layer_rect,
127           const gfx::Rect& opaque_rect);
128   ~Picture();
129
130   // Make thread-safe clones for rasterizing with.
131   void CloneForDrawing(int num_threads);
132
133   // Record a paint operation. To be able to safely use this SkPicture for
134   // playback on a different thread this can only be called once.
135   void Record(ContentLayerClient* client,
136               const SkTileGridPicture::TileGridInfo& tile_grid_info);
137
138   // Gather pixel refs from recording.
139   void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info);
140
141   gfx::Rect layer_rect_;
142   gfx::Rect opaque_rect_;
143   skia::RefPtr<SkPicture> picture_;
144
145   typedef std::vector<scoped_refptr<Picture> > PictureVector;
146   PictureVector clones_;
147
148   PixelRefMap pixel_refs_;
149   gfx::Point min_pixel_cell_;
150   gfx::Point max_pixel_cell_;
151   gfx::Size cell_size_;
152
153   scoped_refptr<base::debug::ConvertableToTraceFormat>
154     AsTraceableRasterData(float scale) const;
155   scoped_refptr<base::debug::ConvertableToTraceFormat>
156     AsTraceableRecordData() const;
157
158   base::ThreadChecker raster_thread_checker_;
159
160   friend class base::RefCountedThreadSafe<Picture>;
161   friend class PixelRefIterator;
162   DISALLOW_COPY_AND_ASSIGN(Picture);
163 };
164
165 }  // namespace cc
166
167 #endif  // CC_RESOURCES_PICTURE_H_