blob: c5af06160b62e17f568e14dbfefc02e14d788ebb [file] [log] [blame]
John Reck16c9d6a2015-11-17 15:51:08 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AnimationContext.h"
John Reck16c9d6a2015-11-17 15:51:08 -080018#include "RenderNode.h"
Chris Craik27e58b42015-12-07 10:01:38 -080019#include "tests/common/TestContext.h"
20#include "tests/common/TestScene.h"
Chris Craik8160f202015-12-02 14:50:25 -080021#include "tests/common/scenes/TestSceneBase.h"
John Reck16c9d6a2015-11-17 15:51:08 -080022#include "renderthread/RenderProxy.h"
23#include "renderthread/RenderTask.h"
24
25#include <cutils/log.h>
26#include <gui/Surface.h>
27#include <ui/PixelFormat.h>
28
29using namespace android;
30using namespace android::uirenderer;
31using namespace android::uirenderer::renderthread;
32using namespace android::uirenderer::test;
33
34class ContextFactory : public IContextFactory {
35public:
36 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
37 return new AnimationContext(clock);
38 }
39};
40
John Reck682573c2015-10-30 10:37:35 -070041template<class T>
42class ModifiedMovingAverage {
43public:
44 ModifiedMovingAverage(int weight) : mWeight(weight) {}
45
46 T add(T today) {
47 if (!mHasValue) {
48 mAverage = today;
49 } else {
50 mAverage = (((mWeight - 1) * mAverage) + today) / mWeight;
51 }
52 return mAverage;
53 }
54
55 T average() {
56 return mAverage;
57 }
58
59private:
60 bool mHasValue = false;
61 int mWeight;
62 T mAverage;
63};
64
Chris Craik27e58b42015-12-07 10:01:38 -080065void run(const TestScene::Info& info, const TestScene::Options& opts) {
John Reck16c9d6a2015-11-17 15:51:08 -080066 // Switch to the real display
67 gDisplay = getBuiltInDisplay();
68
69 std::unique_ptr<TestScene> scene(info.createScene(opts));
70
71 TestContext testContext;
72
73 // create the native surface
74 const int width = gDisplay.w;
75 const int height = gDisplay.h;
76 sp<Surface> surface = testContext.surface();
77
78 sp<RenderNode> rootNode = TestUtils::createNode(0, 0, width, height,
79 [&scene, width, height](RenderProperties& props, TestCanvas& canvas) {
80 props.setClipToBounds(false);
81 scene->createContent(width, height, canvas);
82 });
83
84 ContextFactory factory;
85 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false,
86 rootNode.get(), &factory));
87 proxy->loadSystemProperties();
88 proxy->initialize(surface);
89 float lightX = width / 2.0;
90 proxy->setup(width, height, dp(800.0f), 255 * 0.075, 255 * 0.15);
91 proxy->setLightCenter((Vector3){lightX, dp(-200.0f), dp(800.0f)});
92
93 // Do a few cold runs then reset the stats so that the caches are all hot
John Reck682573c2015-10-30 10:37:35 -070094 for (int i = 0; i < 5; i++) {
John Reck16c9d6a2015-11-17 15:51:08 -080095 testContext.waitForVsync();
96 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
97 UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
John Reck51f2d602016-04-06 07:50:47 -070098 proxy->syncAndDrawFrame(nullptr);
John Reck16c9d6a2015-11-17 15:51:08 -080099 }
John Reck682573c2015-10-30 10:37:35 -0700100
John Reck16c9d6a2015-11-17 15:51:08 -0800101 proxy->resetProfileInfo();
John Reck682573c2015-10-30 10:37:35 -0700102 proxy->fence();
103
104 ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
John Reck16c9d6a2015-11-17 15:51:08 -0800105
106 for (int i = 0; i < opts.count; i++) {
107 testContext.waitForVsync();
John Reck16c9d6a2015-11-17 15:51:08 -0800108 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700109 {
110 ATRACE_NAME("UI-Draw Frame");
111 UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
112 scene->doFrame(i);
John Reck51f2d602016-04-06 07:50:47 -0700113 proxy->syncAndDrawFrame(nullptr);
John Reck682573c2015-10-30 10:37:35 -0700114 }
John Reck682573c2015-10-30 10:37:35 -0700115 if (opts.reportFrametimeWeight) {
Chris Craik2705c982016-02-03 17:39:40 -0800116 proxy->fence();
117 nsecs_t done = systemTime(CLOCK_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700118 avgMs.add((done - vsync) / 1000000.0);
119 if (i % 10 == 9) {
120 printf("Average frametime %.3fms\n", avgMs.average());
121 }
122 }
John Reck16c9d6a2015-11-17 15:51:08 -0800123 }
124
John Recka41f2442016-04-07 16:36:57 -0700125 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
John Reck16c9d6a2015-11-17 15:51:08 -0800126}