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