John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 17 | #include <cutils/log.h> |
| 18 | #include <gui/Surface.h> |
| 19 | #include <ui/PixelFormat.h> |
| 20 | |
| 21 | #include <AnimationContext.h> |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 22 | #include <DisplayListCanvas.h> |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 23 | #include <RenderNode.h> |
| 24 | #include <renderthread/RenderProxy.h> |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 25 | #include <renderthread/RenderTask.h> |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 26 | |
| 27 | #include "TestContext.h" |
| 28 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 32 | using namespace android; |
| 33 | using namespace android::uirenderer; |
| 34 | using namespace android::uirenderer::renderthread; |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 35 | using namespace android::uirenderer::test; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 36 | |
| 37 | class ContextFactory : public IContextFactory { |
| 38 | public: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 39 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 40 | return new AnimationContext(clock); |
| 41 | } |
| 42 | }; |
| 43 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 44 | static DisplayListCanvas* startRecording(RenderNode* node) { |
| 45 | DisplayListCanvas* renderer = new DisplayListCanvas(); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 46 | renderer->setViewport(node->stagingProperties().getWidth(), |
| 47 | node->stagingProperties().getHeight()); |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 48 | renderer->prepare(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 49 | return renderer; |
| 50 | } |
| 51 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 52 | static void endRecording(DisplayListCanvas* renderer, RenderNode* node) { |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 53 | renderer->finish(); |
| 54 | node->setStagingDisplayList(renderer->finishRecording()); |
| 55 | delete renderer; |
| 56 | } |
| 57 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 58 | class TreeContentAnimation { |
| 59 | public: |
| 60 | virtual ~TreeContentAnimation() {} |
| 61 | virtual int getFrameCount() { return 150; } |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 62 | virtual void createContent(int width, int height, DisplayListCanvas* renderer) = 0; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 63 | virtual void doFrame(int frameNr) = 0; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 64 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 65 | template <class T> |
| 66 | static void run() { |
| 67 | T animation; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 68 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 69 | TestContext testContext; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 70 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 71 | // create the native surface |
| 72 | const int width = gDisplay.w; |
| 73 | const int height = gDisplay.h; |
| 74 | sp<Surface> surface = testContext.surface(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 75 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 76 | RenderNode* rootNode = new RenderNode(); |
| 77 | rootNode->incStrong(nullptr); |
| 78 | rootNode->mutateStagingProperties().setLeftTopRightBottom(0, 0, width, height); |
| 79 | rootNode->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 80 | rootNode->mutateStagingProperties().setClipToBounds(false); |
| 81 | rootNode->setPropertyFieldsDirty(RenderNode::GENERIC); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 82 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 83 | ContextFactory factory; |
| 84 | std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode, &factory)); |
| 85 | proxy->loadSystemProperties(); |
| 86 | proxy->initialize(surface); |
| 87 | float lightX = width / 2.0; |
| 88 | proxy->setup(width, height, (Vector3){lightX, dp(-200.0f), dp(800.0f)}, |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 89 | dp(800.0f), 255 * 0.075, 255 * 0.15); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 90 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 91 | android::uirenderer::Rect DUMMY; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 92 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 93 | DisplayListCanvas* renderer = startRecording(rootNode); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 94 | animation.createContent(width, height, renderer); |
| 95 | endRecording(renderer, rootNode); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 96 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 97 | // Do a few cold runs then reset the stats so that the caches are all hot |
| 98 | for (int i = 0; i < 3; i++) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 99 | testContext.waitForVsync(); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 100 | proxy->syncAndDrawFrame(); |
| 101 | } |
| 102 | proxy->resetProfileInfo(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 103 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 104 | for (int i = 0; i < animation.getFrameCount(); i++) { |
| 105 | testContext.waitForVsync(); |
| 106 | |
| 107 | // workaround b/20853441 |
| 108 | proxy->fence(); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 109 | ATRACE_NAME("UI-Draw Frame"); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 110 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
| 111 | UiFrameInfoBuilder(proxy->frameInfo()) |
| 112 | .setVsync(vsync, vsync); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 113 | animation.doFrame(i); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 114 | proxy->syncAndDrawFrame(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 115 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 116 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 117 | proxy->dumpProfileInfo(STDOUT_FILENO, 0); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 118 | rootNode->decStrong(nullptr); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 119 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 120 | }; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 121 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 122 | class ShadowGridAnimation : public TreeContentAnimation { |
| 123 | public: |
| 124 | std::vector< sp<RenderNode> > cards; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 125 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 126 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 127 | renderer->insertReorderBarrier(true); |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 128 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 129 | for (int x = dp(16); x < (width - dp(116)); x += dp(116)) { |
| 130 | for (int y = dp(16); y < (height - dp(116)); y += dp(116)) { |
| 131 | sp<RenderNode> card = createCard(x, y, dp(100), dp(100)); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 132 | renderer->drawRenderNode(card.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 133 | cards.push_back(card); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | renderer->insertReorderBarrier(false); |
| 138 | } |
| 139 | void doFrame(int frameNr) override { |
Andreas Gampe | 2ab8298 | 2014-11-21 14:19:06 -0800 | [diff] [blame] | 140 | for (size_t ci = 0; ci < cards.size(); ci++) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 141 | cards[ci]->mutateStagingProperties().setTranslationX(frameNr); |
| 142 | cards[ci]->mutateStagingProperties().setTranslationY(frameNr); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 143 | cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 144 | } |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 145 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 146 | private: |
| 147 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 148 | sp<RenderNode> node = new RenderNode(); |
| 149 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 150 | node->mutateStagingProperties().setElevation(dp(16)); |
| 151 | node->mutateStagingProperties().mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1); |
| 152 | node->mutateStagingProperties().mutableOutline().setShouldClip(true); |
| 153 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::Z); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 154 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 155 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 156 | renderer->drawColor(0xFFEEEEEE, SkXfermode::kSrcOver_Mode); |
| 157 | endRecording(renderer, node.get()); |
| 158 | return node; |
| 159 | } |
| 160 | }; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 161 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 162 | class RectGridAnimation : public TreeContentAnimation { |
| 163 | public: |
| 164 | sp<RenderNode> card; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 165 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 166 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 167 | renderer->insertReorderBarrier(true); |
| 168 | |
| 169 | card = createCard(40, 40, 200, 200); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 170 | renderer->drawRenderNode(card.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 171 | |
| 172 | renderer->insertReorderBarrier(false); |
| 173 | } |
| 174 | void doFrame(int frameNr) override { |
| 175 | card->mutateStagingProperties().setTranslationX(frameNr); |
| 176 | card->mutateStagingProperties().setTranslationY(frameNr); |
| 177 | card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 178 | } |
| 179 | private: |
| 180 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 181 | sp<RenderNode> node = new RenderNode(); |
| 182 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 183 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 184 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 185 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 186 | renderer->drawColor(0xFFFF00FF, SkXfermode::kSrcOver_Mode); |
| 187 | |
| 188 | float rects[width * height]; |
| 189 | int index = 0; |
| 190 | for (int xOffset = 0; xOffset < width; xOffset+=2) { |
| 191 | for (int yOffset = 0; yOffset < height; yOffset+=2) { |
| 192 | rects[index++] = xOffset; |
| 193 | rects[index++] = yOffset; |
| 194 | rects[index++] = xOffset + 1; |
| 195 | rects[index++] = yOffset + 1; |
| 196 | } |
| 197 | } |
| 198 | int count = width * height; |
| 199 | |
| 200 | SkPaint paint; |
| 201 | paint.setColor(0xff00ffff); |
| 202 | renderer->drawRects(rects, count, &paint); |
| 203 | |
| 204 | endRecording(renderer, node.get()); |
| 205 | return node; |
| 206 | } |
| 207 | }; |
| 208 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 209 | class OvalAnimation : public TreeContentAnimation { |
| 210 | public: |
| 211 | sp<RenderNode> card; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 212 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 213 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 214 | renderer->insertReorderBarrier(true); |
| 215 | |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 216 | card = createCard(40, 40, 400, 400); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 217 | renderer->drawRenderNode(card.get()); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 218 | |
| 219 | renderer->insertReorderBarrier(false); |
| 220 | } |
| 221 | |
| 222 | void doFrame(int frameNr) override { |
| 223 | card->mutateStagingProperties().setTranslationX(frameNr); |
| 224 | card->mutateStagingProperties().setTranslationY(frameNr); |
| 225 | card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 226 | } |
| 227 | private: |
| 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 Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 233 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 234 | |
| 235 | SkPaint paint; |
| 236 | paint.setAntiAlias(true); |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 237 | paint.setColor(0xFF000000); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 238 | renderer->drawOval(0, 0, width, height, paint); |
| 239 | |
| 240 | endRecording(renderer, node.get()); |
| 241 | return node; |
| 242 | } |
| 243 | }; |
| 244 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 245 | struct cstr_cmp { |
| 246 | bool operator()(const char *a, const char *b) const { |
| 247 | return std::strcmp(a, b) < 0; |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | typedef void (*testProc)(); |
| 252 | |
| 253 | std::map<const char*, testProc, cstr_cmp> gTestMap { |
| 254 | {"shadowgrid", TreeContentAnimation::run<ShadowGridAnimation>}, |
| 255 | {"rectgrid", TreeContentAnimation::run<RectGridAnimation> }, |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 256 | {"oval", TreeContentAnimation::run<OvalAnimation> }, |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | int main(int argc, char* argv[]) { |
| 260 | const char* testName = argc > 1 ? argv[1] : "shadowgrid"; |
| 261 | testProc proc = gTestMap[testName]; |
| 262 | if(!proc) { |
| 263 | printf("Error: couldn't find test %s\n", testName); |
| 264 | return 1; |
| 265 | } |
Tim Murray | 1a0f1c7 | 2015-05-06 11:37:37 -0700 | [diff] [blame^] | 266 | int loopCount = 1; |
| 267 | if (argc > 2) { |
| 268 | loopCount = atoi(argv[2]); |
| 269 | if (!loopCount) { |
| 270 | printf("Invalid loop count!\n"); |
| 271 | return 1; |
| 272 | } |
| 273 | } |
| 274 | if (loopCount < 0) { |
| 275 | loopCount = INT_MAX; |
| 276 | } |
| 277 | for (int i = 0; i < loopCount; i++) { |
| 278 | proc(); |
| 279 | } |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 280 | printf("Success!\n"); |
| 281 | return 0; |
| 282 | } |