Skip to content

Commit 6231ee5

Browse files
mitoshimaChromium LUCI CQ
authored andcommitted
Refactor copy layer code
The same code is used in screen rotator and tablet mode controller. Refactor and make it a library so that we can use it in different places like unlock. Bug: None Change-Id: I6bbb99c4fc77da8a30379fa8fc4b02ad2d6c7369 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2562962 Commit-Queue: Mitsuru Oshima <[email protected]> Reviewed-by: Sammie Quon <[email protected]> Cr-Commit-Position: refs/heads/master@{#832233}
1 parent 2be010b commit 6231ee5

File tree

6 files changed

+118
-56
lines changed

6 files changed

+118
-56
lines changed

ash/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,8 @@ component("ash") {
14291429
"tray_action/tray_action.cc",
14301430
"tray_action/tray_action.h",
14311431
"tray_action/tray_action_observer.h",
1432+
"utility/layer_util.cc",
1433+
"utility/layer_util.h",
14321434
"utility/screenshot_controller.cc",
14331435
"utility/screenshot_controller.h",
14341436
"utility/transformer_util.cc",

ash/rotator/screen_rotation_animator.cc

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ash/rotator/screen_rotation_animation.h"
1414
#include "ash/rotator/screen_rotation_animator_observer.h"
1515
#include "ash/shell.h"
16+
#include "ash/utility/layer_util.h"
1617
#include "ash/utility/transformer_util.h"
1718
#include "base/bind.h"
1819
#include "base/command_line.h"
@@ -21,7 +22,6 @@
2122
#include "base/time/time.h"
2223
#include "components/viz/common/frame_sinks/copy_output_request.h"
2324
#include "components/viz/common/frame_sinks/copy_output_result.h"
24-
#include "third_party/khronos/GLES2/gl2.h"
2525
#include "ui/aura/window.h"
2626
#include "ui/base/class_property.h"
2727
#include "ui/compositor/animation_throughput_reporter.h"
@@ -360,23 +360,14 @@ void ScreenRotationAnimator::CreateOldLayerTreeForSlowAnimation() {
360360

361361
std::unique_ptr<ui::LayerTreeOwner> ScreenRotationAnimator::CopyLayerTree(
362362
std::unique_ptr<viz::CopyOutputResult> result) {
363-
DCHECK(!result->IsEmpty());
364-
DCHECK_EQ(result->format(), viz::CopyOutputResult::Format::RGBA_TEXTURE);
365-
auto transfer_resource = viz::TransferableResource::MakeGL(
366-
result->GetTextureResult()->mailbox, GL_LINEAR, GL_TEXTURE_2D,
367-
result->GetTextureResult()->sync_token, result->size(),
368-
false /* is_overlay_candidate */);
369-
std::unique_ptr<viz::SingleReleaseCallback> release_callback =
370-
result->TakeTextureOwnership();
363+
std::unique_ptr<ui::Layer> copy_layer =
364+
CreateLayerFromCopyOutputResult(std::move(result));
371365
const gfx::Rect rect(
372366
GetScreenRotationContainer(root_window_)->layer()->size());
373-
std::unique_ptr<ui::Layer> copy_layer = std::make_unique<ui::Layer>();
374367
copy_layer->SetBounds(rect);
375368
// TODO(crbug.com/1040279): This is a workaround and should be removed once
376369
// the issue is fixed.
377370
copy_layer->SetFillsBoundsOpaquely(false);
378-
copy_layer->SetTransferableResource(transfer_resource,
379-
std::move(release_callback), rect.size());
380371
return std::make_unique<ui::LayerTreeOwner>(std::move(copy_layer));
381372
}
382373

ash/utility/layer_util.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 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 "ash/utility/layer_util.h"
6+
7+
#include "components/viz/common/frame_sinks/copy_output_request.h"
8+
#include "components/viz/common/frame_sinks/copy_output_result.h"
9+
#include "third_party/khronos/GLES2/gl2.h"
10+
#include "ui/compositor/layer.h"
11+
#include "ui/gfx/geometry/size.h"
12+
13+
namespace ash {
14+
namespace {
15+
16+
void CopyCopyOutputResultToLayer(
17+
std::unique_ptr<viz::CopyOutputResult> copy_result,
18+
ui::Layer* target_layer) {
19+
DCHECK(!copy_result->IsEmpty());
20+
DCHECK_EQ(copy_result->format(), viz::CopyOutputResult::Format::RGBA_TEXTURE);
21+
22+
const gfx::Size layer_size = copy_result->size();
23+
viz::TransferableResource transferable_resource =
24+
viz::TransferableResource::MakeGL(
25+
copy_result->GetTextureResult()->mailbox, GL_LINEAR, GL_TEXTURE_2D,
26+
copy_result->GetTextureResult()->sync_token, layer_size,
27+
/*is_overlay_candidate=*/false);
28+
std::unique_ptr<viz::SingleReleaseCallback> release_callback =
29+
copy_result->TakeTextureOwnership();
30+
target_layer->SetTransferableResource(
31+
transferable_resource, std::move(release_callback), layer_size);
32+
}
33+
34+
void CopyToNewLayerOnCopyRequestFinished(
35+
LayerCopyCallback layer_copy_callback,
36+
std::unique_ptr<viz::CopyOutputResult> copy_result) {
37+
if (!copy_result || copy_result->IsEmpty()) {
38+
std::move(layer_copy_callback).Run(nullptr);
39+
return;
40+
}
41+
42+
auto copy_layer = CreateLayerFromCopyOutputResult(std::move(copy_result));
43+
std::move(layer_copy_callback).Run(std::move(copy_layer));
44+
}
45+
46+
} // namespace
47+
48+
std::unique_ptr<ui::Layer> CreateLayerFromCopyOutputResult(
49+
std::unique_ptr<viz::CopyOutputResult> copy_result) {
50+
auto copy_layer = std::make_unique<ui::Layer>();
51+
CopyCopyOutputResultToLayer(std::move(copy_result), copy_layer.get());
52+
return copy_layer;
53+
}
54+
55+
void CopyLayerContentToNewLayer(ui::Layer* layer, LayerCopyCallback callback) {
56+
auto new_callback =
57+
base::BindOnce(&CopyToNewLayerOnCopyRequestFinished, std::move(callback));
58+
auto copy_request = std::make_unique<viz::CopyOutputRequest>(
59+
viz::CopyOutputRequest::ResultFormat::RGBA_TEXTURE,
60+
std::move(new_callback));
61+
gfx::Rect bounds(layer->size());
62+
copy_request->set_area(bounds);
63+
copy_request->set_result_selection(bounds);
64+
layer->RequestCopyOfOutput(std::move(copy_request));
65+
}
66+
67+
} // namespace ash

ash/utility/layer_util.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2020 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+
#ifndef ASH_UTILITY_LAYER_UTIL_H_
6+
#define ASH_UTILITY_LAYER_UTIL_H_
7+
8+
#include <memory>
9+
10+
#include "base/callback.h"
11+
12+
namespace ui {
13+
class Layer;
14+
}
15+
16+
namespace viz {
17+
class CopyOutputResult;
18+
}
19+
20+
namespace ash {
21+
using LayerCopyCallback =
22+
base::OnceCallback<void(std::unique_ptr<ui::Layer> new_layer)>;
23+
24+
// Creates the new layer using the image in |copy_result|.
25+
std::unique_ptr<ui::Layer> CreateLayerFromCopyOutputResult(
26+
std::unique_ptr<viz::CopyOutputResult> copy_result);
27+
28+
// Creates a new layer that has a copy of the |layer|'s content. This is an
29+
// async API and a new layer will be passed to the |callback| when copy is done.
30+
void CopyLayerContentToNewLayer(ui::Layer* layer, LayerCopyCallback callback);
31+
32+
} // namespace ash
33+
34+
#endif // ASH_UTILITY_LAYER_UTIL_H_

ash/wm/tablet_mode/tablet_mode_controller.cc

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ash/shell.h"
2121
#include "ash/shell_delegate.h"
2222
#include "ash/strings/grit/ash_strings.h"
23+
#include "ash/utility/layer_util.h"
2324
#include "ash/wm/overview/overview_controller.h"
2425
#include "ash/wm/splitview/split_view_utils.h"
2526
#include "ash/wm/tablet_mode/internal_input_devices_event_blocker.h"
@@ -38,9 +39,6 @@
3839
#include "base/time/tick_clock.h"
3940
#include "chromeos/dbus/power/power_manager_client.h"
4041
#include "chromeos/system/devicemode.h"
41-
#include "components/viz/common/frame_sinks/copy_output_request.h"
42-
#include "components/viz/common/frame_sinks/copy_output_result.h"
43-
#include "third_party/khronos/GLES2/gl2.h"
4442
#include "ui/aura/client/aura_constants.h"
4543
#include "ui/aura/window_observer.h"
4644
#include "ui/base/accelerators/accelerator.h"
@@ -161,26 +159,6 @@ bool ShouldObserveSequence(ui::LayerAnimationSequence* sequence) {
161159
TabletModeController::GetObservedTabletTransitionProperty();
162160
}
163161

164-
std::unique_ptr<ui::Layer> CreateLayerFromScreenshotResult(
165-
std::unique_ptr<viz::CopyOutputResult> copy_result) {
166-
DCHECK(!copy_result->IsEmpty());
167-
DCHECK_EQ(copy_result->format(), viz::CopyOutputResult::Format::RGBA_TEXTURE);
168-
169-
const gfx::Size layer_size = copy_result->size();
170-
viz::TransferableResource transferable_resource =
171-
viz::TransferableResource::MakeGL(
172-
copy_result->GetTextureResult()->mailbox, GL_LINEAR, GL_TEXTURE_2D,
173-
copy_result->GetTextureResult()->sync_token, layer_size,
174-
/*is_overlay_candidate=*/false);
175-
std::unique_ptr<viz::SingleReleaseCallback> release_callback =
176-
copy_result->TakeTextureOwnership();
177-
auto screenshot_layer = std::make_unique<ui::Layer>();
178-
screenshot_layer->SetTransferableResource(
179-
transferable_resource, std::move(release_callback), layer_size);
180-
181-
return screenshot_layer;
182-
}
183-
184162
// Check if there is any external and internal pointing device in
185163
// |input_devices|.
186164
void CheckHasPointingDevices(
@@ -1199,25 +1177,19 @@ void TabletModeController::TakeScreenshot(aura::Window* top_window) {
11991177

12001178
// Request a screenshot.
12011179
screenshot_taken_callback_.Reset(base::BindOnce(
1202-
&TabletModeController::OnScreenshotTaken, weak_factory_.GetWeakPtr(),
1180+
&TabletModeController::OnLayerCopyed, weak_factory_.GetWeakPtr(),
12031181
std::move(callback), root_window));
12041182

1205-
const gfx::Rect request_bounds(screenshot_window->layer()->size());
1206-
auto screenshot_request = std::make_unique<viz::CopyOutputRequest>(
1207-
viz::CopyOutputRequest::ResultFormat::RGBA_TEXTURE,
1208-
screenshot_taken_callback_.callback());
1209-
screenshot_request->set_area(request_bounds);
1210-
screenshot_request->set_result_selection(request_bounds);
1211-
screenshot_window->layer()->RequestCopyOfOutput(
1212-
std::move(screenshot_request));
1183+
CopyLayerContentToNewLayer(screenshot_window->layer(),
1184+
screenshot_taken_callback_.callback());
12131185

12141186
VLOG(1) << "Tablet screenshot requested.";
12151187
}
12161188

1217-
void TabletModeController::OnScreenshotTaken(
1189+
void TabletModeController::OnLayerCopyed(
12181190
base::OnceClosure on_screenshot_taken,
12191191
aura::Window* root_window,
1220-
std::unique_ptr<viz::CopyOutputResult> copy_result) {
1192+
std::unique_ptr<ui::Layer> copy_layer) {
12211193
aura::Window* top_window =
12221194
destroy_observer_ ? destroy_observer_->window() : nullptr;
12231195
ResetDestroyObserver();
@@ -1228,14 +1200,14 @@ void TabletModeController::OnScreenshotTaken(
12281200
if (!base::Contains(Shell::GetAllRootWindows(), root_window))
12291201
return;
12301202

1231-
if (!copy_result || copy_result->IsEmpty() || !top_window) {
1203+
if (!copy_layer || !top_window) {
12321204
std::move(on_screenshot_taken).Run();
12331205
return;
12341206
}
12351207

12361208
// Stack the screenshot under |top_window|, to fully occlude all windows
12371209
// except |top_window| for the duration of the enter tablet mode animation.
1238-
screenshot_layer_ = CreateLayerFromScreenshotResult(std::move(copy_result));
1210+
screenshot_layer_ = std::move(copy_layer);
12391211
top_window->parent()->layer()->Add(screenshot_layer_.get());
12401212
screenshot_layer_->SetBounds(top_window->GetRootWindow()->bounds());
12411213
top_window->parent()->layer()->StackBelow(screenshot_layer_.get(),

ash/wm/tablet_mode/tablet_mode_controller.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ namespace views {
5353
class Widget;
5454
}
5555

56-
namespace viz {
57-
class CopyOutputResult;
58-
}
59-
6056
namespace ash {
6157

6258
class InternalInputDevicesEventBlocker;
@@ -312,9 +308,9 @@ class ASH_EXPORT TabletModeController
312308
// Called when a screenshot is taken. Creates |screenshot_widget_| which holds
313309
// the screenshot results and stacks it under top window. |root_window|
314310
// specifies on which root window the screen shot is taken.
315-
void OnScreenshotTaken(base::OnceClosure on_screenshot_taken,
316-
aura::Window* root_window,
317-
std::unique_ptr<viz::CopyOutputResult> copy_result);
311+
void OnLayerCopyed(base::OnceClosure on_screenshot_taken,
312+
aura::Window* root_window,
313+
std::unique_ptr<ui::Layer> copy_layer);
318314

319315
// Calculates whether the device is currently in a physical tablet state,
320316
// using the most recent seen device events such as lid angle changes.
@@ -464,7 +460,7 @@ class ASH_EXPORT TabletModeController
464460
// Tracks and record transition smoothness.
465461
base::Optional<ui::ThroughputTracker> transition_tracker_;
466462

467-
base::CancelableOnceCallback<void(std::unique_ptr<viz::CopyOutputResult>)>
463+
base::CancelableOnceCallback<void(std::unique_ptr<ui::Layer>)>
468464
screenshot_taken_callback_;
469465
base::CancelableOnceClosure screenshot_set_callback_;
470466

0 commit comments

Comments
 (0)