summaryrefslogtreecommitdiff
path: root/chromium/ui/snapshot/snapshot_aura.cc
blob: 1cebfe9be08f2c96ec257cba834d83209ed9b2a9 (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
// Copyright (c) 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 "ui/snapshot/snapshot.h"

#include "base/logging.h"
#include "base/safe_numerics.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/dip_util.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/display.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/rect_f.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/skbitmap_operations.h"
#include "ui/gfx/transform.h"

namespace ui {

bool GrabViewSnapshot(gfx::NativeView view,
                      std::vector<unsigned char>* png_representation,
                      const gfx::Rect& snapshot_bounds) {
  return GrabWindowSnapshot(view, png_representation, snapshot_bounds);
}

bool GrabWindowSnapshot(gfx::NativeWindow window,
                        std::vector<unsigned char>* png_representation,
                        const gfx::Rect& snapshot_bounds) {
  ui::Compositor* compositor = window->layer()->GetCompositor();

  gfx::RectF read_pixels_bounds = snapshot_bounds;

  // We must take into account the window's position on the desktop.
  read_pixels_bounds.Offset(
      window->GetBoundsInRootWindow().origin().OffsetFromOrigin());
  aura::WindowEventDispatcher* dispatcher = window->GetDispatcher();
  if (dispatcher)
    dispatcher->GetRootTransform().TransformRect(&read_pixels_bounds);

  gfx::Rect read_pixels_bounds_in_pixel =
      gfx::ToEnclosingRect(read_pixels_bounds);

  // Sometimes (i.e. when using Aero on Windows) the compositor's size is
  // smaller than the window bounds. So trim appropriately.
  read_pixels_bounds_in_pixel.Intersect(gfx::Rect(compositor->size()));

  DCHECK_LE(0, read_pixels_bounds.x());
  DCHECK_LE(0, read_pixels_bounds.y());

  SkBitmap bitmap;
  if (!compositor->ReadPixels(&bitmap, read_pixels_bounds_in_pixel))
    return false;

  gfx::Display display =
      gfx::Screen::GetScreenFor(window)->GetDisplayNearestWindow(window);
  switch (display.rotation()) {
    case gfx::Display::ROTATE_0:
      break;
    case gfx::Display::ROTATE_90:
      bitmap = SkBitmapOperations::Rotate(
          bitmap, SkBitmapOperations::ROTATION_270_CW);
      break;
    case gfx::Display::ROTATE_180:
      bitmap = SkBitmapOperations::Rotate(
          bitmap, SkBitmapOperations::ROTATION_180_CW);
      break;
    case gfx::Display::ROTATE_270:
      bitmap = SkBitmapOperations::Rotate(
          bitmap, SkBitmapOperations::ROTATION_90_CW);
      break;
  }

  unsigned char* pixels = reinterpret_cast<unsigned char*>(
      bitmap.pixelRef()->pixels());
  return gfx::PNGCodec::Encode(
      pixels, gfx::PNGCodec::FORMAT_BGRA,
      gfx::Size(bitmap.width(), bitmap.height()),
      base::checked_numeric_cast<int>(bitmap.rowBytes()),
      true, std::vector<gfx::PNGCodec::Comment>(),
      png_representation);
}

}  // namespace ui