blob: 80d7029857c471ed4b216b1d0c56700678171bb3 [file] [log] [blame]
John Reck94c40fe2014-10-08 09:28:43 -07001/*
2 * Copyright (C) 2014 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
John Reck94c40fe2014-10-08 09:28:43 -070017#include <cutils/log.h>
18#include <gui/Surface.h>
19#include <ui/PixelFormat.h>
20
21#include <AnimationContext.h>
Chris Craikdb663fe2015-04-20 13:34:45 -070022#include <DisplayListCanvas.h>
John Reck94c40fe2014-10-08 09:28:43 -070023#include <RenderNode.h>
24#include <renderthread/RenderProxy.h>
John Reck84e390c2015-01-05 09:42:52 -080025#include <renderthread/RenderTask.h>
John Reck94c40fe2014-10-08 09:28:43 -070026
27#include "TestContext.h"
28
John Reck7f2e5e32015-05-05 11:00:53 -070029#include <stdio.h>
30#include <unistd.h>
31
John Reck94c40fe2014-10-08 09:28:43 -070032using namespace android;
33using namespace android::uirenderer;
34using namespace android::uirenderer::renderthread;
John Reck84e390c2015-01-05 09:42:52 -080035using namespace android::uirenderer::test;
John Reck94c40fe2014-10-08 09:28:43 -070036
37class ContextFactory : public IContextFactory {
38public:
Chris Craikd41c4d82015-01-05 15:51:13 -080039 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
John Reck94c40fe2014-10-08 09:28:43 -070040 return new AnimationContext(clock);
41 }
42};
43
Chris Craikdb663fe2015-04-20 13:34:45 -070044static DisplayListCanvas* startRecording(RenderNode* node) {
45 DisplayListCanvas* renderer = new DisplayListCanvas();
Chris Craik03188872015-02-02 18:39:33 -080046 renderer->setViewport(node->stagingProperties().getWidth(),
47 node->stagingProperties().getHeight());
Tom Hudson8dfaa492014-12-09 15:03:44 -050048 renderer->prepare();
John Reck94c40fe2014-10-08 09:28:43 -070049 return renderer;
50}
51
Chris Craikdb663fe2015-04-20 13:34:45 -070052static void endRecording(DisplayListCanvas* renderer, RenderNode* node) {
John Reck94c40fe2014-10-08 09:28:43 -070053 renderer->finish();
54 node->setStagingDisplayList(renderer->finishRecording());
55 delete renderer;
56}
57
Chris Craik03188872015-02-02 18:39:33 -080058class TreeContentAnimation {
59public:
60 virtual ~TreeContentAnimation() {}
Tim Murraybfbcd882015-05-06 12:38:05 -070061 int frameCount = 150;
62 virtual int getFrameCount() { return frameCount; }
63 virtual void setFrameCount(int fc) {
64 if (fc > 0) {
65 frameCount = fc;
66 }
67 }
Chris Craikdb663fe2015-04-20 13:34:45 -070068 virtual void createContent(int width, int height, DisplayListCanvas* renderer) = 0;
Chris Craik03188872015-02-02 18:39:33 -080069 virtual void doFrame(int frameNr) = 0;
John Reck94c40fe2014-10-08 09:28:43 -070070
Chris Craik03188872015-02-02 18:39:33 -080071 template <class T>
Tim Murraybfbcd882015-05-06 12:38:05 -070072 static void run(int frameCount) {
Chris Craik03188872015-02-02 18:39:33 -080073 T animation;
Tim Murraybfbcd882015-05-06 12:38:05 -070074 animation.setFrameCount(frameCount);
John Reck94c40fe2014-10-08 09:28:43 -070075
Chris Craik03188872015-02-02 18:39:33 -080076 TestContext testContext;
John Reck94c40fe2014-10-08 09:28:43 -070077
Chris Craik03188872015-02-02 18:39:33 -080078 // create the native surface
79 const int width = gDisplay.w;
80 const int height = gDisplay.h;
81 sp<Surface> surface = testContext.surface();
John Reck94c40fe2014-10-08 09:28:43 -070082
Chris Craik03188872015-02-02 18:39:33 -080083 RenderNode* rootNode = new RenderNode();
84 rootNode->incStrong(nullptr);
85 rootNode->mutateStagingProperties().setLeftTopRightBottom(0, 0, width, height);
86 rootNode->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
87 rootNode->mutateStagingProperties().setClipToBounds(false);
88 rootNode->setPropertyFieldsDirty(RenderNode::GENERIC);
John Reck94c40fe2014-10-08 09:28:43 -070089
Chris Craik03188872015-02-02 18:39:33 -080090 ContextFactory factory;
91 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode, &factory));
92 proxy->loadSystemProperties();
93 proxy->initialize(surface);
94 float lightX = width / 2.0;
Alan Viverette50210d92015-05-14 18:05:36 -070095 proxy->setup(width, height, dp(800.0f), 255 * 0.075, 255 * 0.15);
96 proxy->setLightCenter((Vector3){lightX, dp(-200.0f), dp(800.0f)});
John Reck94c40fe2014-10-08 09:28:43 -070097
Chris Craik03188872015-02-02 18:39:33 -080098 android::uirenderer::Rect DUMMY;
John Reck94c40fe2014-10-08 09:28:43 -070099
Chris Craikdb663fe2015-04-20 13:34:45 -0700100 DisplayListCanvas* renderer = startRecording(rootNode);
Chris Craik03188872015-02-02 18:39:33 -0800101 animation.createContent(width, height, renderer);
102 endRecording(renderer, rootNode);
John Reck94c40fe2014-10-08 09:28:43 -0700103
John Reck7f2e5e32015-05-05 11:00:53 -0700104 // Do a few cold runs then reset the stats so that the caches are all hot
105 for (int i = 0; i < 3; i++) {
Chris Craik03188872015-02-02 18:39:33 -0800106 testContext.waitForVsync();
John Reck7f2e5e32015-05-05 11:00:53 -0700107 proxy->syncAndDrawFrame();
108 }
109 proxy->resetProfileInfo();
John Reck94c40fe2014-10-08 09:28:43 -0700110
John Reck7f2e5e32015-05-05 11:00:53 -0700111 for (int i = 0; i < animation.getFrameCount(); i++) {
112 testContext.waitForVsync();
113
Chris Craik03188872015-02-02 18:39:33 -0800114 ATRACE_NAME("UI-Draw Frame");
John Reck7f2e5e32015-05-05 11:00:53 -0700115 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
116 UiFrameInfoBuilder(proxy->frameInfo())
117 .setVsync(vsync, vsync);
Chris Craik03188872015-02-02 18:39:33 -0800118 animation.doFrame(i);
John Reckba6adf62015-02-19 14:36:50 -0800119 proxy->syncAndDrawFrame();
John Reck94c40fe2014-10-08 09:28:43 -0700120 }
Chris Craik03188872015-02-02 18:39:33 -0800121
John Reck7f2e5e32015-05-05 11:00:53 -0700122 proxy->dumpProfileInfo(STDOUT_FILENO, 0);
Chris Craik03188872015-02-02 18:39:33 -0800123 rootNode->decStrong(nullptr);
John Reck94c40fe2014-10-08 09:28:43 -0700124 }
Chris Craik03188872015-02-02 18:39:33 -0800125};
John Reck94c40fe2014-10-08 09:28:43 -0700126
Chris Craik03188872015-02-02 18:39:33 -0800127class ShadowGridAnimation : public TreeContentAnimation {
128public:
129 std::vector< sp<RenderNode> > cards;
Chris Craikdb663fe2015-04-20 13:34:45 -0700130 void createContent(int width, int height, DisplayListCanvas* renderer) override {
Chris Craik03188872015-02-02 18:39:33 -0800131 renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode);
132 renderer->insertReorderBarrier(true);
John Reck84e390c2015-01-05 09:42:52 -0800133
Chris Craik03188872015-02-02 18:39:33 -0800134 for (int x = dp(16); x < (width - dp(116)); x += dp(116)) {
135 for (int y = dp(16); y < (height - dp(116)); y += dp(116)) {
136 sp<RenderNode> card = createCard(x, y, dp(100), dp(100));
Chris Craik956f3402015-04-27 16:41:00 -0700137 renderer->drawRenderNode(card.get());
Chris Craik03188872015-02-02 18:39:33 -0800138 cards.push_back(card);
139 }
140 }
141
142 renderer->insertReorderBarrier(false);
143 }
144 void doFrame(int frameNr) override {
Tim Murraybfbcd882015-05-06 12:38:05 -0700145 int curFrame = frameNr % 150;
Andreas Gampe2ab82982014-11-21 14:19:06 -0800146 for (size_t ci = 0; ci < cards.size(); ci++) {
Tim Murraybfbcd882015-05-06 12:38:05 -0700147 cards[ci]->mutateStagingProperties().setTranslationX(curFrame);
148 cards[ci]->mutateStagingProperties().setTranslationY(curFrame);
John Reck94c40fe2014-10-08 09:28:43 -0700149 cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
150 }
John Reck94c40fe2014-10-08 09:28:43 -0700151 }
Chris Craik03188872015-02-02 18:39:33 -0800152private:
153 sp<RenderNode> createCard(int x, int y, int width, int height) {
154 sp<RenderNode> node = new RenderNode();
155 node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height);
156 node->mutateStagingProperties().setElevation(dp(16));
157 node->mutateStagingProperties().mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1);
158 node->mutateStagingProperties().mutableOutline().setShouldClip(true);
159 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::Z);
John Reck94c40fe2014-10-08 09:28:43 -0700160
Chris Craikdb663fe2015-04-20 13:34:45 -0700161 DisplayListCanvas* renderer = startRecording(node.get());
Chris Craik03188872015-02-02 18:39:33 -0800162 renderer->drawColor(0xFFEEEEEE, SkXfermode::kSrcOver_Mode);
163 endRecording(renderer, node.get());
164 return node;
165 }
166};
John Reck94c40fe2014-10-08 09:28:43 -0700167
Tim Murraybfbcd882015-05-06 12:38:05 -0700168class ShadowGrid2Animation : public TreeContentAnimation {
169public:
170 std::vector< sp<RenderNode> > cards;
171 void createContent(int width, int height, DisplayListCanvas* renderer) override {
172 renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode);
173 renderer->insertReorderBarrier(true);
174
175 for (int x = dp(8); x < (width - dp(58)); x += dp(58)) {
176 for (int y = dp(8); y < (height - dp(58)); y += dp(58)) {
177 sp<RenderNode> card = createCard(x, y, dp(50), dp(50));
178 renderer->drawRenderNode(card.get());
179 cards.push_back(card);
180 }
181 }
182
183 renderer->insertReorderBarrier(false);
184 }
185 void doFrame(int frameNr) override {
186 int curFrame = frameNr % 150;
187 for (size_t ci = 0; ci < cards.size(); ci++) {
188 cards[ci]->mutateStagingProperties().setTranslationX(curFrame);
189 cards[ci]->mutateStagingProperties().setTranslationY(curFrame);
190 cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
191 }
192 }
193private:
194 sp<RenderNode> createCard(int x, int y, int width, int height) {
195 sp<RenderNode> node = new RenderNode();
196 node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height);
197 node->mutateStagingProperties().setElevation(dp(16));
198 node->mutateStagingProperties().mutableOutline().setRoundRect(0, 0, width, height, dp(6), 1);
199 node->mutateStagingProperties().mutableOutline().setShouldClip(true);
200 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::Z);
201
202 DisplayListCanvas* renderer = startRecording(node.get());
203 renderer->drawColor(0xFFEEEEEE, SkXfermode::kSrcOver_Mode);
204 endRecording(renderer, node.get());
205 return node;
206 }
207};
208
Chris Craik03188872015-02-02 18:39:33 -0800209class RectGridAnimation : public TreeContentAnimation {
210public:
211 sp<RenderNode> card;
Chris Craikdb663fe2015-04-20 13:34:45 -0700212 void createContent(int width, int height, DisplayListCanvas* renderer) override {
Chris Craik03188872015-02-02 18:39:33 -0800213 renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode);
214 renderer->insertReorderBarrier(true);
215
216 card = createCard(40, 40, 200, 200);
Chris Craik956f3402015-04-27 16:41:00 -0700217 renderer->drawRenderNode(card.get());
Chris Craik03188872015-02-02 18:39:33 -0800218
219 renderer->insertReorderBarrier(false);
220 }
221 void doFrame(int frameNr) override {
Tim Murraybfbcd882015-05-06 12:38:05 -0700222 int curFrame = frameNr % 150;
223 card->mutateStagingProperties().setTranslationX(curFrame);
224 card->mutateStagingProperties().setTranslationY(curFrame);
Chris Craik03188872015-02-02 18:39:33 -0800225 card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
226 }
227private:
228 sp<RenderNode> createCard(int x, int y, int width, int height) {
229 sp<RenderNode> node = new RenderNode();
230 node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height);
231 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
232
Chris Craikdb663fe2015-04-20 13:34:45 -0700233 DisplayListCanvas* renderer = startRecording(node.get());
Chris Craik03188872015-02-02 18:39:33 -0800234 renderer->drawColor(0xFFFF00FF, SkXfermode::kSrcOver_Mode);
235
236 float rects[width * height];
237 int index = 0;
238 for (int xOffset = 0; xOffset < width; xOffset+=2) {
239 for (int yOffset = 0; yOffset < height; yOffset+=2) {
240 rects[index++] = xOffset;
241 rects[index++] = yOffset;
242 rects[index++] = xOffset + 1;
243 rects[index++] = yOffset + 1;
244 }
245 }
246 int count = width * height;
247
248 SkPaint paint;
249 paint.setColor(0xff00ffff);
250 renderer->drawRects(rects, count, &paint);
251
252 endRecording(renderer, node.get());
253 return node;
254 }
255};
256
Chris Craik117bdbc2015-02-05 10:12:38 -0800257class OvalAnimation : public TreeContentAnimation {
258public:
259 sp<RenderNode> card;
Chris Craikdb663fe2015-04-20 13:34:45 -0700260 void createContent(int width, int height, DisplayListCanvas* renderer) override {
Chris Craik117bdbc2015-02-05 10:12:38 -0800261 renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode);
262 renderer->insertReorderBarrier(true);
263
Chris Craik08fa43f2015-02-09 18:58:32 -0800264 card = createCard(40, 40, 400, 400);
Chris Craik956f3402015-04-27 16:41:00 -0700265 renderer->drawRenderNode(card.get());
Chris Craik117bdbc2015-02-05 10:12:38 -0800266
267 renderer->insertReorderBarrier(false);
268 }
269
270 void doFrame(int frameNr) override {
Tim Murraybfbcd882015-05-06 12:38:05 -0700271 int curFrame = frameNr % 150;
272 card->mutateStagingProperties().setTranslationX(curFrame);
273 card->mutateStagingProperties().setTranslationY(curFrame);
Chris Craik117bdbc2015-02-05 10:12:38 -0800274 card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
275 }
276private:
277 sp<RenderNode> createCard(int x, int y, int width, int height) {
278 sp<RenderNode> node = new RenderNode();
279 node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height);
280 node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
281
Chris Craikdb663fe2015-04-20 13:34:45 -0700282 DisplayListCanvas* renderer = startRecording(node.get());
Chris Craik117bdbc2015-02-05 10:12:38 -0800283
284 SkPaint paint;
285 paint.setAntiAlias(true);
Chris Craik08fa43f2015-02-09 18:58:32 -0800286 paint.setColor(0xFF000000);
Chris Craik117bdbc2015-02-05 10:12:38 -0800287 renderer->drawOval(0, 0, width, height, paint);
288
289 endRecording(renderer, node.get());
290 return node;
291 }
292};
293
Chris Craik03188872015-02-02 18:39:33 -0800294struct cstr_cmp {
295 bool operator()(const char *a, const char *b) const {
296 return std::strcmp(a, b) < 0;
297 }
298};
299
Tim Murraybfbcd882015-05-06 12:38:05 -0700300typedef void (*testProc)(int);
Chris Craik03188872015-02-02 18:39:33 -0800301
302std::map<const char*, testProc, cstr_cmp> gTestMap {
303 {"shadowgrid", TreeContentAnimation::run<ShadowGridAnimation>},
Tim Murraybfbcd882015-05-06 12:38:05 -0700304 {"shadowgrid2", TreeContentAnimation::run<ShadowGrid2Animation>},
Chris Craik03188872015-02-02 18:39:33 -0800305 {"rectgrid", TreeContentAnimation::run<RectGridAnimation> },
Chris Craik117bdbc2015-02-05 10:12:38 -0800306 {"oval", TreeContentAnimation::run<OvalAnimation> },
Chris Craik03188872015-02-02 18:39:33 -0800307};
308
309int main(int argc, char* argv[]) {
310 const char* testName = argc > 1 ? argv[1] : "shadowgrid";
311 testProc proc = gTestMap[testName];
312 if(!proc) {
313 printf("Error: couldn't find test %s\n", testName);
314 return 1;
315 }
Tim Murray1a0f1c72015-05-06 11:37:37 -0700316 int loopCount = 1;
317 if (argc > 2) {
318 loopCount = atoi(argv[2]);
319 if (!loopCount) {
320 printf("Invalid loop count!\n");
321 return 1;
322 }
323 }
Tim Murraybfbcd882015-05-06 12:38:05 -0700324 int frameCount = 150;
325 if (argc > 3) {
326 frameCount = atoi(argv[3]);
327 if (frameCount < 1) {
328 printf("Invalid frame count!\n");
329 return 1;
330 }
331 }
Tim Murray1a0f1c72015-05-06 11:37:37 -0700332 if (loopCount < 0) {
333 loopCount = INT_MAX;
334 }
335 for (int i = 0; i < loopCount; i++) {
Tim Murraybfbcd882015-05-06 12:38:05 -0700336 proc(frameCount);
Tim Murray1a0f1c72015-05-06 11:37:37 -0700337 }
John Reck94c40fe2014-10-08 09:28:43 -0700338 printf("Success!\n");
339 return 0;
340}