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#include "cc/resources/caching_bitmap_content_layer_updater.h"
6
7#include "base/logging.h"
8#include "cc/resources/layer_painter.h"
9#include "skia/ext/platform_canvas.h"
10
11namespace cc {
12
13scoped_refptr<CachingBitmapContentLayerUpdater>
14CachingBitmapContentLayerUpdater::Create(
15    scoped_ptr<LayerPainter> painter,
16    RenderingStatsInstrumentation* stats_instrumentation,
17    int layer_id) {
18  return make_scoped_refptr(
19      new CachingBitmapContentLayerUpdater(painter.Pass(),
20                                           stats_instrumentation,
21                                           layer_id));
22}
23
24CachingBitmapContentLayerUpdater::CachingBitmapContentLayerUpdater(
25    scoped_ptr<LayerPainter> painter,
26    RenderingStatsInstrumentation* stats_instrumentation,
27    int layer_id)
28    : BitmapContentLayerUpdater(painter.Pass(),
29                                stats_instrumentation,
30                                layer_id),
31      pixels_did_change_(false) {}
32
33CachingBitmapContentLayerUpdater::~CachingBitmapContentLayerUpdater() {}
34
35void CachingBitmapContentLayerUpdater::PrepareToUpdate(
36    gfx::Rect content_rect,
37    gfx::Size tile_size,
38    float contents_width_scale,
39    float contents_height_scale,
40    gfx::Rect* resulting_opaque_rect) {
41  BitmapContentLayerUpdater::PrepareToUpdate(content_rect,
42                                             tile_size,
43                                             contents_width_scale,
44                                             contents_height_scale,
45                                             resulting_opaque_rect);
46
47  const SkBitmap& new_bitmap = canvas_->getDevice()->accessBitmap(false);
48  SkAutoLockPixels lock(new_bitmap);
49  DCHECK_GT(new_bitmap.bytesPerPixel(), 0);
50  pixels_did_change_ = new_bitmap.config() != cached_bitmap_.config() ||
51                       new_bitmap.height() != cached_bitmap_.height() ||
52                       new_bitmap.width() != cached_bitmap_.width() ||
53                       memcmp(new_bitmap.getPixels(),
54                              cached_bitmap_.getPixels(),
55                              new_bitmap.getSafeSize());
56
57  if (pixels_did_change_)
58    new_bitmap.deepCopyTo(&cached_bitmap_, new_bitmap.config());
59}
60
61}  // namespace cc
62