SimpleColorMatrixAnimation.cpp revision f6a409067c5df1593c8c6b94304c70220d37901c
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 B " 27 "all scaled and translated the same amount.", 28 TestScene::simpleCreateScene<SimpleColorMatrixAnimation> 29}); 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 } 49private: 50 sp<RenderNode> createCard(int x, int y, int width, int height) { 51 return TestUtils::createNode(x, y, x + width, y + height, 52 [width, height](RenderProperties& props, Canvas& canvas) { 53 SkPaint paint; 54 float matrix[20] = { 0 }; 55 56 // Simple scale/translate case where R, G, and B are all treated equivalently 57 matrix[SkColorMatrix::kR_Scale] = 1.1f; 58 matrix[SkColorMatrix::kG_Scale] = 1.1f; 59 matrix[SkColorMatrix::kB_Scale] = 1.1f; 60 matrix[SkColorMatrix::kA_Scale] = 0.5f; 61 62 matrix[SkColorMatrix::kR_Trans] = 5.0f; 63 matrix[SkColorMatrix::kG_Trans] = 5.0f; 64 matrix[SkColorMatrix::kB_Trans] = 5.0f; 65 matrix[SkColorMatrix::kA_Trans] = 10.0f; 66 67 paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); 68 69 // set a shader so it's not likely for the matrix to be optimized away (since a clever 70 // enough renderer might apply it directly to the paint color) 71 float pos[] = { 0, 1 }; 72 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(width, height) }; 73 SkColor colors[2] = { Color::DeepPurple_500, Color::DeepOrange_500 }; 74 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, 2, 75 SkShader::kClamp_TileMode)); 76 77 // overdraw several times to emphasize shader cost 78 for (int i = 0; i < 10; i++) { 79 canvas.drawRect(i, i, width, height, paint); 80 } 81 }); 82 } 83}; 84