15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/snapshot/snapshot.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/task_runner_util.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "cc/output/copy_output_request.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkBitmap.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/compositor.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/dip_util.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/layer.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ui/snapshot/snapshot_async.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace ui {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GrabViewSnapshot(gfx::NativeView view,
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      std::vector<unsigned char>* png_representation,
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const gfx::Rect& snapshot_bounds) {
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return GrabWindowSnapshot(view, png_representation, snapshot_bounds);
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool GrabWindowSnapshot(gfx::NativeWindow window,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        std::vector<unsigned char>* png_representation,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        const gfx::Rect& snapshot_bounds) {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Not supported in Aura.  Callers should fall back to the async version.
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static void MakeAsyncCopyRequest(
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::NativeWindow window,
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Rect& source_rect,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<cc::CopyOutputRequest> request =
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      cc::CopyOutputRequest::CreateBitmapRequest(callback);
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  request->set_area(source_rect);
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  window->layer()->RequestCopyOfOutput(request.Pass());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void GrabWindowSnapshotAndScaleAsync(
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::NativeWindow window,
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Rect& source_rect,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Size& target_size,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<base::TaskRunner> background_task_runner,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GrabWindowSnapshotAsyncCallback& callback) {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MakeAsyncCopyRequest(window,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       source_rect,
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       base::Bind(&SnapshotAsync::ScaleCopyOutputResult,
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  callback,
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  target_size,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  background_task_runner));
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void GrabWindowSnapshotAsync(
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::NativeWindow window,
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Rect& source_rect,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<base::TaskRunner> background_task_runner,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GrabWindowSnapshotAsyncPNGCallback& callback) {
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MakeAsyncCopyRequest(window,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       source_rect,
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       base::Bind(&SnapshotAsync::EncodeCopyOutputResult,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  callback,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  background_task_runner));
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void GrabViewSnapshotAsync(
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::NativeView view,
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Rect& source_rect,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<base::TaskRunner> background_task_runner,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const GrabWindowSnapshotAsyncPNGCallback& callback) {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GrabWindowSnapshotAsync(view, source_rect, background_task_runner, callback);
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace ui
79