2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
21 #include "generated/image-view-url-frag.h"
22 #include "shared/view.h"
28 const char* BIG_TEST_IMAGE(DEMO_IMAGE_DIR "book-landscape-cover.jpg");
30 const char* const APPLICATION_TITLE("Image View URL");
31 const char* const TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
32 const char* const BUTTON_ICON(DEMO_IMAGE_DIR "icon-change.png");
33 const char* const BUTTON_ICON_SELECTED(DEMO_IMAGE_DIR "icon-change-selected.png");
35 const char* DELTA_UNIFORM_NAME = "uDelta";
37 const Vector2 TARGET_SIZE(800.f, 800.f);
40 class ImageViewUrlApp : public ConnectionTracker
43 ImageViewUrlApp(Application& application, std::string url)
44 : mApplication(application),
46 mDeltaPropertyIndex(Property::INVALID_INDEX)
48 // Connect to the Application's Init signal
49 mApplication.InitSignal().Connect(this, &ImageViewUrlApp::Create);
52 ~ImageViewUrlApp() = default;
55 // The Init signal is received once (only) during the Application lifetime
56 void Create(Application& application)
58 // Get a handle to the window
59 Window window = application.GetWindow();
60 window.KeyEventSignal().Connect(this, &ImageViewUrlApp::OnKeyEvent);
62 Toolkit::ToolBar toolBar;
63 Toolkit::Control background;
64 // Creates a default view with a default tool bar.
65 mContent = DemoHelper::CreateView(application,
72 // Add a button to switch the scene. (right of toolbar)
73 Toolkit::PushButton switchButton = Toolkit::PushButton::New();
74 switchButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, BUTTON_ICON);
75 switchButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, BUTTON_ICON_SELECTED);
76 switchButton.ClickedSignal().Connect(this, &ImageViewUrlApp::OnButtonClicked);
77 toolBar.AddControl(switchButton,
78 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
79 Toolkit::Alignment::HORIZONTAL_RIGHT,
80 DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
82 std::string url = mUrl;
88 CreateRenderTask(url);
92 void CreateRenderTask(const std::string& url)
94 auto rootActor = mApplication.GetWindow().GetRootLayer();
96 auto cameraActor = CameraActor::New(TARGET_SIZE);
97 cameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
98 cameraActor.SetInvertYAxis(true);
99 rootActor.Add(cameraActor);
102 // create actor to render input with applied shader
103 mActorForInput = Toolkit::ImageView::New(url);
104 mActorForInput.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
105 mActorForInput.SetProperty(Actor::Property::SIZE, TARGET_SIZE);
106 Property::Map customShader;
107 customShader[Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VIEW_URL_FRAG.data();
108 Property::Map visualMap;
109 visualMap.Insert(Toolkit::Visual::Property::SHADER, customShader);
110 mActorForInput.SetProperty(Toolkit::ImageView::Property::IMAGE, visualMap);
112 mDeltaPropertyIndex = mActorForInput.RegisterProperty(DELTA_UNIFORM_NAME, 0.f);
114 rootActor.Add(mActorForInput);
116 RenderTaskList taskList = mApplication.GetWindow().GetRenderTaskList();
118 // perform a horizontal blur targeting the internal buffer
119 auto renderTask = taskList.CreateTask();
120 renderTask.SetRefreshRate(RenderTask::REFRESH_ALWAYS);
121 renderTask.SetSourceActor(mActorForInput);
122 renderTask.SetExclusive(true);
123 renderTask.SetInputEnabled(false);
124 renderTask.SetClearColor(Vector4(1., 0., 0., 1.));
125 renderTask.SetClearEnabled(true);
126 renderTask.SetCameraActor(cameraActor);
128 mOutputTexture = Texture::New(TextureType::TEXTURE_2D,
130 unsigned(TARGET_SIZE.width),
131 unsigned(TARGET_SIZE.height));
132 auto framebuffer = FrameBuffer::New(TARGET_SIZE.width, TARGET_SIZE.height, FrameBuffer::Attachment::NONE);
133 framebuffer.AttachColorTexture(mOutputTexture);
135 renderTask.SetFrameBuffer(framebuffer);
138 // animate the shader uniform
139 mAnimation = Animation::New(10.f);
141 mActorForInput.SetProperty(mDeltaPropertyIndex, 0.f);
142 mAnimation.AnimateTo(Property(mActorForInput, mDeltaPropertyIndex), Math::PI * 2.f);
143 mAnimation.SetLooping(true);
150 auto url = Dali::Toolkit::TextureManager::AddTexture(mOutputTexture);
151 mImageView = Toolkit::ImageView::New(url);
153 mImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
154 mImageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
155 mContent.Add(mImageView);
158 bool OnButtonClicked(Toolkit::Button button)
160 if(mAnimation.GetState() == Animation::State::PLAYING)
171 void OnKeyEvent(const KeyEvent& event)
173 if(event.GetState() == KeyEvent::DOWN)
175 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
183 Application& mApplication;
186 Toolkit::ImageView mImageView;
187 Animation mAnimation;
188 Actor mActorForInput;
189 Property::Index mDeltaPropertyIndex;
190 Texture mOutputTexture;
193 int DALI_EXPORT_API main(int argc, char** argv)
195 Application application = Application::New(&argc, &argv);
203 ImageViewUrlApp test(application, url);
204 application.MainLoop();