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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// Copyright 2017 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/android/application_status_listener.h"
#include "base/base_switches.h"
#include "base/test/scoped_feature_list.h"
#include "components/viz/common/features.h"
#include "content/browser/browser_main_loop.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/renderer_host/compositor_impl_android.h"
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/gpu_stream_constants.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "content/test/gpu_browsertest_helpers.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/android/window_android.h"
#include "url/gurl.h"
namespace content {
namespace {
enum class CompositorImplMode {
kNormal,
kViz,
kVizSkDDL,
};
class CompositorImplBrowserTest
: public testing::WithParamInterface<CompositorImplMode>,
public ContentBrowserTest {
public:
CompositorImplBrowserTest() {}
void SetUp() override {
switch (GetParam()) {
case CompositorImplMode::kNormal:
break;
case CompositorImplMode::kViz:
scoped_feature_list_.InitAndEnableFeature(
features::kVizDisplayCompositor);
break;
case CompositorImplMode::kVizSkDDL:
scoped_feature_list_.InitWithFeatures(
{features::kVizDisplayCompositor,
features::kUseSkiaDeferredDisplayList, features::kUseSkiaRenderer},
{});
break;
}
ContentBrowserTest::SetUp();
}
protected:
void SetUpOnMainThread() override {
ASSERT_TRUE(embedded_test_server()->Start());
net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS);
https_server.ServeFilesFromSourceDirectory("content/test/data");
ASSERT_TRUE(https_server.Start());
GURL http_url(embedded_test_server()->GetURL("/title1.html"));
ASSERT_TRUE(NavigateToURL(shell(), http_url));
}
ui::WindowAndroid* window() const {
return web_contents()->GetTopLevelNativeWindow();
}
CompositorImpl* compositor_impl() const {
return static_cast<CompositorImpl*>(window()->GetCompositor());
}
WebContentsImpl* web_contents() const {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
RenderWidgetHostViewAndroid* render_widget_host_view_android() const {
return static_cast<RenderWidgetHostViewAndroid*>(
web_contents()->GetRenderWidgetHostView());
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(CompositorImplBrowserTest);
};
INSTANTIATE_TEST_CASE_P(P,
CompositorImplBrowserTest,
::testing::Values(CompositorImplMode::kNormal,
CompositorImplMode::kViz,
CompositorImplMode::kVizSkDDL));
class CompositorImplLowEndBrowserTest : public CompositorImplBrowserTest {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kEnableLowEndDeviceMode);
command_line->AppendSwitch(switches::kInProcessGPU);
content::ContentBrowserTest::SetUpCommandLine(command_line);
}
};
// Viz on android is not yet compatible with in-process GPU. Only run in
// kNormal mode.
// TODO(ericrk): Make this work everywhere. https://crbug.com/851643
INSTANTIATE_TEST_CASE_P(P,
CompositorImplLowEndBrowserTest,
::testing::Values(CompositorImplMode::kNormal));
// RunLoop implementation that calls glFlush() every second until it observes
// OnContextLost().
class ContextLostRunLoop : public viz::ContextLostObserver {
public:
ContextLostRunLoop(viz::ContextProvider* context_provider)
: context_provider_(context_provider) {
context_provider_->AddObserver(this);
}
~ContextLostRunLoop() override { context_provider_->RemoveObserver(this); }
void RunUntilContextLost() {
CheckForContextLoss();
run_loop_.Run();
}
void CheckForContextLoss() {
if (did_lose_context_) {
run_loop_.Quit();
return;
}
context_provider_->ContextGL()->Flush();
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&ContextLostRunLoop::CheckForContextLoss,
base::Unretained(this)),
base::TimeDelta::FromSeconds(1));
}
private:
// viz::LostContextProvider:
void OnContextLost() override { did_lose_context_ = true; }
viz::ContextProvider* const context_provider_;
bool did_lose_context_ = false;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(ContextLostRunLoop);
};
// RunLoop implementation that runs until it observes a compositor frame.
class CompositorFrameRunLoop : public ui::WindowAndroidObserver {
public:
CompositorFrameRunLoop(ui::WindowAndroid* window) : window_(window) {
window_->AddObserver(this);
}
~CompositorFrameRunLoop() override { window_->RemoveObserver(this); }
void RunUntilFrame() { run_loop_.Run(); }
private:
// ui::WindowAndroidObserver:
void OnCompositingDidCommit() override { run_loop_.Quit(); }
void OnRootWindowVisibilityChanged(bool visible) override {}
void OnAttachCompositor() override {}
void OnDetachCompositor() override {}
void OnActivityStopped() override {}
void OnActivityStarted() override {}
ui::WindowAndroid* const window_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(CompositorFrameRunLoop);
};
IN_PROC_BROWSER_TEST_P(CompositorImplLowEndBrowserTest,
CompositorImplDropsResourcesOnBackground) {
// This test makes invalid assumptions when surface synchronization is
// enabled. The compositor lock is obsolete, and inspecting frames
// from the CompositorImpl does not guarantee renderer CompositorFrames
// are ready.
if (features::IsSurfaceSynchronizationEnabled())
return;
auto* rwhva = render_widget_host_view_android();
auto* compositor = compositor_impl();
auto context = GpuBrowsertestCreateContext(
GpuBrowsertestEstablishGpuChannelSyncRunLoop());
context->BindToCurrentThread();
CompositorFrameRunLoop(window()).RunUntilFrame();
EXPECT_TRUE(rwhva->HasValidFrame());
ContextLostRunLoop run_loop(context.get());
compositor->SetVisibleForTesting(false);
base::android::ApplicationStatusListener::NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES);
rwhva->OnRootWindowVisibilityChanged(false);
rwhva->Hide();
// Ensure that context is eventually dropped and at that point we do not have
// a valid frame.
run_loop.RunUntilContextLost();
EXPECT_FALSE(rwhva->HasValidFrame());
// Become visible again:
compositor->SetVisibleForTesting(true);
base::android::ApplicationStatusListener::NotifyApplicationStateChange(
base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES);
rwhva->Show();
rwhva->OnRootWindowVisibilityChanged(true);
// We should have taken the compositor lock on resume.
EXPECT_TRUE(compositor->IsLockedForTesting());
EXPECT_FALSE(rwhva->HasValidFrame());
// The compositor should eventually be unlocked and produce a frame.
CompositorFrameRunLoop(window()).RunUntilFrame();
EXPECT_FALSE(compositor->IsLockedForTesting());
EXPECT_TRUE(rwhva->HasValidFrame());
}
// RunLoop implementation that runs until it observes a swap with size.
class CompositorSwapRunLoop {
public:
CompositorSwapRunLoop(CompositorImpl* compositor) : compositor_(compositor) {
compositor_->SetSwapCompletedWithSizeCallbackForTesting(base::BindRepeating(
&CompositorSwapRunLoop::DidSwap, base::Unretained(this)));
}
~CompositorSwapRunLoop() {
compositor_->SetSwapCompletedWithSizeCallbackForTesting(base::DoNothing());
}
void RunUntilSwap() { run_loop_.Run(); }
private:
void DidSwap(const gfx::Size& pixel_size) {
EXPECT_FALSE(pixel_size.IsEmpty());
run_loop_.Quit();
}
CompositorImpl* compositor_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(CompositorSwapRunLoop);
};
IN_PROC_BROWSER_TEST_P(CompositorImplBrowserTest,
CompositorImplReceivesSwapCallbacks) {
CompositorSwapRunLoop(compositor_impl()).RunUntilSwap();
}
} // namespace
} // namespace content
|