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 18#include "TestSceneBase.h" 19 20#include <SkGradientShader.h> 21 22class SimpleGradientAnimation; 23 24static TestScene::Registrar _SimpleGradient(TestScene::Info{ 25 "simpleGradient", 26 "A benchmark of shader performance of linear, 2 color gradients with black in them.", 27 TestScene::simpleCreateScene<SimpleGradientAnimation> 28}); 29 30class SimpleGradientAnimation : public TestScene { 31public: 32 std::vector< sp<RenderNode> > cards; 33 void createContent(int width, int height, Canvas& canvas) override { 34 canvas.drawColor(Color::White, SkBlendMode::kSrcOver); 35 36 sp<RenderNode> card = createCard(0, 0, width, height); 37 canvas.drawRenderNode(card.get()); 38 cards.push_back(card); 39 } 40 void doFrame(int frameNr) override { 41 int curFrame = frameNr % 20; 42 for (size_t ci = 0; ci < cards.size(); ci++) { 43 cards[ci]->mutateStagingProperties().setTranslationX(curFrame); 44 cards[ci]->mutateStagingProperties().setTranslationY(curFrame); 45 cards[ci]->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); 46 } 47 } 48private: 49 sp<RenderNode> createCard(int x, int y, int width, int height) { 50 return TestUtils::createNode(x, y, x + width, y + height, 51 [width, height](RenderProperties& props, Canvas& canvas) { 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 }); 64 } 65}; 66