blob: ff0cb3705cb8aa6a4ff82762e31599be36d5bce0 [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
17#include "TestSceneBase.h"
18
19#include <SkColorMatrixFilter.h>
20#include <SkGradientShader.h>
21
22class SimpleColorMatrixAnimation;
23
24static TestScene::Registrar _SimpleColorMatrix(TestScene::Info{
John Reck1bcacfd2017-11-03 10:12:19 -070025 "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 Craikf6a40902017-02-03 16:19:54 -080030
31class SimpleColorMatrixAnimation : public TestScene {
32public:
John Reck1bcacfd2017-11-03 10:12:19 -070033 std::vector<sp<RenderNode> > cards;
Chris Craikf6a40902017-02-03 16:19:54 -080034 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 Reck1bcacfd2017-11-03 10:12:19 -070049
Chris Craikf6a40902017-02-03 16:19:54 -080050private:
51 sp<RenderNode> createCard(int x, int y, int width, int height) {
John Reck1bcacfd2017-11-03 10:12:19 -070052 return TestUtils::createNode(
53 x, y, x + width, y + height,
Chris Craikf6a40902017-02-03 16:19:54 -080054 [width, height](RenderProperties& props, Canvas& canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -070055 SkPaint paint;
56 float matrix[20] = {0};
Chris Craikf6a40902017-02-03 16:19:54 -080057
John Reck1bcacfd2017-11-03 10:12:19 -070058 // 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 Craikf6a40902017-02-03 16:19:54 -080063
John Reck1bcacfd2017-11-03 10:12:19 -070064 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 Craikf6a40902017-02-03 16:19:54 -080068
John Reck1bcacfd2017-11-03 10:12:19 -070069 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
Chris Craikf6a40902017-02-03 16:19:54 -080070
John Reck1bcacfd2017-11-03 10:12:19 -070071 // 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 Craikf6a40902017-02-03 16:19:54 -080079
John Reck1bcacfd2017-11-03 10:12:19 -070080 // 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 Craikf6a40902017-02-03 16:19:54 -080085 }
86};