1 /*
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 
17 #include "TestSceneBase.h"
18 
19 #include <SkBlendMode.h>
20 #include <SkGradientShader.h>
21 
22 class SimpleGradientAnimation;
23 
24 static TestScene::Registrar _SimpleGradient(TestScene::Info{
25         "simpleGradient",
26         "A benchmark of shader performance of linear, 2 color gradients with black in them.",
27         TestScene::simpleCreateScene<SimpleGradientAnimation>});
28 
29 class SimpleGradientAnimation : public TestScene {
30 public:
31     std::vector<sp<RenderNode> > cards;
createContent(int width,int height,Canvas & canvas)32     void createContent(int width, int height, Canvas& canvas) override {
33         canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
34 
35         sp<RenderNode> card = createCard(0, 0, width, height);
36         canvas.drawRenderNode(card.get());
37         cards.push_back(card);
38     }
doFrame(int frameNr)39     void doFrame(int frameNr) override {
40         int curFrame = frameNr % 20;
41         for (size_t ci = 0; ci < cards.size(); ci++) {
42             cards[ci]->mutateStagingProperties().setTranslationX(curFrame);
43             cards[ci]->mutateStagingProperties().setTranslationY(curFrame);
44             cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
45         }
46     }
47 
48 private:
createCard(int x,int y,int width,int height)49     sp<RenderNode> createCard(int x, int y, int width, int height) {
50         return TestUtils::createNode(
51                 x, y, x + width, y + height,
52                 [width, height](RenderProperties& props, Canvas& canvas) {
53                     float pos[] = {0, 1};
54                     SkPoint pts[] = {SkPoint::Make(0, 0), SkPoint::Make(width, height)};
55                     Paint paint;
56                     // overdraw several times to emphasize shader cost
57                     for (int i = 0; i < 10; i++) {
58                         // use i%2 start position to pick 2 color combo with black in it
59                         SkColor colors[3] = {Color::Transparent, Color::Black, Color::Cyan_500};
60                         paint.setShader(SkGradientShader::MakeLinear(pts, colors + (i % 2), pos, 2,
61                                                                      SkTileMode::kClamp));
62                         canvas.drawRect(i, i, width, height, paint);
63                     }
64                 });
65     }
66 };
67