| /* |
| * Copyright 2020 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include <cutils/properties.h> |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| #include <hardware/gralloc.h> |
| #include <renderengine/impl/ExternalTexture.h> |
| #include <renderengine/mock/RenderEngine.h> |
| #include <ui/PixelFormat.h> |
| #include "../threaded/RenderEngineThreaded.h" |
| |
| namespace android { |
| |
| using testing::_; |
| using testing::Eq; |
| using testing::Mock; |
| using testing::Return; |
| |
| struct RenderEngineThreadedTest : public ::testing::Test { |
| ~RenderEngineThreadedTest() {} |
| |
| void SetUp() override { |
| mThreadedRE = renderengine::threaded::RenderEngineThreaded::create( |
| [this]() { return std::unique_ptr<renderengine::RenderEngine>(mRenderEngine); }); |
| } |
| |
| std::unique_ptr<renderengine::threaded::RenderEngineThreaded> mThreadedRE; |
| renderengine::mock::RenderEngine* mRenderEngine = new renderengine::mock::RenderEngine(); |
| }; |
| |
| TEST_F(RenderEngineThreadedTest, dump) { |
| std::string testString = "XYZ"; |
| EXPECT_CALL(*mRenderEngine, dump(_)); |
| mThreadedRE->dump(testString); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, primeCache) { |
| EXPECT_CALL(*mRenderEngine, primeCache(false)); |
| mThreadedRE->primeCache(false); |
| // need to call ANY synchronous function after primeCache to ensure that primeCache has |
| // completed asynchronously before the test completes execution. |
| mThreadedRE->getContextPriority(); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, getMaxTextureSize_returns20) { |
| size_t size = 20; |
| EXPECT_CALL(*mRenderEngine, getMaxTextureSize()).WillOnce(Return(size)); |
| size_t result = mThreadedRE->getMaxTextureSize(); |
| ASSERT_EQ(size, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, getMaxTextureSize_returns0) { |
| size_t size = 0; |
| EXPECT_CALL(*mRenderEngine, getMaxTextureSize()).WillOnce(Return(size)); |
| size_t result = mThreadedRE->getMaxTextureSize(); |
| ASSERT_EQ(size, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, getMaxViewportDims_returns20) { |
| size_t dims = 20; |
| EXPECT_CALL(*mRenderEngine, getMaxViewportDims()).WillOnce(Return(dims)); |
| size_t result = mThreadedRE->getMaxViewportDims(); |
| ASSERT_EQ(dims, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, getMaxViewportDims_returns0) { |
| size_t dims = 0; |
| EXPECT_CALL(*mRenderEngine, getMaxViewportDims()).WillOnce(Return(dims)); |
| size_t result = mThreadedRE->getMaxViewportDims(); |
| ASSERT_EQ(dims, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, supportsProtectedContent_returnsFalse) { |
| EXPECT_CALL(*mRenderEngine, supportsProtectedContent()).WillOnce(Return(false)); |
| status_t result = mThreadedRE->supportsProtectedContent(); |
| ASSERT_EQ(false, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, supportsProtectedContent_returnsTrue) { |
| EXPECT_CALL(*mRenderEngine, supportsProtectedContent()).WillOnce(Return(true)); |
| status_t result = mThreadedRE->supportsProtectedContent(); |
| ASSERT_EQ(true, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, PostRenderCleanup_skipped) { |
| EXPECT_CALL(*mRenderEngine, cleanupPostRender()).Times(0); |
| mThreadedRE->cleanupPostRender(); |
| |
| // call ANY synchronous function to ensure that cleanupPostRender has completed. |
| mThreadedRE->getContextPriority(); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, PostRenderCleanup_notSkipped) { |
| renderengine::DisplaySettings settings; |
| std::vector<renderengine::LayerSettings> layers; |
| std::shared_ptr<renderengine::ExternalTexture> buffer = std::make_shared< |
| renderengine::impl:: |
| ExternalTexture>(sp<GraphicBuffer>::make(), *mRenderEngine, |
| renderengine::impl::ExternalTexture::Usage::READABLE | |
| renderengine::impl::ExternalTexture::Usage::WRITEABLE); |
| base::unique_fd bufferFence; |
| |
| EXPECT_CALL(*mRenderEngine, useProtectedContext(false)); |
| EXPECT_CALL(*mRenderEngine, drawLayersInternal) |
| .WillOnce([&](const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
| const renderengine::DisplaySettings&, |
| const std::vector<renderengine::LayerSettings>&, |
| const std::shared_ptr<renderengine::ExternalTexture>&, |
| base::unique_fd&&) { resultPromise->set_value(Fence::NO_FENCE); }); |
| EXPECT_CALL(*mRenderEngine, cleanupPostRender()).WillOnce(Return()); |
| ftl::Future<FenceResult> future = |
| mThreadedRE->drawLayers(settings, layers, buffer, std::move(bufferFence)); |
| mThreadedRE->cleanupPostRender(); |
| |
| // call ANY synchronous function to ensure that cleanupPostRender has completed. |
| mThreadedRE->getContextPriority(); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, supportsBackgroundBlur_returnsFalse) { |
| EXPECT_CALL(*mRenderEngine, supportsBackgroundBlur()).WillOnce(Return(false)); |
| status_t result = mThreadedRE->supportsBackgroundBlur(); |
| ASSERT_EQ(false, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, supportsBackgroundBlur_returnsTrue) { |
| EXPECT_CALL(*mRenderEngine, supportsBackgroundBlur()).WillOnce(Return(true)); |
| status_t result = mThreadedRE->supportsBackgroundBlur(); |
| ASSERT_EQ(true, result); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, drawLayers) { |
| renderengine::DisplaySettings settings; |
| std::vector<renderengine::LayerSettings> layers; |
| std::shared_ptr<renderengine::ExternalTexture> buffer = std::make_shared< |
| renderengine::impl:: |
| ExternalTexture>(sp<GraphicBuffer>::make(), *mRenderEngine, |
| renderengine::impl::ExternalTexture::Usage::READABLE | |
| renderengine::impl::ExternalTexture::Usage::WRITEABLE); |
| |
| base::unique_fd bufferFence; |
| |
| EXPECT_CALL(*mRenderEngine, useProtectedContext(false)); |
| EXPECT_CALL(*mRenderEngine, drawLayersInternal) |
| .WillOnce([&](const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
| const renderengine::DisplaySettings&, |
| const std::vector<renderengine::LayerSettings>&, |
| const std::shared_ptr<renderengine::ExternalTexture>&, |
| base::unique_fd&&) { resultPromise->set_value(Fence::NO_FENCE); }); |
| |
| ftl::Future<FenceResult> future = |
| mThreadedRE->drawLayers(settings, layers, buffer, std::move(bufferFence)); |
| ASSERT_TRUE(future.valid()); |
| auto result = future.get(); |
| ASSERT_TRUE(result.ok()); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, drawLayers_protectedLayer) { |
| renderengine::DisplaySettings settings; |
| auto layerBuffer = sp<GraphicBuffer>::make(); |
| layerBuffer->usage |= GRALLOC_USAGE_PROTECTED; |
| renderengine::LayerSettings layer; |
| layer.source.buffer.buffer = std::make_shared< |
| renderengine::impl::ExternalTexture>(std::move(layerBuffer), *mRenderEngine, |
| renderengine::impl::ExternalTexture::Usage:: |
| READABLE); |
| std::vector<renderengine::LayerSettings> layers = {std::move(layer)}; |
| std::shared_ptr<renderengine::ExternalTexture> buffer = std::make_shared< |
| renderengine::impl:: |
| ExternalTexture>(sp<GraphicBuffer>::make(), *mRenderEngine, |
| renderengine::impl::ExternalTexture::Usage::READABLE | |
| renderengine::impl::ExternalTexture::Usage::WRITEABLE); |
| |
| base::unique_fd bufferFence; |
| |
| EXPECT_CALL(*mRenderEngine, useProtectedContext(true)); |
| EXPECT_CALL(*mRenderEngine, drawLayersInternal) |
| .WillOnce([&](const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
| const renderengine::DisplaySettings&, |
| const std::vector<renderengine::LayerSettings>&, |
| const std::shared_ptr<renderengine::ExternalTexture>&, |
| base::unique_fd&&) { resultPromise->set_value(Fence::NO_FENCE); }); |
| |
| ftl::Future<FenceResult> future = |
| mThreadedRE->drawLayers(settings, layers, buffer, std::move(bufferFence)); |
| ASSERT_TRUE(future.valid()); |
| auto result = future.get(); |
| ASSERT_TRUE(result.ok()); |
| } |
| |
| TEST_F(RenderEngineThreadedTest, drawLayers_protectedOutputBuffer) { |
| renderengine::DisplaySettings settings; |
| std::vector<renderengine::LayerSettings> layers; |
| auto graphicBuffer = sp<GraphicBuffer>::make(); |
| graphicBuffer->usage |= GRALLOC_USAGE_PROTECTED; |
| std::shared_ptr<renderengine::ExternalTexture> buffer = std::make_shared< |
| renderengine::impl:: |
| ExternalTexture>(std::move(graphicBuffer), *mRenderEngine, |
| renderengine::impl::ExternalTexture::Usage::READABLE | |
| renderengine::impl::ExternalTexture::Usage::WRITEABLE); |
| |
| base::unique_fd bufferFence; |
| |
| EXPECT_CALL(*mRenderEngine, useProtectedContext(true)); |
| EXPECT_CALL(*mRenderEngine, drawLayersInternal) |
| .WillOnce([&](const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
| const renderengine::DisplaySettings&, |
| const std::vector<renderengine::LayerSettings>&, |
| const std::shared_ptr<renderengine::ExternalTexture>&, |
| base::unique_fd&&) { resultPromise->set_value(Fence::NO_FENCE); }); |
| |
| ftl::Future<FenceResult> future = |
| mThreadedRE->drawLayers(settings, layers, buffer, std::move(bufferFence)); |
| ASSERT_TRUE(future.valid()); |
| auto result = future.get(); |
| ASSERT_TRUE(result.ok()); |
| } |
| |
| } // namespace android |