Home | History | Annotate | Download | only in scenes
      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 #include "renderthread/RenderProxy.h"
     19 #include "utils/Color.h"
     20 #include "hwui/Paint.h"
     21 
     22 class MagnifierAnimation;
     23 
     24 static TestScene::Registrar _Magnifier(TestScene::Info{
     25         "magnifier", "A sample magnifier using Readback",
     26         TestScene::simpleCreateScene<MagnifierAnimation>});
     27 
     28 class MagnifierAnimation : public TestScene {
     29 public:
     30     sp<RenderNode> card;
     31     sp<RenderNode> zoomImageView;
     32 
     33     void createContent(int width, int height, Canvas& canvas) override {
     34         magnifier = TestUtils::createBitmap(200, 100);
     35         SkBitmap temp;
     36         magnifier->getSkBitmap(&temp);
     37         temp.eraseColor(Color::White);
     38         canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
     39         card = TestUtils::createNode(
     40                 0, 0, width, height, [&](RenderProperties& props, Canvas& canvas) {
     41                     Paint paint;
     42                     paint.setAntiAlias(true);
     43                     paint.getSkFont().setSize(50);
     44 
     45                     paint.setColor(Color::Black);
     46                     TestUtils::drawUtf8ToCanvas(&canvas, "Test string", paint, 10, 400);
     47                 });
     48         canvas.drawRenderNode(card.get());
     49         zoomImageView = TestUtils::createNode(
     50                 100, 100, 500, 300, [&](RenderProperties& props, Canvas& canvas) {
     51                     props.setElevation(dp(16));
     52                     props.mutableOutline().setRoundRect(0, 0, props.getWidth(), props.getHeight(),
     53                                                         dp(6), 1);
     54                     props.mutableOutline().setShouldClip(true);
     55                     canvas.drawBitmap(*magnifier, 0.0f, 0.0f, (float)magnifier->width(),
     56                                       (float)magnifier->height(), 0, 0, (float)props.getWidth(),
     57                                       (float)props.getHeight(), nullptr);
     58                 });
     59         canvas.insertReorderBarrier(true);
     60         canvas.drawRenderNode(zoomImageView.get());
     61         canvas.insertReorderBarrier(false);
     62     }
     63 
     64     void doFrame(int frameNr) override {
     65         int curFrame = frameNr % 150;
     66         card->mutateStagingProperties().setTranslationX(curFrame);
     67         card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
     68         if (renderTarget) {
     69             SkBitmap temp;
     70             magnifier->getSkBitmap(&temp);
     71             constexpr int x = 90;
     72             constexpr int y = 325;
     73             RenderProxy::copySurfaceInto(renderTarget, x, y, x + magnifier->width(),
     74                                          y + magnifier->height(), &temp);
     75         }
     76     }
     77 
     78     sk_sp<Bitmap> magnifier;
     79 };
     80