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