DirectRendering:
[platform/core/uifw/dali-demo.git] / examples / drawable-actor / drawable-actor-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/public-api/actors/drawable-actor.h>
20
21 #include "native-renderer.h"
22
23 // Include the GLES header
24 #include <GLES3/gl3.h>
25 #include <dali/graphics-api/graphics-buffer-create-info.h>
26
27 #include <dali/devel-api/common/stage-devel.h>
28
29 using TextLabel = Dali::Toolkit::TextLabel;
30 using namespace Dali;
31
32 // This example shows DrawableActor using native GL code to clear screen to red
33 // and renders TextLabel on top of it.
34 //
35 class DrawableActorExampleController : public ConnectionTracker
36 {
37 public:
38
39   explicit DrawableActorExampleController(Application& application)
40     : mApplication(application)
41   {
42     // Connect to the Application's Init signal
43     mApplication.InitSignal().Connect(this, &DrawableActorExampleController::Create);
44   }
45
46   ~DrawableActorExampleController() override = default; // Nothing to do in destructor
47
48   // The Init signal is received once (only) during the Application lifetime
49   void Create(Application& application)
50   {
51     // Get a handle to the window
52     Window window = application.GetWindow();
53     window.SetBackgroundColor(Color::WHITE);
54
55     // Create native renderer
56     mRenderer = std::make_unique<NativeRenderer>(window.GetSize().GetWidth(), window.GetSize().GetHeight());
57
58     // Create render callback
59     mRenderCallback = RenderCallback::New<NativeRenderer>( mRenderer.get(), &NativeRenderer::OnRender );
60
61     // Create drawable actor
62     mGLActor = DrawableActor::New( *mRenderCallback );
63
64     mGLActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
65     mGLActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
66
67     // Set size on the actor (half the window size to show that glClear() and scissor test work together)
68     mGLActor.SetProperty( Actor::Property::SIZE, Size( window.GetSize() ) * 0.5f);
69
70     // Add actor to the scene
71     window.Add(mGLActor);
72
73     // Create TextLabel
74     mTextLabel = TextLabel::New("This text overlays DrawableActor");
75     mTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
76     mTextLabel.SetProperty(Dali::Actor::Property::NAME, "SomeTextLabel");
77     window.Add(mTextLabel);
78
79     // Respond to a touch anywhere on the window
80     window.GetRootLayer().TouchedSignal().Connect(this, &DrawableActorExampleController::OnTouch);
81
82     // Respond to key events
83     window.KeyEventSignal().Connect(this, &DrawableActorExampleController::OnKeyEvent);
84   }
85
86   bool OnTouch(Actor actor, const TouchEvent& touch)
87   {
88     // quit the application
89     mApplication.Quit();
90     return true;
91   }
92
93   void OnKeyEvent(const KeyEvent& event)
94   {
95     if(event.GetState() == KeyEvent::DOWN)
96     {
97       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
98       {
99         mApplication.Quit();
100       }
101     }
102   }
103
104   TextLabel mTextLabel;
105   DrawableActor mGLActor;
106
107   std::unique_ptr<RenderCallback> mRenderCallback;
108   std::unique_ptr<NativeRenderer> mRenderer{nullptr};
109
110 private:
111   Application& mApplication;
112 };
113
114 int DALI_EXPORT_API main(int argc, char** argv)
115 {
116   Application          application = Application::New(&argc, &argv);
117   DrawableActorExampleController test(application);
118   application.MainLoop();
119   return 0;
120 }