Add interface with native Java type argument - awt

I want to use swig to create a JNI interface not in the common way that the C++ function exists. I want to add a method/function with an java class argument.
File : test.i
%typemap(javaimports) A %{
import java.awt.Component;
%}
%extend(java) A {
void Attach( Component awt)
{
// here I Want to retrieve XDisplay, Window, Screen from
// /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.25.x86_64/include/jawt.h
// I need canvas of type jobject.
// with this information I call Attach( XDisplay * dpy, Window win);
}
}
class A
{
public:
void Attach( XDisplay * dpy, Window win);
};

You'll need a few typemaps to have a java.awt.Component passed through to an extended method as a jobject:
%module test
%typemap(jstype) jobject *awtcomponent "java.awt.Component";
%typemap(jtype) jobject *awtcomponent "java.awt.Component";
%typemap(jni) jobject *awtcomponent "jobject";
%typemap(javain) jobject *awtcomponent "$javainput";
%extend A {
void attach(jobject awtcomponent) {
// Call whatever JNI functions you want with this jobject
// (which is always an AWT component)
// Then:
$self->Attach(arg1, arg2);
}
}
%inline %{
struct A {
};
%}
Given that you already have a member function you can in fact skip the %extend entirely and use a typemap to deal with that directly:
%module test
%typemap(jstype) (XDisplay * dpy, Window win) "java.awt.Component";
%typemap(jtype) (XDisplay * dpy, Window win) "java.awt.Component";
%typemap(jni) (XDisplay * dpy, Window win) "jobject";
%typemap(javain) (XDisplay * dpy, Window win) "$javainput";
%typemap(in,numinputs=1) (XDisplay * dpy, Window win) {
// some JNI to convert $input (jobject) into $1 (dpy) and $2 (win)
}
%inline %{
struct A {
void Attach(XDisplay * dpy, Window win) {}
};
%}
These typemaps have the added benefit of working everywhere you have a XDisplay, Window pairing with no extra effort.
The completed example, derived from the jawt.h example, with all the JNI filled out becomes:
%module test
%{
#include <X11/Xlib.h>
#include <jawt.h>
#include <jawt_md.h>
#include <cassert>
%}
%typemap(jstype) (Display * dpy, Window win) "java.awt.Component";
%typemap(jtype) (Display * dpy, Window win) "java.awt.Component";
%typemap(jni) (Display * dpy, Window win) "jobject";
%typemap(javain) (Display * dpy, Window win) "$javainput";
%typemap(in,noblock=1,numinputs=1) (Display * dpy, Window win) {
// some JNI to convert $input (jobject) into $1 (dpy) and $2 (win)
JAWT awt;
JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo *dsi;
JAWT_X11DrawingSurfaceInfo* dsi_x11;
jboolean result;
jint lock;
awt.version = JAWT_VERSION_1_3;
result = JAWT_GetAWT(jenv, &awt);
assert(result != JNI_FALSE);
ds = awt.GetDrawingSurface(jenv, $input);
assert(ds != NULL);
lock = ds->Lock(ds);
assert((lock & JAWT_LOCK_ERROR) == 0);
dsi = ds->GetDrawingSurfaceInfo(ds);
dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
$1 = dsi_x11->display;
$2 = dsi_x11->drawable;
}
%typemap(freearg) (Display * dpy, Window win) {
ds->FreeDrawingSurfaceInfo(dsi);
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
}
%inline %{
struct A {
void Attach(Display * dpy, Window win) {}
};
%}
The two notable changes are the added freearg typemap which runs after the call to cleanup and the noblock=1 to make the local temporary variables visible to the freearg.

Related

Notifying a control that another control has a state change

I am using C++Builder Enterprise and need some ideas.
I have a Form with a bunch of TButton and TSpeedButton controls on it. What I want to have happen is that, when a given button is pressed, I want to disable it and several others, while enabling several other buttons, i.e. state changes.
The issue is that I am duplicating the enable/disable code in a bunch of places. I've thought about somehow using the TNotifyEvent delegation to alert the buttons of a state change, but I don't think that delegation technique will work in this situation. I want to avoid creating a bunch of sub-classes of TButton/TSpeedButton.
I also would like to try to use techniques that are available from the VCL, as in each component carries an observer list and wonder if I could leverage that somehow.
My 2 cents worth..
I have done a proof of concept for your problem, would need a lot more work but I think it is doable and should do what you want.
I will do more work on it and post if there is interest.
To find all controls of a class type on a form
--- TControlAgent->FindTControl(this, "TButton");
I added 4 buttons on a form, added code bellow to button1 click event,
TControlAgent->SetControlProperty("Enabled", "TButton", false, true, "Button3");
in this case, want controls with Enabled property whose class name is TButton, state = true or false, execute the action but but exclude control named TButton3
There result was all TButtons on the form were set to disable except for button 3
(note: the exclude option would be a lookup list of controls to be excluded from any action or be able to set a state that is not the state eg Enabled = !true or !false )
In the click event of button3 I placed the following code;
TControlAgent->SetControlProperty("Enabled", "TButton", true, true, "");
The result of this is to re-enable all the controls, this is just an idea but with with the extra work it could be possible to execute any action on any control from a single parent form for any form in a collection of forms.
I also think it could be possible to fire events, just a crazy idea...
.h file using berlin 10.1
//---------------------------------------------------------------------------
#ifndef TControlAgentH
#define TControlAgentH
//---------------------------------------------------------------------------
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <System.TypInfo.hpp>
#include <vector>
#include <algorithm>
//---------------------------------------------------------------------------
class PACKAGE TControlAgent : public TComponent
{
private:
std::vector<TControl*> FControls;
protected:
public:
std::vector<UnicodeString> excludeControlNames;
__fastcall TControlAgent(TComponent* Owner);
TControl * __fastcall GetControl(TControl* ctrl, UnicodeString property);
void __fastcall FindTControl(TForm *f, UnicodeString className);
TControl* __fastcall SetControlProperty(UnicodeString property, UnicodeString className, bool state, bool exec, UnicodeString excludeControlName );
__published:
};
//---------------------------------------------------------------------------
#endif
.cpp file
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TControlAgent.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//---------------------------------------------------------------------------
// component to act as agent for other components derived from TControl
//
// Components have properties and events, is it possible to get all TControls into a vector
// then be able to get a property of a component then fire an event or carry out an action
// on the component!!!!
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TControlAgent *)
{
new TControlAgent(NULL);
}
//---------------------------------------------------------------------------
__fastcall TControlAgent::TControlAgent(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TForm *f, UnicodeString className)
{
FControls.clear();
for(int i = 0; i < f->ControlCount; i++)
{
if(f->Controls[i]->ClassName() == className) //control classes that to set as a group
FControls.push_back(f->Controls[i]);
}
//note: could have a function that appends other class names
}
//---------------------------------------------------------------------------
TControl* __fastcall TControlAgent::SetControlProperty(UnicodeString property, UnicodeString className, bool state, bool exec, UnicodeString excludeControlName )
{
PPropInfo propinfo;
for(int i = 0; i < FControls.size(); i++)
{
if(FControls[i]->ClassName() == className)
{
propinfo = GetPropInfo(FControls[i], property);
if (!propinfo)
continue;
if(exec && FControls[i]->Name != excludeControlName )
{
if(property == "Enabled")
FControls[i]->Enabled = state;
}
}
}
}
//---------------------------------------------------------------------------
namespace Tcontrolagent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TControlAgent)};
RegisterComponents(L"SQLite", classes, 0);
}
}
//---------------------------------------------------------------------------
**** UPDATED *****
Progress of TControlAgent
Obviously still work in progress but this code works, create a form with a TPanel, to this add 6 TButtons
Add a TControl Agent, set the OnButtonClick event as per Form code bellow
TControlAgent code
.h
//---------------------------------------------------------------------------
#ifndef TControlAgentH
#define TControlAgentH
//---------------------------------------------------------------------------
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <System.TypInfo.hpp>
#include <vector>
#include <algorithm>
class TControls;
class TControlGroup;
typedef void __fastcall (__closure *TControlAgentOnClick)(System::TObject *Sender, TControls *ctrl);
typedef void __fastcall (__closure *TButtonClickEvent)(System::TObject *Sender);
enum action {aEnabled, aOnClick, aChange};
enum tcontrols {tbutton, tedit, tcombo, tcheckbox};
//---------------------------------------------------------------------------
class PACKAGE TControlAgent : public TComponent
{
private:
std::vector<TControls*> FControls;
std::vector<TControlGroup*> FControlGroups;
TControlAgentOnClick FClickSupliment;
TButtonClickEvent FOnButtonClick;
bool FButtonClickRedirect;
protected:
public:
std::vector<UnicodeString> excludeControlNames; // not implemented yet
std::vector<TControlGroup*> __fastcall Groups();
std::vector<TControls*> __fastcall Controls(int GroupIndex);
__fastcall TControlAgent(TComponent* Owner);
TControl * __fastcall GetControl(TControl* ctrl, UnicodeString property);
void __fastcall FindTControl(TForm *f, UnicodeString className);
void __fastcall FindTControl(TPanel *p, UnicodeString GroupName, bool ClearIfControlsExists);
void __fastcall SetControlProperty(TControl *ctrl, bool state, int Action);
int __fastcall GetGroup(String name);
void __fastcall SetControlClickEvent(String ClassName, String GroupName, String ExcludeGroup, int tControlType);
void __fastcall SetButtonPropValue(TButton* b, String Property, String Value, bool v);
int __fastcall AddTControl(TControl* c, String Group);
void __fastcall SetButtonPropValue(String Group, String ExcludeGroup,int TControlType,String Property,String GroupValue,bool GroupV,String excludeGroupValue,bool excludeGroupV);
__published:
__property bool TButtonClick = {read = FButtonClickRedirect, write = FButtonClickRedirect };
__property TControlAgentOnClick OnClickSuppliment={read=FClickSupliment, write=FClickSupliment};
__property TButtonClickEvent OnButtonClick = {read = FOnButtonClick, write = FOnButtonClick };
};
//---------------------------------------------------------------------------
class TControlGroup
{
public:
UnicodeString GroupName;
std::vector<TControls*> GroupControls;
__fastcall TControlGroup();
};
//---------------------------------------------------------------------------
class TControls
{
public:
TControl* ctrl;
bool WantButtonOnClick;
bool WantControlOnChange;
bool FireStateChange;
bool WantOnClickSupliment;
bool state;
__fastcall TControls();
};
#endif
.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TControlAgent.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
//---------------------------------------------------------------------------
// component to act as agent for other components derived from TControl
//
// Components have properties and events, is it possible to get all TControls into a vector
// then be able to get a property of a component then fire an event or carry out an action
// on the component!!!!
//---------------------------------------------------------------------------
struct IsCtrlGroup {
String _name;
IsCtrlGroup(String name) : _name(name)
{
}
bool operator()(const TControlGroup * item) const
{
return (item->GroupName == _name);
}
};
//---------------------------------------------------------------------------
struct IsClassName {
String _name;
IsClassName(String name) : _name(name)
{
}
bool operator()(const TControl * item) const
{
return (item->ClassName() == _name);
}
};
//---------------------------------------------------------------------------
struct IsCtrl {
TControl* _ctrl;
IsCtrl(TControl* ctrl) : _ctrl(ctrl) {
}
bool operator()(const TControls * item) const {
return (item->ctrl->Name == _ctrl->Name);
}
};
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TControlAgent *)
{
new TControlAgent(NULL);
}
//---------------------------------------------------------------------------
__fastcall TControlAgent::TControlAgent(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TForm *f, UnicodeString className)
{
FControls.clear();
TControls* ctrl;
for(int i = 0; i < f->ControlCount; i++)
{
if(f->Controls[i]->ClassName() == className) //control classes that to set as a group
{
TControls* ctrl = new TControls();
ctrl->ctrl = f->Controls[i];
ctrl->FireStateChange = false;
ctrl->WantOnClickSupliment = false;
FControls.push_back(ctrl);
}
}
//note: could have a function that appends other class names
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::FindTControl(TPanel *p, UnicodeString GroupName, bool ClearIfControlsExists)
{
/*
group vector[]
--> controlsindex vector[index]
*/
TControlGroup* g;
TControls* ctrl;
int i = -1;
int group_index = -1, controls_index = -1;
// check if group name exists in group vector
group_index = GetGroup(GroupName);
//clear controls vector if exists, controls will not exist if group does not exist...
if(ClearIfControlsExists && group_index > 0)
FControlGroups[group_index]->GroupControls.clear();
// if group does not exist, push new group onto vector
if(group_index == -1)
{
g = new TControlGroup();
g->GroupName = GroupName;
FControlGroups.push_back(g);
group_index = GetGroup(GroupName);
}
//group must bnow exist
for(i = 0; i < p->ControlCount; i++)
{
TControls* ctrl = new TControls();
ctrl->ctrl = p->Controls[i];
FControlGroups[group_index]->GroupControls.push_back(ctrl);
controls_index = FControlGroups[group_index]->GroupControls.size() -1;
FControlGroups[group_index]->GroupControls[controls_index]->ctrl = p->Controls[i];
}
}
//---------------------------------------------------------------------------
int __fastcall TControlAgent::AddTControl(TControl* c, String Group)
{
int index;
TControlGroup* g;
int group_index = GetGroup(Group);
if(group_index == -1)
{
g = new TControlGroup();
g->GroupName = Group;
FControlGroups.push_back(g);
group_index = GetGroup(Group);
}
TControls* ctrl = new TControls();
ctrl->ctrl = c;
FControlGroups[group_index]->GroupControls.push_back(ctrl);
index = FControlGroups[group_index]->GroupControls.size()-1;
return(index);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetControlClickEvent(String ClassName, String GroupName, String ExcludeGroup, int tControlType)
{
int group_index = GetGroup(GroupName);
for(int i = 0; i < FControlGroups[group_index]->GroupControls.size(); i++)
{
if(FControlGroups[group_index]->GroupControls[i]->ctrl->ClassName() == ClassName)
{
switch(tControlType)
{
case tbutton:
dynamic_cast<TButton*>(FControlGroups[group_index]->GroupControls[i]->ctrl)->OnClick = FOnButtonClick;
break;
case tedit:
break;
case tcombo:
break;
case tcheckbox:
break;
}
}
}
}
//---------------------------------------------------------------------------
std::vector<TControlGroup*> __fastcall TControlAgent::Groups()
{
return(FControlGroups);
}
//---------------------------------------------------------------------------
std::vector<TControls*> __fastcall TControlAgent::Controls(int GroupIndex)
{
return(FControlGroups[GroupIndex]->GroupControls);
}
//---------------------------------------------------------------------------
int __fastcall TControlAgent::GetGroup(String name)
{
int group_index =-1;
std::vector<TControlGroup*>::iterator found = std::find_if(FControlGroups.begin(), FControlGroups.end(), IsCtrlGroup(name));
if(found != FControlGroups.end())
group_index = std::distance(FControlGroups.begin(), found);
return(group_index);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetButtonPropValue(TButton* b, String Property, String Value, bool v)
{
PPropInfo propinfo;
propinfo = GetPropInfo(b, Property);
if (!propinfo)
return;
if(Value.IsEmpty())
SetPropValue(b, propinfo, v);
else
SetPropValue(b, propinfo, Value);
}
//---------------------------------------------------------------------------
void __fastcall TControlAgent::SetButtonPropValue(String Group,
String ExcludeGroup,
int TControlType,
String Property,
String GroupValue,
bool GroupV,
String excludeGroupValue,
bool excludeGroupV)
{
// Group can hold all TControls on a form
// ExcludeGroup contains TControls that will be excluded from an action on the Group controls
// Group is an existing group of TControls found on a container
// ExcludGroup is a group that can be found on a container or added to a Group
// then parsed to this method in ExcludeGroup param
int i;
PPropInfo propinfo;
TControl *c;
int group_index = GetGroup(Group);
int exclude_Group_index = GetGroup(ExcludeGroup);
TControl* ctrl;
for(i = 0; i < FControlGroups[group_index]->GroupControls.size(); i++)
{
c = FControlGroups[group_index]->GroupControls[i]->ctrl;
//check if TControl is to be excluded
std::vector<TControls*>::iterator found = std::find_if(FControlGroups[exclude_Group_index]->GroupControls.begin(),
FControlGroups[exclude_Group_index]->GroupControls.end(),
IsCtrl(c));
// if found, control is in the exclude list so continue, do not apply
// property value change
if(found != FControlGroups[exclude_Group_index]->GroupControls.end())
{
c = (*found)->ctrl;
//set property value for exclude group controls
propinfo = GetPropInfo(c, Property);
if(propinfo)
{
if(excludeGroupValue.IsEmpty())
SetPropValue(c, propinfo, excludeGroupV);
else
SetPropValue(c, propinfo, excludeGroupValue);
}
continue;
}
//if it gets here, c is not in exclude list
propinfo = GetPropInfo(c, Property);
if (!propinfo)
return;
if(GroupValue.IsEmpty())
SetPropValue(c, propinfo, GroupV);
else
SetPropValue(c, propinfo, GroupValue);
}
}
//---------------------------------------------------------------------------
namespace Tcontrolagent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TControlAgent)};
RegisterComponents(L"SQLite", classes, 0);
}
}
//---------------------------------------------------------------------------
__fastcall TControls::TControls(){}
//---------------------------------------------------------------------------
__fastcall TControlGroup::TControlGroup(){}
Form Code
.cpp
Form has a TPanel with 6 TButtons placed on it
#run time TButton3 is disabled
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "frmTControlTest.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TControlAgent"
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
int i;
std::vector<TControls*> group_controls;
//get ALL controls on a container
ControlAgent1->FindTControl(p, "Group1", true);
//create the exclude (for want of a better name) groups
/*
add
edit
save
delete
cancel
exit
*/
ControlAgent1->AddTControl(bnSave, "Group2");
ControlAgent1->AddTControl(bnCancel, "Group2");
ControlAgent1->AddTControl(bnAdd, "Group3");
ControlAgent1->AddTControl(bnEdit, "Group3");
ControlAgent1->AddTControl(bnDelete, "Group3");
ControlAgent1->AddTControl(bnExit, "Group3");
i = ControlAgent1->GetGroup("Group1");
group_controls = ControlAgent1->Controls(i);
ControlAgent1->SetControlClickEvent("TButton", "Group1", "", tbutton);
}
//---------------------------------------------------------------------------
void __fastcall TForm3::ControlAgent1ButtonClick(TObject *Sender)
{
TButton* b;
if((b = dynamic_cast<TButton*>(Sender)) == bnAdd )
ControlAgent1->SetButtonPropValue("Group1", "Group2", tbutton, "Enabled", "", false, "", true);
if((b = dynamic_cast<TButton*>(Sender)) == bnCancel )
ControlAgent1->SetButtonPropValue("Group1", "Group3", tbutton, "Enabled", "", false, "", true);
if((b = dynamic_cast<TButton*>(Sender)) == bnSave )
{
ControlAgent1->SetButtonPropValue("Group1", "Group3", tbutton, "Enabled", "", false, "", true);
//Do Stuff
}
}
//---------------------------------------------------------------------------

How to measure the execution time of GPU with using Profiling+openCL+Sycl+DPCPP

I read this link
https://github.com/intel/pti-gpu
and I tried to use Device Activity Tracing for OpenCL(TM), but I am confused and I do not know how should I measure the time on the accelerators with using Device Activity documentation.
for measuring the performance of CPU I used chrono, but I am interested in to using profiling for measuring the performance of CPU and GPU in different devices.
my program:
#include <CL/sycl.hpp>
#include <iostream>
#include <tbb/tbb.h>
#include <tbb/parallel_for.h>
#include <vector>
#include <string>
#include <queue>
#include<tbb/blocked_range.h>
#include <tbb/global_control.h>
#include <chrono>
using namespace tbb;
template<class Tin, class Tout, class Function>
class Map {
private:
Function fun;
public:
Map() {}
Map(Function f):fun(f) {}
std::vector<Tout> operator()(bool use_tbb, std::vector<Tin>& v) {
std::vector<Tout> r(v.size());
if(use_tbb){
// Start measuring time
auto begin = std::chrono::high_resolution_clock::now();
tbb::parallel_for(tbb::blocked_range<Tin>(0, v.size()),
[&](tbb::blocked_range<Tin> t) {
for (int index = t.begin(); index < t.end(); ++index){
r[index] = fun(v[index]);
}
});
// Stop measuring time and calculate the elapsed time
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
return r;
} else {
sycl::queue gpuQueue{sycl::gpu_selector()};
sycl::range<1> n_item{v.size()};
sycl::buffer<Tin, 1> in_buffer(&v[0], n_item);
sycl::buffer<Tout, 1> out_buffer(&r[0], n_item);
gpuQueue.submit([&](sycl::handler& h){
//local copy of fun
auto f = fun;
sycl::accessor in_accessor(in_buffer, h, sycl::read_only);
sycl::accessor out_accessor(out_buffer, h, sycl::write_only);
h.parallel_for(n_item, [=](sycl::id<1> index) {
out_accessor[index] = f(in_accessor[index]);
});
}).wait();
}
return r;
}
};
template<class Tin, class Tout, class Function>
Map<Tin, Tout, Function> make_map(Function f) { return Map<Tin, Tout, Function>(f);}
typedef int(*func)(int x);
//define different functions
auto function = [](int x){ return x; };
auto functionTimesTwo = [](int x){ return (x*2); };
auto functionDivideByTwo = [](int x){ return (x/2); };
auto lambdaFunction = [](int x){return (++x);};
int main(int argc, char *argv[]) {
std::vector<int> v = {1,2,3,4,5,6,7,8,9};
//auto f = [](int x){return (++x);};
//Array of functions
func functions[] =
{
function,
functionTimesTwo,
functionDivideByTwo,
lambdaFunction
};
for(int i = 0; i< sizeof(functions); i++){
auto m1 = make_map<int, int>(functions[i]);
//auto m1 = make_map<int, int>(f);
std::vector<int> r = m1(true, v);
//print the result
for(auto &e:r) {
std::cout << e << " ";
}
}
return 0;
}
First of all, SYCL Kernel wont support function pointers. So, you can change the code accordingly.
One way to achieve profiling in GPU is by following the steps below:
1.Enable profiling mode for the command queue of the target device
2.Introduce the event for the target device activity
3.Set the callback to be notified when the activity is completed
4.Read the profiling data inside the callback
Basically, you need to use CL_PROFILING_COMMAND_START and CL_PROFILING_COMMAND_END (command identified by event start and end execution on the device) inside the call back.
You can find the detailed steps here
https://github.com/intel/pti-gpu/blob/master/chapters/device_activity_tracing/OpenCL.md
I would also advice you to check the samples for pti-gpu using Device Activity Tracing. Check the URL for the same
https://github.com/intel/pti-gpu/tree/master/samples

How to io_control of boost library socket with customized command

I' trying to make an identical function to "ioctl" in c++ style using boost library.
Here is my "c" style code:
int sockfd;
char * id;
struct iwreq wreq;
memset(&wreq, 0, sizeof(struct iwreq));
sprintf(wreq.ifr_name, IW_INTERFACE);
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
fprintf(stderr, "Cannot open socket \n");
fprintf(stderr, "errno = %d \n", errno);
fprintf(stderr, "Error description is : %s\n",strerror(errno));
exit(1);
}
printf("Socket opened successfully \n");
id = malloc(IW_ESSID_MAX_SIZE+1);
wreq.u.essid.pointer = id;
if (ioctl(sockfd, SIOCGIWESSID, &wreq)) {
fprintf(stderr, "Get ESSID ioctl failed \n");
fprintf(stderr, "errno = %d \n", errno);
fprintf(stderr, "Error description : %s\n",strerror(errno));
exit(2);
}
printf("IOCTL Successfull\n");
printf("ESSID is %s\n", wreq.u.essid.pointer);
I found some relevant example, but I'm not clear how to use it correctly. example
Main function:
boost::asio::ip::udp::socket socket(io_service);
struct iwreq wreq
memset(&wreq, 0, sizeof(struct iwreq));
sprintf(wreq.ifr_name, IW_INTERFACE);
id = malloc(IW_ESSID_MAX_SIZE+1);
wreq.u.essid.pointer = id;
boost::asio::detail::io_control::myCommand command;
command.set(&wreq);
boost::system::error_code ec;
socket.io_control(command, ec);
if (ec)
{
// An error occurred.
}
Custom command:
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <boost/config.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace detail {
namespace io_control {
// I/O control command for getting number of bytes available.
class myCommand
{
public:
// Default constructor.
myCommand()
: value_(0)
{
}
// Get the name of the IO control command.
int name() const
{
return static_cast<int>(SIOCGIWESSID);
}
// Set the value of the I/O control command.
void set(struct iwreq* value)
{
value_ = static_cast<detail::ioctl_arg_type>(value);
}
// Get the current value of the I/O control command.
std::size_t get() const
{
return static_cast<struct iwreq*>(value_);
}
// Get the address of the command data.
detail::ioctl_arg_type* data()
{
return &value_;
}
// Get the address of the command data.
const detail::ioctl_arg_type* data() const
{
return &value_;
}
private:
detail::ioctl_arg_type value_;
};
} // namespace io_control
} // namespace detail
} // namespace asio
} // namespace boost
However, the code does not work.
If you have any example code or solution, please let me know.
Thank you.

Qt5 to stream two videos simultaneously

I created a GUI to play two videos from two cameras, one camera is a USB camera, and another one is integrated in my laptop. My code is simple, I have three files:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraImageCapture>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QList <QCameraInfo> camList;
QCamera *camera1;
QCamera *camera2;
private slots:
void onCameraChanged(int);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
camera1 = NULL;
camera2 = NULL;
connect(ui->cameraComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,
&MainWindow::onCameraChanged);
camList = QCameraInfo::availableCameras();
for(QList <QCameraInfo>::iterator it = camList.begin();it!=camList.end();++it) {
ui->cameraComboBox->addItem(it->description());
}
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onCameraChanged(int idx) {
if(camera1 != NULL) {
camera1->stop();
}
qDebug() << idx;
camera1 = new QCamera(camList.at(idx),this);
camera1->setViewfinder(ui->mainView);
camera1->setCaptureMode(QCamera::CaptureStillImage);
camera1->start();
if(camera2 != NULL) {
camera2->stop();
}
camera2 = new QCamera(camList.at(idx+1),this);
camera2->setViewfinder(ui->submainView);
camera2->setCaptureMode(QCamera::CaptureStillImage);
camera2->start();
}
I have deleted irrelevant code. Of course I have this in the *.pro file:
QT += multimedia multimediawidgets
My code compiles correctly, but it can only stream one video (camera1), and the other one shows nothing. The error I got is
CameraBin error: "Error starting streaming on device '/dev/video1'."
CameraBin error: "Could not negotiate format"
Any help will be appreciated.

Is there an equivalent to __attribute__((ns_returns_retained)) for a malloc'd pointer?

I'm looking for an annotation something like
-(SomeStruct *) structFromInternals __attribute__((returns_malloced_ptr))
{
SomeStruct *ret = malloc(sizeof(SomeStruct));
//do stuff
return ret;
}
to soothe the clang static analyzer beasts.
The only viable attributes link I can find is for GCC, but it doesn't even include ns_returns_retained, which is in an extension, I assume.
EDIT:
as to why this is needed, I have a scenario that I can't repro in a simple case, so it may have to do with a c lib in an Objective-C project... The gist is, I get a static analyzer warning that the malloc in createStruct is leaked:
typedef struct{
void * data;
size_t len;
}MyStruct;
void destroyStruct(MyStruct * s)
{
if (s && s->data) {
free(s->data);
}
if (s) {
free(s);
}
}
MyStruct * createStructNoCopy(size_t len, void * data)
{
MyStruct * retStruct = malloc(sizeof(MyStruct));
retStruct->len = len;
retStruct->data = data;
return retStruct;
}
MyStruct * createStruct(size_t len, void * data)
{
char * tmpData = malloc(len);
memcpy(tmpData, data, len);
return createStructNoCopy(len, tmpData);
}
MyStruct * copyStruct(MyStruct * s)
{
return createStruct(s->len, s->data);
}
The function annotation ownership_returns(malloc) will tell the Clang static analyser that the function returns a pointer that should be passed to free() at some point (or a function with ownership_takes(malloc, ...)). For example:
void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
...
void af1() {
int *p = my_malloc(1);
return; // expected-warning{{Potential leak of memory pointed to by}}
}
void af2() {
int *p = my_malloc(1);
my_free(p);
return; // no-warning
}
(See the malloc-annotations.c test file for some more examples of their use.)
At the moment, these annotations only take effect when the alpha.unix.MallocWithAnnotations checker is run (which is not run by default). If you're using Xcode, you'll need to add -Xclang -analyzer-checker=alpha.unix.MallocWithAnnotations to your build flags.