native-image-source-example.cpp
14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* 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.
*
*/
// EXTERNAL INCLUDES
#include <dali-toolkit/dali-toolkit.h>
#include <dali/dali.h>
#include <cstring>
// INTERNAL INCLUDES
#include "shared/utility.h"
#include "generated/native-image-source-texture-vert.h"
#include "generated/native-image-source-texture-frag.h"
using namespace Dali;
using namespace Toolkit;
namespace
{
const float BUTTON_HEIGHT = 100.0f;
const float BUTTON_COUNT = 5.0f;
const std::string JPG_FILENAME = DEMO_IMAGE_DIR "gallery-medium-4.jpg";
const std::string CAPTURE_FILENAME = DEMO_DATA_PUBLIC_RW_DIR "native-image-capture.png";
/**
* @brief Creates a shader used to render a native image
* @param[in] nativeImage The native image
* @return A shader to render the native image
*/
Shader CreateShader(NativeImageInterface& nativeImage)
{
static const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
std::string fragmentShader;
//Get custom fragment shader prefix
const char* fragmentPrefix = nativeImage.GetCustomFragmentPrefix();
if(fragmentPrefix)
{
fragmentShader = fragmentPrefix;
fragmentShader += SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_FRAG.data();
}
else
{
fragmentShader = SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_FRAG.data();
}
//Get custom sampler type name
const char* customSamplerTypename = nativeImage.GetCustomSamplerTypename();
if(customSamplerTypename)
{
fragmentShader.replace(fragmentShader.find(DEFAULT_SAMPLER_TYPENAME), strlen(DEFAULT_SAMPLER_TYPENAME), customSamplerTypename);
}
return Shader::New(SHADER_NATIVE_IMAGE_SOURCE_TEXTURE_VERT, fragmentShader);
}
} // namespace
// This example shows how to create and use a NativeImageSource as the target of the render task.
//
class NativeImageSourceController : public ConnectionTracker
{
public:
NativeImageSourceController(Application& application)
: mApplication(application),
mRefreshAlways(true)
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect(this, &NativeImageSourceController::Create);
}
~NativeImageSourceController()
{
// Nothing to do here;
}
// The Init signal is received once (only) during the Application lifetime
void Create(Application& application)
{
// Get a handle to the window
Window window = application.GetWindow();
window.SetBackgroundColor(Color::WHITE);
window.KeyEventSignal().Connect(this, &NativeImageSourceController::OnKeyEvent);
CreateButtonArea();
CreateContentAreas();
}
void CreateButtonArea()
{
Window window = mApplication.GetWindow();
Vector2 windowSize = window.GetSize();
mButtonArea = Layer::New();
mButtonArea.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x, BUTTON_HEIGHT));
mButtonArea.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
mButtonArea.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
window.Add(mButtonArea);
mButtonShow = PushButton::New();
mButtonShow.SetProperty(Button::Property::TOGGLABLE, true);
mButtonShow.SetProperty(Toolkit::Button::Property::LABEL, "SHOW");
mButtonShow.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
mButtonShow.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mButtonShow.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x / BUTTON_COUNT, BUTTON_HEIGHT));
mButtonShow.ClickedSignal().Connect(this, &NativeImageSourceController::OnButtonSelected);
mButtonArea.Add(mButtonShow);
mButtonRefreshAlways = PushButton::New();
mButtonRefreshAlways.SetProperty(Button::Property::TOGGLABLE, true);
mButtonRefreshAlways.SetProperty(Toolkit::Button::Property::LABEL, "ALWAYS");
mButtonRefreshAlways.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
mButtonRefreshAlways.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mButtonRefreshAlways.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x / BUTTON_COUNT, BUTTON_HEIGHT));
mButtonRefreshAlways.SetProperty(Actor::Property::POSITION, Vector2((windowSize.x / BUTTON_COUNT) * 1.0f, 0.0f));
mButtonRefreshAlways.StateChangedSignal().Connect(this, &NativeImageSourceController::OnButtonSelected);
mButtonArea.Add(mButtonRefreshAlways);
mButtonRefreshOnce = PushButton::New();
mButtonRefreshOnce.SetProperty(Toolkit::Button::Property::LABEL, "ONCE");
mButtonRefreshOnce.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
mButtonRefreshOnce.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mButtonRefreshOnce.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x / BUTTON_COUNT, BUTTON_HEIGHT));
mButtonRefreshOnce.SetProperty(Actor::Property::POSITION, Vector2((windowSize.x / BUTTON_COUNT) * 2.0f, 0.0f));
mButtonRefreshOnce.ClickedSignal().Connect(this, &NativeImageSourceController::OnButtonSelected);
mButtonArea.Add(mButtonRefreshOnce);
mButtonCapture = PushButton::New();
mButtonCapture.SetProperty(Toolkit::Button::Property::LABEL, "CAPTURE");
mButtonCapture.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
mButtonCapture.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mButtonCapture.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x / BUTTON_COUNT, BUTTON_HEIGHT));
mButtonCapture.SetProperty(Actor::Property::POSITION, Vector2((windowSize.x / BUTTON_COUNT) * 3.0f, 0.0f));
mButtonCapture.ClickedSignal().Connect(this, &NativeImageSourceController::OnButtonSelected);
mButtonArea.Add(mButtonCapture);
mButtonReset = PushButton::New();
mButtonReset.SetProperty(Toolkit::Button::Property::LABEL, "RESET");
mButtonReset.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
mButtonReset.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mButtonReset.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x / BUTTON_COUNT, BUTTON_HEIGHT));
mButtonReset.SetProperty(Actor::Property::POSITION, Vector2((windowSize.x / BUTTON_COUNT) * 4.0f, 0.0f));
mButtonReset.ClickedSignal().Connect(this, &NativeImageSourceController::OnButtonSelected);
mButtonArea.Add(mButtonReset);
}
void CreateContentAreas()
{
Window window = mApplication.GetWindow();
Vector2 windowSize = window.GetSize();
float contentHeight((windowSize.y - BUTTON_HEIGHT) / 2.0f);
mTopContentArea = Actor::New();
mTopContentArea.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x, contentHeight));
mTopContentArea.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
mTopContentArea.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
mTopContentArea.SetProperty(Actor::Property::POSITION_Y, BUTTON_HEIGHT);
window.Add(mTopContentArea);
mBottomContentArea = Actor::New();
mBottomContentArea.SetProperty(Actor::Property::SIZE, Vector2(windowSize.x, contentHeight));
mBottomContentArea.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
mBottomContentArea.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
window.Add(mBottomContentArea);
mSourceActor = ImageView::New(JPG_FILENAME);
mSourceActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mSourceActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
mTopContentArea.Add(mSourceActor);
Animation animation = Animation::New(2.f);
Degree relativeRotationDegrees(90.0f);
Radian relativeRotationRadians(relativeRotationDegrees);
animation.AnimateTo(Property(mSourceActor, Actor::Property::ORIENTATION), Quaternion(relativeRotationRadians, Vector3::ZAXIS), AlphaFunction::LINEAR, TimePeriod(0.f, 0.5f));
animation.AnimateBy(Property(mSourceActor, Actor::Property::ORIENTATION), Quaternion(relativeRotationRadians, Vector3::ZAXIS), AlphaFunction::LINEAR, TimePeriod(0.5f, 0.5f));
animation.AnimateBy(Property(mSourceActor, Actor::Property::ORIENTATION), Quaternion(relativeRotationRadians, Vector3::ZAXIS), AlphaFunction::LINEAR, TimePeriod(1.f, 0.5f));
animation.AnimateBy(Property(mSourceActor, Actor::Property::ORIENTATION), Quaternion(relativeRotationRadians, Vector3::ZAXIS), AlphaFunction::LINEAR, TimePeriod(1.5f, 0.5f));
animation.SetLooping(true);
animation.Play();
TextLabel textLabel1 = TextLabel::New("Image");
textLabel1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
textLabel1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
mTopContentArea.Add(textLabel1);
// Wait until button press before creating mOffscreenRenderTask
TextLabel textLabel2 = TextLabel::New("Native Image");
textLabel2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
textLabel2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
mBottomContentArea.Add(textLabel2);
}
void SetupNativeImage()
{
if(!mOffscreenRenderTask)
{
Window window = mApplication.GetWindow();
Vector2 windowSize = window.GetSize();
float contentHeight((windowSize.y - BUTTON_HEIGHT) / 2.0f);
Vector2 imageSize(windowSize.x, contentHeight);
mNativeImageSourcePtr = NativeImageSource::New(imageSize.width, imageSize.height, NativeImageSource::COLOR_DEPTH_DEFAULT);
mNativeTexture = Texture::New(*mNativeImageSourcePtr);
mFrameBuffer = FrameBuffer::New(mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), FrameBuffer::Attachment::NONE);
mFrameBuffer.AttachColorTexture(mNativeTexture);
mCameraActor = CameraActor::New(imageSize);
mCameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mCameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, AnchorPoint::CENTER);
mTopContentArea.Add(mCameraActor);
RenderTaskList taskList = window.GetRenderTaskList();
mOffscreenRenderTask = taskList.CreateTask();
mOffscreenRenderTask.SetSourceActor(mSourceActor);
mOffscreenRenderTask.SetClearColor(Color::WHITE);
mOffscreenRenderTask.SetClearEnabled(true);
mOffscreenRenderTask.SetCameraActor(mCameraActor);
mOffscreenRenderTask.GetCameraActor().SetInvertYAxis(true);
mOffscreenRenderTask.SetFrameBuffer(mFrameBuffer);
}
if(mRefreshAlways)
{
mOffscreenRenderTask.SetRefreshRate(RenderTask::REFRESH_ALWAYS);
}
else
{
mOffscreenRenderTask.SetRefreshRate(RenderTask::REFRESH_ONCE);
}
}
void SetupDisplayActor(bool show)
{
if(show)
{
if(!mDisplayActor)
{
// Make sure we have something to display
SetupNativeImage();
mDisplayActor = Actor::New();
mDisplayActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mDisplayActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
Geometry geometry = DemoHelper::CreateTexturedQuad();
Shader shader = CreateShader(*mNativeImageSourcePtr);
Renderer renderer = Renderer::New(geometry, shader);
TextureSet textureSet = TextureSet::New();
textureSet.SetTexture(0u, mNativeTexture);
renderer.SetTextures(textureSet);
mDisplayActor.AddRenderer(renderer);
mDisplayActor.SetProperty(Actor::Property::SIZE, Vector2(mNativeTexture.GetWidth(), mNativeTexture.GetHeight()));
mBottomContentArea.Add(mDisplayActor);
}
}
else
{
UnparentAndReset(mDisplayActor);
}
}
void Capture()
{
mRefreshAlways = false;
SetupNativeImage();
mOffscreenRenderTask.FinishedSignal().Connect(this, &NativeImageSourceController::DoCapture);
}
void DoCapture(RenderTask& task)
{
task.FinishedSignal().Disconnect(this, &NativeImageSourceController::DoCapture);
mNativeImageSourcePtr->EncodeToFile(CAPTURE_FILENAME);
}
void Reset()
{
SetupDisplayActor(false);
Window window = mApplication.GetWindow();
RenderTaskList taskList = window.GetRenderTaskList();
taskList.RemoveTask(mOffscreenRenderTask);
mOffscreenRenderTask.Reset();
mCameraActor.Reset();
mFrameBuffer.Reset();
mNativeTexture.Reset();
mNativeImageSourcePtr.Reset();
}
bool OnButtonSelected(Toolkit::Button button)
{
Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(button);
if(pushButton == mButtonShow)
{
bool isSelected = mButtonShow.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
SetupDisplayActor(isSelected);
}
else if(pushButton == mButtonRefreshAlways)
{
bool isSelected = mButtonRefreshAlways.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
mRefreshAlways = isSelected;
SetupNativeImage();
}
else if(pushButton == mButtonRefreshOnce)
{
bool isSelected = mButtonRefreshAlways.GetProperty(Toolkit::Button::Property::SELECTED).Get<bool>();
if(isSelected)
{
mButtonRefreshAlways.SetProperty(Button::Property::SELECTED, false);
}
mRefreshAlways = false;
SetupNativeImage();
}
else if(pushButton == mButtonCapture)
{
Capture();
}
else if(pushButton == mButtonReset)
{
Reset();
}
return true;
}
void OnKeyEvent(const KeyEvent& event)
{
if(event.GetState() == KeyEvent::DOWN)
{
if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
{
mApplication.Quit();
}
}
}
private:
Application& mApplication;
Layer mButtonArea;
Actor mTopContentArea;
Actor mBottomContentArea;
PushButton mButtonShow;
PushButton mButtonRefreshAlways;
PushButton mButtonRefreshOnce;
PushButton mButtonCapture;
PushButton mButtonReset;
Actor mSourceActor;
NativeImageSourcePtr mNativeImageSourcePtr;
Texture mNativeTexture;
FrameBuffer mFrameBuffer;
RenderTask mOffscreenRenderTask;
CameraActor mCameraActor;
Actor mDisplayActor;
bool mRefreshAlways;
};
int DALI_EXPORT_API main(int argc, char** argv)
{
Application application = Application::New(&argc, &argv);
NativeImageSourceController test(application);
application.MainLoop();
return 0;
}