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