1 /*
2 * Copyright (C) 2016 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 "TestListViewSceneBase.h"
18
19 #include "TestContext.h"
20 #include "TestUtils.h"
21
22 #include <SkBlendMode.h>
23
24 #include <utils/Color.h>
25
26 namespace android {
27 namespace uirenderer {
28 namespace test {
29
createContent(int width,int height,Canvas & canvas)30 void TestListViewSceneBase::createContent(int width, int height, Canvas& canvas) {
31 srand(0);
32 mItemHeight = dp(60);
33 mItemSpacing = dp(16);
34 mItemWidth = std::min((height - mItemSpacing * 2), (int)dp(300));
35 mItemLeft = (width - mItemWidth) / 2;
36 int heightWithSpacing = mItemHeight + mItemSpacing;
37 for (int y = 0; y < height + (heightWithSpacing - 1); y += heightWithSpacing) {
38 int id = mListItems.size();
39 auto setup = std::bind(&TestListViewSceneBase::createListItem, this, std::placeholders::_1,
40 std::placeholders::_2, id, mItemWidth, mItemHeight);
41 auto node =
42 TestUtils::createNode(mItemLeft, y, mItemLeft + mItemWidth, y + mItemHeight, setup);
43 mListItems.push_back(node);
44 }
45 mListView = TestUtils::createNode(0, 0, width, height,
46 [this](RenderProperties& props, Canvas& canvas) {
47 for (size_t ci = 0; ci < mListItems.size(); ci++) {
48 canvas.drawRenderNode(mListItems[ci].get());
49 }
50 });
51
52 canvas.drawColor(Color::Grey_500, SkBlendMode::kSrcOver);
53 canvas.drawRenderNode(mListView.get());
54 }
55
doFrame(int frameNr)56 void TestListViewSceneBase::doFrame(int frameNr) {
57 int scrollPx = dp(frameNr) * 3;
58 int itemIndexOffset = scrollPx / (mItemSpacing + mItemHeight);
59 int pxOffset = -(scrollPx % (mItemSpacing + mItemHeight));
60
61 std::unique_ptr<Canvas> canvas(Canvas::create_recording_canvas(
62 mListView->stagingProperties().getWidth(), mListView->stagingProperties().getHeight(),
63 mListView.get()));
64 for (size_t ci = 0; ci < mListItems.size(); ci++) {
65 // update item position
66 auto listItem = mListItems[(ci + itemIndexOffset) % mListItems.size()];
67 int top = ((int)ci) * (mItemSpacing + mItemHeight) + pxOffset;
68 listItem->mutateStagingProperties().setLeftTopRightBottom(
69 mItemLeft, top, mItemLeft + mItemWidth, top + mItemHeight);
70 listItem->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
71
72 // draw it to parent DisplayList
73 canvas->drawRenderNode(mListItems[ci].get());
74 }
75 canvas->finishRecording(mListView.get());
76 }
77
78 } // namespace test
79 } // namespace uirenderer
80 } // namespace android
81