summaryrefslogtreecommitdiff
path: root/chromium/cc/resources/picture.h
blob: 085fb6a453167e9c967f3260d119b730670e2d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_RESOURCES_PICTURE_H_
#define CC_RESOURCES_PICTURE_H_

#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/containers/hash_tables.h"
#include "base/debug/trace_event.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/base/region.h"
#include "skia/ext/lazy_pixel_ref.h"
#include "skia/ext/refptr.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "third_party/skia/include/core/SkTileGridPicture.h"
#include "ui/gfx/rect.h"

namespace base {
class Value;
}

namespace skia {
class AnalysisCanvas;
}

namespace cc {

class ContentLayerClient;

class CC_EXPORT Picture
    : public base::RefCountedThreadSafe<Picture> {
 public:
  typedef std::pair<int, int> PixelRefMapKey;
  typedef std::vector<skia::LazyPixelRef*> PixelRefs;
  typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap;

  static scoped_refptr<Picture> Create(gfx::Rect layer_rect);
  static scoped_refptr<Picture> CreateFromValue(const base::Value* value);
  static scoped_refptr<Picture> CreateFromSkpValue(const base::Value* value);

  gfx::Rect LayerRect() const { return layer_rect_; }
  gfx::Rect OpaqueRect() const { return opaque_rect_; }

  // Get thread-safe clone for rasterizing with on a specific thread.
  scoped_refptr<Picture> GetCloneForDrawingOnThread(
      unsigned thread_index) const;

  // Make thread-safe clones for rasterizing with.
  void CloneForDrawing(int num_threads);

  // Record a paint operation. To be able to safely use this SkPicture for
  // playback on a different thread this can only be called once.
  void Record(ContentLayerClient* client,
              const SkTileGridPicture::TileGridInfo& tile_grid_info);

  // Gather pixel refs from recording.
  void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info);

  // Has Record() been called yet?
  bool HasRecording() const { return picture_.get() != NULL; }

  // Apply this scale and raster the negated region into the canvas. See comment
  // in PicturePileImpl::RasterCommon for explanation on negated content region.
  int Raster(SkCanvas* canvas,
             SkDrawPictureCallback* callback,
             const Region& negated_content_region,
             float contents_scale);

  // Draw the picture directly into the given canvas, without applying any
  // clip/scale/layer transformations.
  void Replay(SkCanvas* canvas);

  scoped_ptr<base::Value> AsValue() const;

  class CC_EXPORT PixelRefIterator {
   public:
    PixelRefIterator();
    PixelRefIterator(gfx::Rect layer_rect, const Picture* picture);
    ~PixelRefIterator();

    skia::LazyPixelRef* operator->() const {
      DCHECK_LT(current_index_, current_pixel_refs_->size());
      return (*current_pixel_refs_)[current_index_];
    }

    skia::LazyPixelRef* operator*() const {
      DCHECK_LT(current_index_, current_pixel_refs_->size());
      return (*current_pixel_refs_)[current_index_];
    }

    PixelRefIterator& operator++();
    operator bool() const {
      return current_index_ < current_pixel_refs_->size();
    }

   private:
    static base::LazyInstance<PixelRefs> empty_pixel_refs_;
    const Picture* picture_;
    const PixelRefs* current_pixel_refs_;
    unsigned current_index_;

    gfx::Point min_point_;
    gfx::Point max_point_;
    int current_x_;
    int current_y_;
  };

  void EmitTraceSnapshot();
  void EmitTraceSnapshotAlias(Picture* original);

  bool WillPlayBackBitmaps() const { return picture_->willPlayBackBitmaps(); }

 private:
  explicit Picture(gfx::Rect layer_rect);
  // This constructor assumes SkPicture is already ref'd and transfers
  // ownership to this picture.
  Picture(const skia::RefPtr<SkPicture>&,
          gfx::Rect layer_rect,
          gfx::Rect opaque_rect,
          const PixelRefMap& pixel_refs);
  // This constructor will call AdoptRef on the SkPicture.
  Picture(SkPicture*,
          gfx::Rect layer_rect,
          gfx::Rect opaque_rect);
  ~Picture();

  gfx::Rect layer_rect_;
  gfx::Rect opaque_rect_;
  skia::RefPtr<SkPicture> picture_;

  typedef std::vector<scoped_refptr<Picture> > PictureVector;
  PictureVector clones_;

  PixelRefMap pixel_refs_;
  gfx::Point min_pixel_cell_;
  gfx::Point max_pixel_cell_;
  gfx::Size cell_size_;

  scoped_refptr<base::debug::ConvertableToTraceFormat>
    AsTraceableRasterData(float scale) const;
  scoped_refptr<base::debug::ConvertableToTraceFormat>
    AsTraceableRecordData() const;

  friend class base::RefCountedThreadSafe<Picture>;
  friend class PixelRefIterator;
  DISALLOW_COPY_AND_ASSIGN(Picture);
};

}  // namespace cc

#endif  // CC_RESOURCES_PICTURE_H_