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 | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 29 | #include "protos/hwui.pb.h" |
| 30 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <unistd.h> |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame^] | 33 | #include <getopt.h> |
| 34 | #include <vector> |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 35 | |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 36 | using namespace android; |
| 37 | using namespace android::uirenderer; |
| 38 | using namespace android::uirenderer::renderthread; |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 39 | using namespace android::uirenderer::test; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 40 | |
| 41 | class ContextFactory : public IContextFactory { |
| 42 | public: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 43 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 44 | return new AnimationContext(clock); |
| 45 | } |
| 46 | }; |
| 47 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 48 | static DisplayListCanvas* startRecording(RenderNode* node) { |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 49 | DisplayListCanvas* renderer = new DisplayListCanvas( |
| 50 | node->stagingProperties().getWidth(), node->stagingProperties().getHeight()); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 51 | return renderer; |
| 52 | } |
| 53 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 54 | static void endRecording(DisplayListCanvas* renderer, RenderNode* node) { |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 55 | node->setStagingDisplayList(renderer->finishRecording()); |
| 56 | delete renderer; |
| 57 | } |
| 58 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 59 | class TreeContentAnimation { |
| 60 | public: |
| 61 | virtual ~TreeContentAnimation() {} |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 62 | int frameCount = 150; |
| 63 | virtual int getFrameCount() { return frameCount; } |
| 64 | virtual void setFrameCount(int fc) { |
| 65 | if (fc > 0) { |
| 66 | frameCount = fc; |
| 67 | } |
| 68 | } |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 69 | virtual void createContent(int width, int height, DisplayListCanvas* renderer) = 0; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 70 | virtual void doFrame(int frameNr) = 0; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 71 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 72 | template <class T> |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 73 | static void run(int frameCount) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 74 | T animation; |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 75 | animation.setFrameCount(frameCount); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 76 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 77 | TestContext testContext; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 78 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 79 | // create the native surface |
| 80 | const int width = gDisplay.w; |
| 81 | const int height = gDisplay.h; |
| 82 | sp<Surface> surface = testContext.surface(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 83 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 84 | RenderNode* rootNode = new RenderNode(); |
| 85 | rootNode->incStrong(nullptr); |
| 86 | rootNode->mutateStagingProperties().setLeftTopRightBottom(0, 0, width, height); |
| 87 | rootNode->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 88 | rootNode->mutateStagingProperties().setClipToBounds(false); |
| 89 | rootNode->setPropertyFieldsDirty(RenderNode::GENERIC); |
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 | ContextFactory factory; |
| 92 | std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode, &factory)); |
| 93 | proxy->loadSystemProperties(); |
| 94 | proxy->initialize(surface); |
| 95 | float lightX = width / 2.0; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 96 | proxy->setup(width, height, dp(800.0f), 255 * 0.075, 255 * 0.15); |
| 97 | proxy->setLightCenter((Vector3){lightX, dp(-200.0f), dp(800.0f)}); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 98 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 99 | android::uirenderer::Rect DUMMY; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 100 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 101 | DisplayListCanvas* renderer = startRecording(rootNode); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 102 | animation.createContent(width, height, renderer); |
| 103 | endRecording(renderer, rootNode); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 104 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 105 | // Do a few cold runs then reset the stats so that the caches are all hot |
| 106 | for (int i = 0; i < 3; i++) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 107 | testContext.waitForVsync(); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 108 | proxy->syncAndDrawFrame(); |
| 109 | } |
| 110 | proxy->resetProfileInfo(); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 111 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 112 | for (int i = 0; i < animation.getFrameCount(); i++) { |
| 113 | testContext.waitForVsync(); |
| 114 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 115 | ATRACE_NAME("UI-Draw Frame"); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 116 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
| 117 | UiFrameInfoBuilder(proxy->frameInfo()) |
| 118 | .setVsync(vsync, vsync); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 119 | animation.doFrame(i); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 120 | proxy->syncAndDrawFrame(); |
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 | |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 123 | proxy->dumpProfileInfo(STDOUT_FILENO, 0); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 124 | rootNode->decStrong(nullptr); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 125 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 126 | }; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 127 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 128 | class ShadowGridAnimation : public TreeContentAnimation { |
| 129 | public: |
| 130 | std::vector< sp<RenderNode> > cards; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 131 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 132 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 133 | renderer->insertReorderBarrier(true); |
John Reck | 84e390c | 2015-01-05 09:42:52 -0800 | [diff] [blame] | 134 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 135 | for (int x = dp(16); x < (width - dp(116)); x += dp(116)) { |
| 136 | for (int y = dp(16); y < (height - dp(116)); y += dp(116)) { |
| 137 | sp<RenderNode> card = createCard(x, y, dp(100), dp(100)); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 138 | renderer->drawRenderNode(card.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 139 | cards.push_back(card); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | renderer->insertReorderBarrier(false); |
| 144 | } |
| 145 | void doFrame(int frameNr) override { |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 146 | int curFrame = frameNr % 150; |
Andreas Gampe | 2ab8298 | 2014-11-21 14:19:06 -0800 | [diff] [blame] | 147 | for (size_t ci = 0; ci < cards.size(); ci++) { |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 148 | cards[ci]->mutateStagingProperties().setTranslationX(curFrame); |
| 149 | cards[ci]->mutateStagingProperties().setTranslationY(curFrame); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 150 | cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 151 | } |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 152 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 153 | private: |
| 154 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 155 | sp<RenderNode> node = new RenderNode(); |
| 156 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 157 | node->mutateStagingProperties().setElevation(dp(16)); |
| 158 | node->mutateStagingProperties().mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1); |
| 159 | node->mutateStagingProperties().mutableOutline().setShouldClip(true); |
| 160 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::Z); |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 161 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 162 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 163 | renderer->drawColor(0xFFEEEEEE, SkXfermode::kSrcOver_Mode); |
| 164 | endRecording(renderer, node.get()); |
| 165 | return node; |
| 166 | } |
| 167 | }; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 168 | |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 169 | class ShadowGrid2Animation : public TreeContentAnimation { |
| 170 | public: |
| 171 | std::vector< sp<RenderNode> > cards; |
| 172 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
| 173 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 174 | renderer->insertReorderBarrier(true); |
| 175 | |
| 176 | for (int x = dp(8); x < (width - dp(58)); x += dp(58)) { |
| 177 | for (int y = dp(8); y < (height - dp(58)); y += dp(58)) { |
| 178 | sp<RenderNode> card = createCard(x, y, dp(50), dp(50)); |
| 179 | renderer->drawRenderNode(card.get()); |
| 180 | cards.push_back(card); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | renderer->insertReorderBarrier(false); |
| 185 | } |
| 186 | void doFrame(int frameNr) override { |
| 187 | int curFrame = frameNr % 150; |
| 188 | for (size_t ci = 0; ci < cards.size(); ci++) { |
| 189 | cards[ci]->mutateStagingProperties().setTranslationX(curFrame); |
| 190 | cards[ci]->mutateStagingProperties().setTranslationY(curFrame); |
| 191 | cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 192 | } |
| 193 | } |
| 194 | private: |
| 195 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 196 | sp<RenderNode> node = new RenderNode(); |
| 197 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 198 | node->mutateStagingProperties().setElevation(dp(16)); |
| 199 | node->mutateStagingProperties().mutableOutline().setRoundRect(0, 0, width, height, dp(6), 1); |
| 200 | node->mutateStagingProperties().mutableOutline().setShouldClip(true); |
| 201 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::Z); |
| 202 | |
| 203 | DisplayListCanvas* renderer = startRecording(node.get()); |
| 204 | renderer->drawColor(0xFFEEEEEE, SkXfermode::kSrcOver_Mode); |
| 205 | endRecording(renderer, node.get()); |
| 206 | return node; |
| 207 | } |
| 208 | }; |
| 209 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 210 | class RectGridAnimation : public TreeContentAnimation { |
| 211 | public: |
| 212 | sp<RenderNode> card; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 213 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 214 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 215 | renderer->insertReorderBarrier(true); |
| 216 | |
| 217 | card = createCard(40, 40, 200, 200); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 218 | renderer->drawRenderNode(card.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 219 | |
| 220 | renderer->insertReorderBarrier(false); |
| 221 | } |
| 222 | void doFrame(int frameNr) override { |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 223 | int curFrame = frameNr % 150; |
| 224 | card->mutateStagingProperties().setTranslationX(curFrame); |
| 225 | card->mutateStagingProperties().setTranslationY(curFrame); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 226 | card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 227 | } |
| 228 | private: |
| 229 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 230 | sp<RenderNode> node = new RenderNode(); |
| 231 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 232 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 233 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 234 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 235 | renderer->drawColor(0xFFFF00FF, SkXfermode::kSrcOver_Mode); |
| 236 | |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 237 | SkRegion region; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 238 | for (int xOffset = 0; xOffset < width; xOffset+=2) { |
| 239 | for (int yOffset = 0; yOffset < height; yOffset+=2) { |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 240 | region.op(xOffset, yOffset, xOffset + 1, yOffset + 1, SkRegion::kUnion_Op); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 243 | |
| 244 | SkPaint paint; |
| 245 | paint.setColor(0xff00ffff); |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 246 | renderer->drawRegion(region, paint); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 247 | |
| 248 | endRecording(renderer, node.get()); |
| 249 | return node; |
| 250 | } |
| 251 | }; |
| 252 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 253 | class OvalAnimation : public TreeContentAnimation { |
| 254 | public: |
| 255 | sp<RenderNode> card; |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 256 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 257 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 258 | renderer->insertReorderBarrier(true); |
| 259 | |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 260 | card = createCard(40, 40, 400, 400); |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 261 | renderer->drawRenderNode(card.get()); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 262 | |
| 263 | renderer->insertReorderBarrier(false); |
| 264 | } |
| 265 | |
| 266 | void doFrame(int frameNr) override { |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 267 | int curFrame = frameNr % 150; |
| 268 | card->mutateStagingProperties().setTranslationX(curFrame); |
| 269 | card->mutateStagingProperties().setTranslationY(curFrame); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 270 | card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 271 | } |
| 272 | private: |
| 273 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
| 274 | sp<RenderNode> node = new RenderNode(); |
| 275 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 276 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 277 | |
Chris Craik | db663fe | 2015-04-20 13:34:45 -0700 | [diff] [blame] | 278 | DisplayListCanvas* renderer = startRecording(node.get()); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 279 | |
| 280 | SkPaint paint; |
| 281 | paint.setAntiAlias(true); |
Chris Craik | 08fa43f | 2015-02-09 18:58:32 -0800 | [diff] [blame] | 282 | paint.setColor(0xFF000000); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 283 | renderer->drawOval(0, 0, width, height, paint); |
| 284 | |
| 285 | endRecording(renderer, node.get()); |
| 286 | return node; |
| 287 | } |
| 288 | }; |
| 289 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 290 | class PartialInvalTest : public TreeContentAnimation { |
| 291 | public: |
| 292 | std::vector< sp<RenderNode> > cards; |
| 293 | void createContent(int width, int height, DisplayListCanvas* renderer) override { |
| 294 | static SkColor COLORS[] = { |
| 295 | 0xFFF44336, |
| 296 | 0xFF9C27B0, |
| 297 | 0xFF2196F3, |
| 298 | 0xFF4CAF50, |
| 299 | }; |
| 300 | |
| 301 | renderer->drawColor(0xFFFFFFFF, SkXfermode::kSrcOver_Mode); |
| 302 | |
| 303 | for (int x = dp(16); x < (width - dp(116)); x += dp(116)) { |
| 304 | for (int y = dp(16); y < (height - dp(116)); y += dp(116)) { |
| 305 | sp<RenderNode> card = createCard(x, y, dp(100), dp(100), |
| 306 | COLORS[static_cast<int>((y / dp(116))) % 4]); |
| 307 | renderer->drawRenderNode(card.get()); |
| 308 | cards.push_back(card); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | void doFrame(int frameNr) override { |
| 313 | int curFrame = frameNr % 150; |
| 314 | cards[0]->mutateStagingProperties().setTranslationX(curFrame); |
| 315 | cards[0]->mutateStagingProperties().setTranslationY(curFrame); |
| 316 | cards[0]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 317 | |
| 318 | DisplayListCanvas* renderer = startRecording(cards[0].get()); |
| 319 | renderer->drawColor(interpolateColor(curFrame / 150.0f, 0xFFF44336, 0xFFF8BBD0), |
| 320 | SkXfermode::kSrcOver_Mode); |
| 321 | endRecording(renderer, cards[0].get()); |
| 322 | } |
| 323 | |
| 324 | static SkColor interpolateColor(float fraction, SkColor start, SkColor end) { |
| 325 | int startA = (start >> 24) & 0xff; |
| 326 | int startR = (start >> 16) & 0xff; |
| 327 | int startG = (start >> 8) & 0xff; |
| 328 | int startB = start & 0xff; |
| 329 | |
| 330 | int endA = (end >> 24) & 0xff; |
| 331 | int endR = (end >> 16) & 0xff; |
| 332 | int endG = (end >> 8) & 0xff; |
| 333 | int endB = end & 0xff; |
| 334 | |
| 335 | return (int)((startA + (int)(fraction * (endA - startA))) << 24) | |
| 336 | (int)((startR + (int)(fraction * (endR - startR))) << 16) | |
| 337 | (int)((startG + (int)(fraction * (endG - startG))) << 8) | |
| 338 | (int)((startB + (int)(fraction * (endB - startB)))); |
| 339 | } |
| 340 | private: |
| 341 | sp<RenderNode> createCard(int x, int y, int width, int height, SkColor color) { |
| 342 | sp<RenderNode> node = new RenderNode(); |
| 343 | node->mutateStagingProperties().setLeftTopRightBottom(x, y, x + width, y + height); |
| 344 | node->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 345 | |
| 346 | DisplayListCanvas* renderer = startRecording(node.get()); |
| 347 | renderer->drawColor(color, SkXfermode::kSrcOver_Mode); |
| 348 | endRecording(renderer, node.get()); |
| 349 | return node; |
| 350 | } |
| 351 | }; |
| 352 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 353 | struct cstr_cmp { |
| 354 | bool operator()(const char *a, const char *b) const { |
| 355 | return std::strcmp(a, b) < 0; |
| 356 | } |
| 357 | }; |
| 358 | |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 359 | typedef void (*testProc)(int); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 360 | |
| 361 | std::map<const char*, testProc, cstr_cmp> gTestMap { |
| 362 | {"shadowgrid", TreeContentAnimation::run<ShadowGridAnimation>}, |
Tim Murray | bfbcd88 | 2015-05-06 12:38:05 -0700 | [diff] [blame] | 363 | {"shadowgrid2", TreeContentAnimation::run<ShadowGrid2Animation>}, |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 364 | {"rectgrid", TreeContentAnimation::run<RectGridAnimation> }, |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 365 | {"oval", TreeContentAnimation::run<OvalAnimation> }, |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 366 | {"partialinval", TreeContentAnimation::run<PartialInvalTest> }, |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 367 | }; |
| 368 | |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame^] | 369 | static int gFrameCount = 150; |
| 370 | static int gRepeatCount = 1; |
| 371 | static std::vector<testProc> gRunTests; |
| 372 | |
| 373 | static void printHelp() { |
| 374 | printf("\ |
| 375 | USAGE: hwuitest [OPTIONS] <TESTNAME>\n\ |
| 376 | \n\ |
| 377 | OPTIONS:\n\ |
| 378 | -c, --count=NUM NUM loops a test should run (example, number of frames)\n\ |
| 379 | -r, --runs=NUM Repeat the test(s) NUM times\n\ |
| 380 | -h, --help Display this help\n\ |
| 381 | --list List all tests\n\ |
| 382 | \n"); |
| 383 | } |
| 384 | |
| 385 | static void listTests() { |
| 386 | printf("Tests: \n"); |
| 387 | for (auto&& test : gTestMap) { |
| 388 | printf("%-20s <TODO DESCRIPTION>\n", test.first); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static const struct option LONG_OPTIONS[] = { |
| 393 | { "frames", required_argument, nullptr, 'f' }, |
| 394 | { "repeat", required_argument, nullptr, 'r' }, |
| 395 | { "help", no_argument, nullptr, 'h' }, |
| 396 | { "list", no_argument, nullptr, 'l' }, |
| 397 | { 0, 0, 0, 0 } |
| 398 | }; |
| 399 | |
| 400 | static const char* SHORT_OPTIONS = "c:r:h"; |
| 401 | |
| 402 | void parseOptions(int argc, char* argv[]) { |
| 403 | int c; |
| 404 | // temporary variable |
| 405 | int count; |
| 406 | bool error = false; |
| 407 | opterr = 0; |
| 408 | |
| 409 | while (true) { |
| 410 | |
| 411 | /* getopt_long stores the option index here. */ |
| 412 | int option_index = 0; |
| 413 | |
| 414 | c = getopt_long(argc, argv, SHORT_OPTIONS, LONG_OPTIONS, &option_index); |
| 415 | |
| 416 | if (c == -1) |
| 417 | break; |
| 418 | |
| 419 | switch (c) { |
| 420 | case 0: |
| 421 | // Option set a flag, don't need to do anything |
| 422 | // (although none of the current LONG_OPTIONS do this...) |
| 423 | break; |
| 424 | |
| 425 | case 'l': |
| 426 | listTests(); |
| 427 | exit(EXIT_SUCCESS); |
| 428 | break; |
| 429 | |
| 430 | case 'c': |
| 431 | count = atoi(optarg); |
| 432 | if (!count) { |
| 433 | fprintf(stderr, "Invalid frames argument '%s'\n", optarg); |
| 434 | error = true; |
| 435 | } else { |
| 436 | gFrameCount = (count > 0 ? count : INT_MAX); |
| 437 | } |
| 438 | break; |
| 439 | |
| 440 | case 'r': |
| 441 | count = atoi(optarg); |
| 442 | if (!count) { |
| 443 | fprintf(stderr, "Invalid repeat argument '%s'\n", optarg); |
| 444 | error = true; |
| 445 | } else { |
| 446 | gRepeatCount = (count > 0 ? count : INT_MAX); |
| 447 | } |
| 448 | break; |
| 449 | |
| 450 | case 'h': |
| 451 | printHelp(); |
| 452 | exit(EXIT_SUCCESS); |
| 453 | break; |
| 454 | |
| 455 | case '?': |
| 456 | fprintf(stderr, "Unrecognized option '%s'\n", argv[optind - 1]); |
| 457 | // fall-through |
| 458 | default: |
| 459 | error = true; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (error) { |
| 465 | fprintf(stderr, "Try 'hwuitest --help' for more information.\n"); |
| 466 | exit(EXIT_FAILURE); |
| 467 | } |
| 468 | |
| 469 | /* Print any remaining command line arguments (not options). */ |
| 470 | if (optind < argc) { |
| 471 | do { |
| 472 | const char* test = argv[optind++]; |
| 473 | auto pos = gTestMap.find(test); |
| 474 | if (pos == gTestMap.end()) { |
| 475 | fprintf(stderr, "Unknown test '%s'\n", test); |
| 476 | exit(EXIT_FAILURE); |
| 477 | } else { |
| 478 | gRunTests.push_back(pos->second); |
| 479 | } |
| 480 | } while (optind < argc); |
| 481 | } else { |
| 482 | gRunTests.push_back(gTestMap["shadowgrid"]); |
| 483 | } |
| 484 | } |
| 485 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 486 | int main(int argc, char* argv[]) { |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame^] | 487 | parseOptions(argc, argv); |
| 488 | |
| 489 | for (int i = 0; i < gRepeatCount; i++) { |
| 490 | for (auto&& test : gRunTests) { |
| 491 | test(gFrameCount); |
Tim Murray | 1a0f1c7 | 2015-05-06 11:37:37 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 494 | printf("Success!\n"); |
| 495 | return 0; |
| 496 | } |