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