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 22class SimpleColorMatrixAnimation; 23 24static TestScene::Registrar _SimpleColorMatrix(TestScene::Info{ 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>}); 30 31class SimpleColorMatrixAnimation : public TestScene { 32public: 33 std::vector<sp<RenderNode> > cards; 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 } 49 50private: 51 sp<RenderNode> createCard(int x, int y, int width, int height) { 52 return TestUtils::createNode( 53 x, y, x + width, y + height, 54 [width, height](RenderProperties& props, Canvas& canvas) { 55 SkPaint paint; 56 float matrix[20] = {0}; 57 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; 63 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; 68 69 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); 70 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)); 79 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 }); 85 } 86}; 87