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 #include <SkBitmap.h> 23 #include <SkBlendMode.h> 24 #include <SkFont.h> 25 26 class MagnifierAnimation; 27 28 using Rect = android::uirenderer::Rect; 29 30 static TestScene::Registrar _Magnifier(TestScene::Info{ 31 "magnifier", "A sample magnifier using Readback", 32 TestScene::simpleCreateScene<MagnifierAnimation>}); 33 34 class BlockingCopyRequest : public CopyRequest { 35 sk_sp<Bitmap> mDestination; 36 std::mutex mLock; 37 std::condition_variable mCondVar; 38 CopyResult mResult; 39 40 public: BlockingCopyRequest(::Rect rect,sk_sp<Bitmap> bitmap)41 BlockingCopyRequest(::Rect rect, sk_sp<Bitmap> bitmap) 42 : CopyRequest(rect), mDestination(bitmap) {} 43 getDestinationBitmap(int srcWidth,int srcHeight)44 virtual SkBitmap getDestinationBitmap(int srcWidth, int srcHeight) override { 45 SkBitmap bitmap; 46 mDestination->getSkBitmap(&bitmap); 47 return bitmap; 48 } 49 onCopyFinished(CopyResult result)50 virtual void onCopyFinished(CopyResult result) override { 51 std::unique_lock _lock{mLock}; 52 mResult = result; 53 mCondVar.notify_all(); 54 } 55 waitForResult()56 CopyResult waitForResult() { 57 std::unique_lock _lock{mLock}; 58 mCondVar.wait(_lock); 59 return mResult; 60 } 61 }; 62 63 class MagnifierAnimation : public TestScene { 64 public: 65 sp<RenderNode> card; 66 sp<RenderNode> zoomImageView; 67 sk_sp<Bitmap> magnifier; 68 std::shared_ptr<BlockingCopyRequest> copyRequest; 69 createContent(int width,int height,Canvas & canvas)70 void createContent(int width, int height, Canvas& canvas) override { 71 magnifier = TestUtils::createBitmap(200, 100); 72 setupCopyRequest(); 73 SkBitmap temp; 74 magnifier->getSkBitmap(&temp); 75 temp.eraseColor(Color::White); 76 canvas.drawColor(Color::White, SkBlendMode::kSrcOver); 77 card = TestUtils::createNode( 78 0, 0, width, height, [&](RenderProperties& props, Canvas& canvas) { 79 Paint paint; 80 paint.setAntiAlias(true); 81 paint.getSkFont().setSize(50); 82 83 paint.setColor(Color::Black); 84 TestUtils::drawUtf8ToCanvas(&canvas, "Test string", paint, 10, 400); 85 }); 86 canvas.drawRenderNode(card.get()); 87 zoomImageView = TestUtils::createNode( 88 100, 100, 500, 300, [&](RenderProperties& props, Canvas& canvas) { 89 props.setElevation(dp(16)); 90 props.mutableOutline().setRoundRect(0, 0, props.getWidth(), props.getHeight(), 91 dp(6), 1); 92 props.mutableOutline().setShouldClip(true); 93 canvas.drawBitmap(*magnifier, 0.0f, 0.0f, (float)magnifier->width(), 94 (float)magnifier->height(), 0, 0, (float)props.getWidth(), 95 (float)props.getHeight(), nullptr); 96 }); 97 canvas.enableZ(true); 98 canvas.drawRenderNode(zoomImageView.get()); 99 canvas.enableZ(false); 100 } 101 setupCopyRequest()102 void setupCopyRequest() { 103 constexpr int x = 90; 104 constexpr int y = 325; 105 copyRequest = std::make_shared<BlockingCopyRequest>( 106 ::Rect(x, y, x + magnifier->width(), y + magnifier->height()), magnifier); 107 } 108 doFrame(int frameNr)109 void doFrame(int frameNr) override { 110 int curFrame = frameNr % 150; 111 card->mutateStagingProperties().setTranslationX(curFrame); 112 card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y); 113 if (renderTarget) { 114 RenderProxy::copySurfaceInto(renderTarget.get(), copyRequest); 115 copyRequest->waitForResult(); 116 } 117 } 118 }; 119