Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 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 | |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 17 | #include "TestSceneBase.h" |
| 18 | |
| 19 | #include <SkGradientShader.h> |
| 20 | |
| 21 | class SimpleGradientAnimation; |
| 22 | |
| 23 | static TestScene::Registrar _SimpleGradient(TestScene::Info{ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | "simpleGradient", |
| 25 | "A benchmark of shader performance of linear, 2 color gradients with black in them.", |
| 26 | TestScene::simpleCreateScene<SimpleGradientAnimation>}); |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 27 | |
| 28 | class SimpleGradientAnimation : public TestScene { |
| 29 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | std::vector<sp<RenderNode> > cards; |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 31 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 47 | private: |
| 48 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | return TestUtils::createNode( |
| 50 | x, y, x + width, y + height, |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 51 | [width, height](RenderProperties& props, Canvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | 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 Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 64 | } |
| 65 | }; |