summaryrefslogtreecommitdiffstats
path: root/cc/playback/pixel_ref_map.h
blob: f8c8ca781e5bd894ff8e1fc28df993dc902799b9 (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
// Copyright 2015 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_PLAYBACK_PIXEL_REF_MAP_H_
#define CC_PLAYBACK_PIXEL_REF_MAP_H_

#include <utility>
#include <vector>

#include "base/containers/hash_tables.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "cc/base/cc_export.h"
#include "skia/ext/pixel_ref_utils.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

class SkPixelRef;

namespace cc {

class Picture;
class DisplayItemList;

typedef std::pair<int, int> PixelRefMapKey;
typedef std::vector<skia::PositionPixelRef> PixelRefs;
typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefHashmap;

// This class is used and owned by cc Picture class. It is used to gather pixel
// refs which would happen after record. It takes in |cell_size| to decide how
// big each grid cell should be.
class CC_EXPORT PixelRefMap {
 public:
  explicit PixelRefMap(const gfx::Size& cell_size);
  ~PixelRefMap();
  void GatherPixelRefsFromPicture(SkPicture* picture,
                                  const gfx::Rect& layer_rect);

  bool empty() const { return data_hash_map_.empty(); }

  // This iterator imprecisely returns the set of pixel refs that are needed to
  // raster this layer rect from this picture.  Internally, pixel refs are
  // clumped into tile grid buckets, so there may be false positives.
  class CC_EXPORT Iterator {
   public:
    // Default iterator constructor that is used as place holder for invalid
    // Iterator.
    Iterator();
    Iterator(const gfx::Rect& layer_rect, const Picture* picture);
    Iterator(const gfx::Rect& layer_rect, const DisplayItemList* picture);
    ~Iterator();

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

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

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

   private:
    void PointToFirstPixelRef(const gfx::Rect& query_rect);

    static base::LazyInstance<PixelRefs> empty_pixel_refs_;
    const PixelRefMap* target_pixel_ref_map_;
    const PixelRefs* current_pixel_refs_;
    unsigned current_index_;

    gfx::Rect map_layer_rect_;

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

 private:
  gfx::Point min_pixel_cell_;
  gfx::Point max_pixel_cell_;
  gfx::Size cell_size_;

  PixelRefHashmap data_hash_map_;
};

}  // namespace cc

#endif  // CC_PLAYBACK_PIXEL_REF_MAP_H_