blob: 016c65c17c4c9d685e19b4c2384a155ebcd67da3 [file] [log] [blame]
Chris Craikf6a40902017-02-03 16:19:54 -08001/*
2 * Copyright (C) 2017 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
Chris Craikf6a40902017-02-03 16:19:54 -080017#include "TestSceneBase.h"
18
19#include <SkGradientShader.h>
20
21class SimpleGradientAnimation;
22
23static TestScene::Registrar _SimpleGradient(TestScene::Info{
John Reck1bcacfd2017-11-03 10:12:19 -070024 "simpleGradient",
25 "A benchmark of shader performance of linear, 2 color gradients with black in them.",
26 TestScene::simpleCreateScene<SimpleGradientAnimation>});
Chris Craikf6a40902017-02-03 16:19:54 -080027
28class SimpleGradientAnimation : public TestScene {
29public:
John Reck1bcacfd2017-11-03 10:12:19 -070030 std::vector<sp<RenderNode> > cards;
Chris Craikf6a40902017-02-03 16:19:54 -080031 void createContent(int width, int height, Canvas& canvas) override {
32 canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
33
34 sp<RenderNode> card = createCard(0, 0, width, height);
35 canvas.drawRenderNode(card.get());
36 cards.push_back(card);
37 }
38 void doFrame(int frameNr) override {
39 int curFrame = frameNr % 20;
40 for (size_t ci = 0; ci < cards.size(); ci++) {
41 cards[ci]->mutateStagingProperties().setTranslationX(curFrame);
42 cards[ci]->mutateStagingProperties().setTranslationY(curFrame);
43 cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
44 }
45 }
John Reck1bcacfd2017-11-03 10:12:19 -070046
Chris Craikf6a40902017-02-03 16:19:54 -080047private:
48 sp<RenderNode> createCard(int x, int y, int width, int height) {
John Reck1bcacfd2017-11-03 10:12:19 -070049 return TestUtils::createNode(
50 x, y, x + width, y + height,
Chris Craikf6a40902017-02-03 16:19:54 -080051 [width, height](RenderProperties& props, Canvas& canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -070052 float pos[] = {0, 1};
53 SkPoint pts[] = {SkPoint::Make(0, 0), SkPoint::Make(width, height)};
54 SkPaint paint;
55 // overdraw several times to emphasize shader cost
56 for (int i = 0; i < 10; i++) {
57 // use i%2 start position to pick 2 color combo with black in it
58 SkColor colors[3] = {Color::Transparent, Color::Black, Color::Cyan_500};
59 paint.setShader(SkGradientShader::MakeLinear(pts, colors + (i % 2), pos, 2,
60 SkShader::kClamp_TileMode));
61 canvas.drawRect(i, i, width, height, paint);
62 }
63 });
Chris Craikf6a40902017-02-03 16:19:54 -080064 }
65};