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 | |
| 17 | #include "TestSceneBase.h" |
| 18 | |
| 19 | #include <SkColorMatrixFilter.h> |
| 20 | #include <SkGradientShader.h> |
| 21 | |
| 22 | class SimpleColorMatrixAnimation; |
| 23 | |
| 24 | static TestScene::Registrar _SimpleColorMatrix(TestScene::Info{ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | "simpleColorMatrix", |
| 26 | "A color matrix shader benchmark for the simple scale/translate case, which has R, G, and " |
| 27 | "B " |
| 28 | "all scaled and translated the same amount.", |
| 29 | TestScene::simpleCreateScene<SimpleColorMatrixAnimation>}); |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 30 | |
| 31 | class SimpleColorMatrixAnimation : public TestScene { |
| 32 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | std::vector<sp<RenderNode> > cards; |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 34 | void createContent(int width, int height, Canvas& canvas) override { |
| 35 | canvas.drawColor(Color::White, SkBlendMode::kSrcOver); |
| 36 | |
| 37 | sp<RenderNode> card = createCard(0, 0, width, height); |
| 38 | canvas.drawRenderNode(card.get()); |
| 39 | cards.push_back(card); |
| 40 | } |
| 41 | void doFrame(int frameNr) override { |
| 42 | int curFrame = frameNr % 20; |
| 43 | for (size_t ci = 0; ci < cards.size(); ci++) { |
| 44 | cards[ci]->mutateStagingProperties().setTranslationX(curFrame); |
| 45 | cards[ci]->mutateStagingProperties().setTranslationY(curFrame); |
| 46 | cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); |
| 47 | } |
| 48 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 50 | private: |
| 51 | sp<RenderNode> createCard(int x, int y, int width, int height) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | return TestUtils::createNode( |
| 53 | x, y, x + width, y + height, |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 54 | [width, height](RenderProperties& props, Canvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | float matrix[20] = {0}; |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 57 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 58 | // Simple scale/translate case where R, G, and B are all treated equivalently |
| 59 | matrix[SkColorMatrix::kR_Scale] = 1.1f; |
| 60 | matrix[SkColorMatrix::kG_Scale] = 1.1f; |
| 61 | matrix[SkColorMatrix::kB_Scale] = 1.1f; |
| 62 | matrix[SkColorMatrix::kA_Scale] = 0.5f; |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 63 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | matrix[SkColorMatrix::kR_Trans] = 5.0f; |
| 65 | matrix[SkColorMatrix::kG_Trans] = 5.0f; |
| 66 | matrix[SkColorMatrix::kB_Trans] = 5.0f; |
| 67 | matrix[SkColorMatrix::kA_Trans] = 10.0f; |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 68 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 70 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | // set a shader so it's not likely for the matrix to be optimized away (since a |
| 72 | // clever |
| 73 | // enough renderer might apply it directly to the paint color) |
| 74 | float pos[] = {0, 1}; |
| 75 | SkPoint pts[] = {SkPoint::Make(0, 0), SkPoint::Make(width, height)}; |
| 76 | SkColor colors[2] = {Color::DeepPurple_500, Color::DeepOrange_500}; |
| 77 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, |
| 78 | SkShader::kClamp_TileMode)); |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 79 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 80 | // overdraw several times to emphasize shader cost |
| 81 | for (int i = 0; i < 10; i++) { |
| 82 | canvas.drawRect(i, i, width, height, paint); |
| 83 | } |
| 84 | }); |
Chris Craik | f6a4090 | 2017-02-03 16:19:54 -0800 | [diff] [blame] | 85 | } |
| 86 | }; |