summaryrefslogtreecommitdiffstats
path: root/cc/picture_pile_impl.cc
blob: 4155664f79ad3396e2938ac5472527446ee930d4 (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
// 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.

#include "base/debug/trace_event.h"
#include "cc/picture_pile_impl.h"
#include "cc/region.h"
#include "cc/rendering_stats.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSize.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/skia_util.h"

namespace cc {

scoped_refptr<PicturePileImpl> PicturePileImpl::Create() {
  return make_scoped_refptr(new PicturePileImpl());
}

PicturePileImpl::PicturePileImpl() {
}

PicturePileImpl::~PicturePileImpl() {
}

PicturePileImpl* PicturePileImpl::GetCloneForDrawingOnThread(
    base::Thread* thread) {
  // Do we have a clone for this thread yet?
  CloneMap::iterator it = clones_.find(thread->thread_id());
  if (it != clones_.end())
    return it->second;

  // Create clone for this thread.
  scoped_refptr<PicturePileImpl> clone = CloneForDrawing();
  clones_[thread->thread_id()] = clone;
  return clone;
}

scoped_refptr<PicturePileImpl> PicturePileImpl::CloneForDrawing() const {
  TRACE_EVENT0("cc", "PicturePileImpl::CloneForDrawing");
  scoped_refptr<PicturePileImpl> clone = Create();
  for (Pile::const_iterator i = pile_.begin(); i != pile_.end(); ++i)
    clone->pile_.push_back((*i)->Clone());
  clone->min_contents_scale_ = min_contents_scale_;

  return clone;
}

void PicturePileImpl::Raster(
    SkCanvas* canvas,
    gfx::Rect content_rect,
    float contents_scale,
    RenderingStats* stats) {

  if (!pile_.size())
    return;

  DCHECK(contents_scale >= min_contents_scale_);

  base::TimeTicks rasterizeBeginTime = base::TimeTicks::Now();

  canvas->save();
  canvas->translate(-content_rect.x(), -content_rect.y());
  canvas->clipRect(gfx::RectToSkRect(content_rect));

  // Raster through the pile top down, using clips to make sure that
  // pictures on top are not overdrawn by pictures on the bottom.
  Region unclipped(content_rect);
  for (Pile::reverse_iterator i = pile_.rbegin(); i != pile_.rend(); ++i) {
    // This is intentionally *enclosed* rect, so that the clip is aligned on
    // integral post-scale content pixels and does not extend past the edges of
    // the picture's layer rect.  The min_contents_scale enforces that enough
    // buffer pixels have been added such that the enclosed rect encompasses all
    // invalidated pixels at any larger scale level.
    gfx::Rect content_clip = gfx::ToEnclosedRect(
        gfx::ScaleRect((*i)->LayerRect(), contents_scale));
    if (!unclipped.Intersects(content_clip))
      continue;
    (*i)->Raster(canvas, content_clip, contents_scale);

    // Don't allow pictures underneath to draw where this picture did.
    canvas->clipRect(gfx::RectToSkRect(content_clip), SkRegion::kDifference_Op);
    unclipped.Subtract(content_clip);

    stats->totalPixelsRasterized +=
        content_clip.width() * content_clip.height();
  }
  canvas->restore();

  stats->totalRasterizeTime += base::TimeTicks::Now() - rasterizeBeginTime;
}

void PicturePileImpl::GatherPixelRefs(
    const gfx::Rect& rect, std::list<skia::LazyPixelRef*>& pixel_refs) {
  std::list<skia::LazyPixelRef*> result;
  for (Pile::const_iterator i = pile_.begin(); i != pile_.end(); ++i) {
    (*i)->GatherPixelRefs(rect, result);
    pixel_refs.splice(pixel_refs.end(), result);
  }
}

skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() {
  TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture");

  gfx::Rect layer_rect;
  for (Pile::const_iterator i = pile_.begin(); i != pile_.end(); ++i) {
    layer_rect.Union((*i)->LayerRect());
  }

  skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
  if (layer_rect.IsEmpty())
    return picture;

  SkCanvas* canvas = picture->beginRecording(
      layer_rect.width(),
      layer_rect.height(),
      SkPicture::kUsePathBoundsForClip_RecordingFlag);

  RenderingStats stats;
  Raster(canvas, layer_rect, 1.0, &stats);
  picture->endRecording();

  return picture;
}

}  // namespace cc