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.
19 #include "camera-view-impl.h"
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/window-devel.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/adaptor-framework/native-image-source.h>
26 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali/public-api/object/type-registry.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/public-api/controls/camera-view/camera-view.h>
46 return Toolkit::CameraView::New(NULL);
49 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::CameraView, Toolkit::Control, Create)
50 DALI_TYPE_REGISTRATION_END()
53 CameraView::CameraView(Dali::Toolkit::CameraView::DisplayType displayType)
54 : Control(ControlBehaviour(ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS)),
55 mDisplayType(displayType)
59 CameraView::~CameraView()
63 Toolkit::CameraView CameraView::New(Any cameraHandle, Dali::Toolkit::CameraView::DisplayType type)
65 CameraView* impl = new CameraView(type);
66 Toolkit::CameraView handle = Toolkit::CameraView(*impl);
67 impl->mCameraPlayer = Dali::CameraPlayer::New();
69 if(impl->mCameraPlayer)
71 impl->mCameraPlayer.SetCameraPlayer(cameraHandle);
76 void CameraView::Update()
78 UpdateDisplayArea(mSizeUpdateNotification);
81 void CameraView::OnSceneConnection(int depth)
83 Control::OnSceneConnection(depth);
84 if(mDisplayType == Dali::Toolkit::CameraView::DisplayType::WINDOW)
86 SetWindowSurfaceTarget();
88 else if(mDisplayType == Dali::Toolkit::CameraView::DisplayType::IMAGE)
90 SetNativeImageTarget();
95 void CameraView::OnSceneDisconnection()
97 Control::OnSceneDisconnection();
101 self.RemoveRenderer(mTextureRenderer);
102 mTextureRenderer.Reset();
107 self.RemoveRenderer(mOverlayRenderer);
108 mOverlayRenderer.Reset();
112 void CameraView::SetWindowSurfaceTarget()
116 mPositionUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_POSITION, StepCondition(1.0f, 1.0f));
117 mSizeUpdateNotification = self.AddPropertyNotification(Actor::Property::SIZE, StepCondition(1.0f, 1.0f));
118 mScaleUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
119 mPositionUpdateNotification.NotifySignal().Connect(this, &CameraView::UpdateDisplayArea);
120 mSizeUpdateNotification.NotifySignal().Connect(this, &CameraView::UpdateDisplayArea);
121 mScaleUpdateNotification.NotifySignal().Connect(this, &CameraView::UpdateDisplayArea);
123 // For underlay rendering mode, camera display area have to be transparent.
124 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
125 Shader shader = Shader::New(SHADER_VIDEO_VIEW_VERT, SHADER_VIDEO_VIEW_FRAG);
126 mOverlayRenderer = Renderer::New(geometry, shader);
127 mOverlayRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::OFF);
129 Self().AddRenderer(mOverlayRenderer);
131 // Note CameraPlayer::SetWindowRenderingTarget
132 mCameraPlayer.SetWindowRenderingTarget(DevelWindow::Get(self));
135 void CameraView::SetNativeImageTarget()
139 self.RemovePropertyNotification(mPositionUpdateNotification);
140 self.RemovePropertyNotification(mSizeUpdateNotification);
141 self.RemovePropertyNotification(mScaleUpdateNotification);
144 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New(source);
145 mNativeTexture = Dali::Texture::New(*nativeImageSourcePtr);
147 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
148 Dali::Shader shader = CreateShader(nativeImageSourcePtr->GetCustomFragmentPrefix());
149 Dali::TextureSet textureSet = Dali::TextureSet::New();
150 textureSet.SetTexture(0u, mNativeTexture);
152 mTextureRenderer = Renderer::New(geometry, shader);
153 mTextureRenderer.SetTextures(textureSet);
155 Self().AddRenderer(mTextureRenderer);
157 // Note CameraPlayer::SetNativeImageRenderingTarget.
158 mCameraPlayer.SetNativeImageRenderingTarget(nativeImageSourcePtr);
161 void CameraView::UpdateDisplayArea(Dali::PropertyNotification& source)
163 if(mDisplayType != Dali::Toolkit::CameraView::DisplayType::WINDOW)
170 bool positionUsesAnchorPoint = self.GetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
171 Vector3 actorSize = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
172 Vector3 anchorPointOffSet = actorSize * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
174 Vector2 screenPosition = self.GetProperty(Actor::Property::SCREEN_POSITION).Get<Vector2>();
176 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
177 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
178 mDisplayArea.width = actorSize.x;
179 mDisplayArea.height = actorSize.y;
181 mCameraPlayer.SetDisplayArea(mDisplayArea);
184 Dali::Shader CameraView::CreateShader(const char* fragmentPrefix)
186 std::string fragmentShader = fragmentPrefix;
187 std::string vertexShader;
189 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
190 fragmentShader += SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
192 return Dali::Shader::New(vertexShader, fragmentShader);
195 } // namespace Internal
197 } // namespace Toolkit