Understanding and implementing native Binders in android using C++ - android

I want to implement a simple IPC mechanism using Binders in android. For that, I searched on the Internet and found this. I compiled it and it runs fine on my Android device. I tried to take an overall understanding of the program, by searching for each class on AOSP, but everything got more difficult and messed up. Can anyone please explain (just high level), maybe by adding more comments, so that it also helps some future visitors. Here's the code is taken from there:
#define LOG_TAG "binder_demo"
#include <stdlib.h>
#include "utils/RefBase.h"
#include "utils/Log.h"
#include "utils/TextOutput.h"
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
using namespace android;
#define INFO(...) \
do { \
printf(__VA_ARGS__); \
printf("\n"); \
LOGD(__VA_ARGS__); \
} while(0)
void assert_fail(const char *file, int line, const char *func, const char *expr) {
INFO("assertion failed at file %s, line %d, function %s:",
file, line, func);
INFO("%s", expr);
abort();
}
#define ASSERT(e) \
do { \
if (!(e)) \
assert_fail(__FILE__, __LINE__, __func__, #e); \
} while(0)
// Where to print the parcel contents: aout, alog, aerr. alog doesn't seem to work.
#define PLOG aout
// Interface (our AIDL) - Shared by server and client
class IDemo : public IInterface {
public:
enum {
ALERT = IBinder::FIRST_CALL_TRANSACTION,
PUSH,
ADD
};
// Sends a user-provided value to the service
virtual void push(int32_t data) = 0;
// Sends a fixed alert string to the service
virtual void alert() = 0;
// Requests the service to perform an addition and return the result
virtual int32_t add(int32_t v1, int32_t v2) = 0;
DECLARE_META_INTERFACE(Demo); // Expands to 5 lines below:
//static const android::String16 descriptor;
//static android::sp<IDemo> asInterface(const android::sp<android::IBinder>& obj);
//virtual const android::String16& getInterfaceDescriptor() const;
//IDemo();
//virtual ~IDemo();
};
// Client
class BpDemo : public BpInterface<IDemo> {
public:
BpDemo(const sp<IBinder>& impl) : BpInterface<IDemo>(impl) {
LOGD("BpDemo::BpDemo()");
}
virtual void push(int32_t push_data) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(push_data);
aout << "BpDemo::push parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(PUSH, data, &reply);
aout << "BpDemo::push parcel reply:\n";
reply.print(PLOG); endl(PLOG);
LOGD("BpDemo::push(%i)", push_data);
}
virtual void alert() {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeString16(String16("The alert string"));
remote()->transact(ALERT, data, &reply, IBinder::FLAG_ONEWAY); // asynchronous call
LOGD("BpDemo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(v1);
data.writeInt32(v2);
aout << "BpDemo::add parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(ADD, data, &reply);
LOGD("BpDemo::add transact reply");
reply.print(PLOG); endl(PLOG);
int32_t res;
status_t status = reply.readInt32(&res);
LOGD("BpDemo::add(%i, %i) = %i (status: %i)", v1, v2, res, status);
return res;
}
};
//IMPLEMENT_META_INTERFACE(Demo, "Demo");
// Macro above expands to code below. Doing it by hand so we can log ctor and destructor calls.
const android::String16 IDemo::descriptor("Demo");
const android::String16& IDemo::getInterfaceDescriptor() const {
return IDemo::descriptor;
}
android::sp<IDemo> IDemo::asInterface(const android::sp<android::IBinder>& obj) {
android::sp<IDemo> intr;
if (obj != NULL) {
intr = static_cast<IDemo*>(obj->queryLocalInterface(IDemo::descriptor).get());
if (intr == NULL) {
intr = new BpDemo(obj);
}
}
return intr;
}
IDemo::IDemo() { LOGD("IDemo::IDemo()"); }
IDemo::~IDemo() { LOGD("IDemo::~IDemo()"); }
// End of macro expansion
// Server
class BnDemo : public BnInterface<IDemo> {
virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0);
};
status_t BnDemo::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
LOGD("BnDemo::onTransact(%i) %i", code, flags);
data.checkInterface(this);
data.print(PLOG); endl(PLOG);
switch(code) {
case ALERT: {
alert(); // Ignoring the fixed alert string
return NO_ERROR;
} break;
case PUSH: {
int32_t inData = data.readInt32();
LOGD("BnDemo::onTransact got %i", inData);
push(inData);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
return NO_ERROR;
} break;
case ADD: {
int32_t inV1 = data.readInt32();
int32_t inV2 = data.readInt32();
int32_t sum = add(inV1, inV2);
LOGD("BnDemo::onTransact add(%i, %i) = %i", inV1, inV2, sum);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
reply->writeInt32(sum);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
class Demo : public BnDemo {
virtual void push(int32_t data) {
INFO("Demo::push(%i)", data);
}
virtual void alert() {
INFO("Demo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
INFO("Demo::add(%i, %i)", v1, v2);
return v1 + v2;
}
};
// Helper function to get a hold of the "Demo" service.
sp<IDemo> getDemoServ() {
sp<IServiceManager> sm = defaultServiceManager();
ASSERT(sm != 0);
sp<IBinder> binder = sm->getService(String16("Demo"));
// TODO: If the "Demo" service is not running, getService times out and binder == 0.
ASSERT(binder != 0);
sp<IDemo> demo = interface_cast<IDemo>(binder);
ASSERT(demo != 0);
return demo;
}
int main(int argc, char **argv) {
if (argc == 1) {
LOGD("We're the service");
defaultServiceManager()->addService(String16("Demo"), new Demo());
android::ProcessState::self()->startThreadPool();
LOGD("Demo service is now ready");
IPCThreadState::self()->joinThreadPool();
LOGD("Demo service thread joined");
} else if (argc == 2) {
INFO("We're the client: %s", argv[1]);
int v = atoi(argv[1]);
sp<IDemo> demo = getDemoServ();
demo->alert();
demo->push(v);
const int32_t adder = 5;
int32_t sum = demo->add(v, adder);
LOGD("Addition result: %i + %i = %i", v, adder, sum);
}
return 0;
}

I know this is a bit late but checkout this amazing explanation by Gabriel Burca on Android IPC mechanism here. You can find a working example with a very simple C++ code from the same author here. Further it has clear instructions how to compile and add it to your AOSP source. Cheers!

Related

Implement native binder code of c++

I have the following code for native binder implementation.
#define LOG_TAG "binder_demo"
#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
using namespace android;
#define INFO(...) \
do { \
printf(__VA_ARGS__); \
printf("\n"); \
ALOGD(__VA_ARGS__); \
} while(0)
void assert_fail(const char *file, int line, const char *func, const char *expr) {
INFO("assertion failed at file %s, line %d, function %s:",
file, line, func);
INFO("%s", expr);
abort();
}
#define ASSERT(e) \
do { \
if (!(e)) \
assert_fail(__FILE__, __LINE__, __func__, #e); \
} while(0)
#define PLOG aout
class IDemo : public IInterface {
public:
enum {
ALERT = IBinder::FIRST_CALL_TRANSACTION,
PUSH,
ADD
};
// Sends a user-provided value to the service
virtual void push(int32_t data) = 0;
// Sends a fixed alert string to the service
virtual void alert() = 0;
// Requests the service to perform an addition and return the result
virtual int32_t add(int32_t v1, int32_t v2) = 0;
DECLARE_META_INTERFACE(Demo); // Expands to 5 lines below:
};
// Client
class BpDemo : public BpInterface<IDemo> {
public:
BpDemo(const sp<IBinder>& impl) : BpInterface<IDemo>(impl) {
ALOGD("BpDemo::BpDemo()");
}
virtual void push(int32_t push_data) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(push_data);
aout << "BpDemo::push parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(PUSH, data, &reply);
aout << "BpDemo::push parcel reply:\n";
reply.print(PLOG); endl(PLOG);
ALOGD("BpDemo::push(%i)", push_data);
}
virtual void alert() {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeString16(String16("The alert string"));
remote()->transact(ALERT, data, &reply, IBinder::FLAG_ONEWAY); // asynchronous call
ALOGD("BpDemo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(v1);
data.writeInt32(v2);
aout << "BpDemo::add parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(ADD, data, &reply);
ALOGD("BpDemo::add transact reply");
reply.print(PLOG); endl(PLOG);
int32_t res;
status_t status = reply.readInt32(&res);
ALOGD("BpDemo::add(%i, %i) = %i (status: %i)", v1, v2, res, status);
return res;
}
};
DECLARE_META_INTERFACE(Demo,"Demo"); // Expands to 5 lines below:
// Server
class BnDemo : public BnInterface<IDemo> {
virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0);
};
status_t BnDemo::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
ALOGD("BnDemo::onTransact(%i) %i", code, flags);
data.checkInterface(this);
data.print(PLOG); endl(PLOG);
switch(code) {
case ALERT: {
alert(); // Ignoring the fixed alert string
return NO_ERROR;
} break;
case PUSH: {
int32_t inData = data.readInt32();
ALOGD("BnDemo::onTransact got %i", inData);
push(inData);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
return NO_ERROR;
} break;
case ADD: {
int32_t inV1 = data.readInt32();
int32_t inV2 = data.readInt32();
int32_t sum = add(inV1, inV2);
ALOGD("BnDemo::onTransact add(%i, %i) = %i", inV1, inV2, sum);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
reply->writeInt32(sum);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
class Demo : public BnDemo {
virtual void push(int32_t data) {
INFO("Demo::push(%i)", data);
}
virtual void alert() {
INFO("Demo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
INFO("Demo::add(%i, %i)", v1, v2);
return v1 + v2;
}
};
// Helper function to get a hold of the "Demo" service.
sp<IDemo> getDemoServ() {
sp<IServiceManager> sm = defaultServiceManager();
ASSERT(sm != 0);
sp<IBinder> binder = sm->getService(String16("Demo"));
// TODO: If the "Demo" service is not running, getService times out and binder == 0.
ASSERT(binder != 0);
sp<IDemo> demo = interface_cast<IDemo>(binder);
ASSERT(demo != 0);
return demo;
}
int main(int argc, char **argv) {
if (argc == 1) {
ALOGD("We're the service");
defaultServiceManager()->addService(String16("Demo"), new Demo());
android::ProcessState::self()->startThreadPool();
ALOGD("Demo service is now ready");
IPCThreadState::self()->joinThreadPool();
ALOGD("Demo service thread joined");
} else if (argc == 2) {
INFO("We're the client: %s", argv[1]);
int v = atoi(argv[1]);
sp<IDemo> demo = getDemoServ()
demo->alert();
demo->push(v);
const int32_t adder = 5;
int32_t sum = demo->add(v, adder);
ALOGD("Addition result: %i + %i = %i", v, adder, sum);
}
return 0;
}
The problem is how to compile and test it on a android platform. I don't want to build a new binary. What I want is that the code should compile on a running android platform and result should be displayed on a terminal like adb shell.

how to make interact between nodes that are made in different cpps...?

There are over 20 Layers like Layer01.cpp, Layer02.cpp, Layer03.cpp ...
and there is a tableview named "itemSlots" in HelloWorld.cpp.
When user touch dragon button in Layer01,
a banana sprite in HelloWorld.cpp will be dissappeared,
and a poop appear in itemSlots.
That is all I want to do and I thought it is quite simple.
I made Layers and tableview also, like below,
but still couldn't find a way to make interact between button and sprite
which are made in different cpps.
Layer01.h
#ifndef __LAYER01__H__
#define __LAYER01__H__
#include "cocos2d.h"
#include "ui/CocosGUI.h"
class Layer01 : public cocos2d::LayerColor
{
public:
Layer01();
virtual bool init();
cocos2d::ui::Button* dragon;
void touchDragon();
private:
};
#endif
Layer01.cpp
#include "Layer01.h"
USING_NS_CC;
Layer01::Layer01()
{
bool bOk = initWithColor(Color4B::BLACK, 750, 400);
if (bOk == true) {
this->autorelease();
init();
};
}
bool Layer01::init()
{
scene01 = Sprite::create("images/scene01.jpg");
scene01->setScale(this->getContentSize().width/sc02a->getContentSize().width);
scene01->setAnchorPoint(Point::ZERO);
scene01->setPosition(Point::ZERO);
this->addChild(scene01);
dragon = ui::Button::create("images/dragon.png", "images/dragon.png", "");
dragon->setContentSize(Size(50, 50));
dragon->setPosition(Point(250,300));
dragon->addClickEventListener(CC_CALLBACK_0(Layer01::touchDragon, this));
this->addChild(dragon);
return true;
}
void Layer01::touchDragon()
{
/*
layer01->removeChild(banana);
auto poopCell = itemSlots->cellAtIndex(2);
poopCell->addChild(poop);
*/
}
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Layer01.h"
#include "ui/CocosGUI.h"
#include "cocos-ext.h"
#include "CustomTableViewCell.h"
class HelloWorld : public cocos2d::Layer,
public cocos2d::extension::TableViewDataSource,
public cocos2d::extension::TableViewDelegate
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
cocos2d::Sprite* banana;
cocos2d::Sprite* poop;
cocos2d::extension::TableView* itemSlots;
virtual void tableCellTouched(cocos2d::extension::TableView* table,
cocos2d::extension::TableViewCell* cell);
virtual cocos2d::Size tableCellSizeForIndex
(cocos2d::extension::TableView* table, ssize_t idx);
virtual cocos2d::extension::TableViewCell* tableCellAtIndex
(cocos2d::extension::TableView* table, ssize_t idx);
virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView* view);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
USING_NS_CC_EXT;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
layer01 = new Layer01();
layer->setPosition(Point(0, 100));
this->addChild(layer01);
banana = Sprite::create("images/banana.png");
banana->setPosition(Point(300,300));
layer01->addChild(banana);
poop = Sprite::create("images/poop.png");
itemSlots = TableView::create(this, Size(535, 70));
itemSlots->setDirection(ScrollView::Direction::HORIZONTAL);
itemSlots->setPosition(Point(115, 15));
itemSlots->setDelegate(this);
this->addChild(itemSlots);
itemSlots->reloadData();
return true;
}
void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell)
{
}
Size HelloWorld::tableCellSizeForIndex(TableView* table, ssize_t idx)
{
return Size(77, 77);
}
TableViewCell* HelloWorld::tableCellAtIndex(TableView* table, ssize_t idx)
{
auto string = String::createWithFormat("%ld", idx);
TableViewCell* cell = table->dequeueCell();
if (cell == false)
{
cell = new CustomTableViewCell();
cell->autorelease();
auto sprite01 = Sprite::create();
sprite01->setAnchorPoint(Point::ZERO);
sprite01->setPosition(Point::ZERO);
cell->addChild(sprite01);
auto label = LabelTTF::create(string->getCString(), "arial", 20.0);
label->setAnchorPoint(Point::ZERO);
label->setPosition(Point(5, 5));
label->setTag(120);
cell->addChild(label);
}
else {
auto label = (LabelTTF*)cell->getChildByTag(120);
label->setString(string->getCString());
}
return cell;
}
ssize_t HelloWorld::numberOfCellsInTableView(TableView* table)
{
return 20;
}
The easiest but unsafe way is to do following:
//Layer01.cpp
#include "HelloWorld.h"
...
void Layer01::touchDragon()
{
removeChild(banana);
// As your parent is a HelloWorld you can cast it:
HelloWorld* helloWorld = (HelloWorld*)getParent();
auto poopCell = helloWorld->itemSlots->cellAtIndex(2);
poopCell->addChild(helloWorld->poop);
}
But poop will be automatically garbage collected at that time. You need to retain it after creating and release when no longer need.
The better option would be to have a pointer to the HelloWorld layer in the Layer01 and have a separate method for setting a poop:
Layer01.h
// Forward declaration
class HelloWorld;
class Layer01
{
...
HelloWorld* m_hellowWorld;
// Don't forget about create function
static Layer01* create(HelloWorld* helloWorld)
{
Layer01* result = new (std::nothrow) Layer01();
if(result && result->init(helloWorld))
{
result->autorelease();
}
else
{
delete result;
result = nullptr;
}
return result;
}
bool Layer01::init(HelloWorld* helloWorld);
...
};
Layer01.cpp
#include "Layer01.h"
#include "HelloWorld.h"
Layer01::Layer01()
: HelloWorld(nullptr)
{}
bool Layer01::init(HelloWorld* helloWorld)
{
removeChild(banana);
m_hellowWorld = helloWorld;
...
}
void Layer01::touchDragon()
{
m_hellowWorld->setPoop();
}
HelloWorld.h
class HelloWorld
{
...
void setPoop();
...
};
HelloWorld.cpp
...
void HelloWorld::setPoop()
{
auto poopCell = itemSlots->cellAtIndex(2);
poopCell->addChild(Sprite::create("images/poop.png"));
}

Getting a ANativeWindowBuffer from ANativeWindow_Buffer

To get fast OpenGL ES 2.0 texture pixel access on Android NDK, I want to use the eglCreateImageKHR() extension.
According to the EGL_NATIVE_BUFFER_ANDROID docs:
This extension enables using an Android window buffer (struct
ANativeWindowBuffer) as an EGLImage source.
ANativeWindowBuffer is an internal struct used by the native framework classes like GraphicBuffer.
Unfortunately, since I am on NDK I do not have direct access to these classes.
The NDK native_window interface allows me to pass a Java Surface object through to the NDK. I can then use ANativeWindow_fromSurface() to get an opaque ANativeWindow* handle. With this pointer I can call ANativeWindow_lock() to fill a struct of type ANativeWindow_Buffer (Note the _).
If I try to use this &ANativeWindow_Buffer object with eglCreateImageKHR() it fails with EGL_BAD_NATIVE_WINDOW.
My question is: How can I use ANativeWindow_Buffer with eglCreateImageKHR() or alternatively how to get an ANativeWindowBuffer from ANativeWindow_Buffer or from ANativeWindow*.
From what I figured out while going down this road, ANativeWindow_Buffer and ANativeWindowBuffer are entirely different types. Well, they are somewhat similar, but definitely so different that they can't be used interchangeably.
If you want to compare, here are the definitions:
ANativeWindow_Buffer: http://androidxref.com/4.4.4_r1/xref/prebuilts/ndk/current/platforms/android-18/arch-arm/usr/include/android/native_window.h
ANativeWindowBuffer: http://androidxref.com/4.4.4_r1/xref/system/core/include/system/window.h
You will notice that they have a few fields in common (width, height, stride, format). The big difference is that ANativeWindow_Buffer contains a pointer to the actual data, while ANativeWindowBuffer contains an opaque handle of type buffer_handle_t.
So if you found out how to get a ANativeWindow_Buffer, and were hoping that you were well on your way to a ANativeWindowBuffer, you're... probably not. At least that was my conclusion. I think the very similar names are just a tease.
I did not find a way to create an ANativeWindowBuffer from NDK code. At least with using only supported APIs, I believe it's not possible. My research was with KitKat.
I found this question and I though it might be useful to answer it with newer information and developments, since I had to look up again on how to do it and this was one of the first answers on google for ImageKHR.
This is how you get a native buffer to use with ImageKHR. You have to "politely" ask for one from the gralloc, for that you just open the linux_kernel file that represents the IPC between the binder and gralloc, its way deeper inside the internals.
The technique demonstrated bellow will use dlopen to get the pointers to one of the ".so" that does that, but as it is internal to the system, and uses JNI reflection, there's a chance that the app verifier won't like it if you try to publish it.
You can circunvent it by going one level deeper and implementing what the gralloc itself does, it just writes and reads a file block device, the app verifier wouldn't stand a chance if its just a fopen call, it can't possibly check in runtime the difference between calls from the actual libui.so or you code, it just do a simple static analysis.
For doing this you can just copy the source code of the GrAlloc or link the libui.so with a different name as said in the github project.
Just for completeness, although I use this technique, I have a fallback using PBOs for transferring data from the GPU to the CPU in case of failures, but in most cases PBOs have acceptable performance.
the bare minimum needed
the complete library for doing this I used as reference
FramebufferNativeWindow.cpp
GraphicBuffer.h
#pragma once
#include <exception>
#include <cstdint>
#include <cerrno>
class DynamicLibrary
{
public:
DynamicLibrary(const char *fileName);
~DynamicLibrary();
void *getFunctionPtr(const char *name) const;
DynamicLibrary(const DynamicLibrary &) = delete;
DynamicLibrary & operator = (const DynamicLibrary &other) = delete;
private:
void *libHandle;
};
struct ANativeWindowBuffer;
namespace android
{
class GraphicBuffer;
// include/system/window.h
struct android_native_base_t
{
uint32_t magic;
uint32_t version;
void* reserved[4];
void (*incRef)(struct android_native_base_t* base);
void (*decRef)(struct android_native_base_t* base);
};
// include/ui/android_native_buffer.h
struct android_native_buffer_t
{
struct android_native_base_t common;
int32_t width;
int32_t height;
int32_t stride;
int32_t format;
int32_t usage;
// ...
};
}
// utils/Errors.h
enum status_t
{ /*ommited, look at the gist */ };
// ui/PixelFormat.h, system/graphics.h
enum PixelFormat
{ /*ommited, look at the gist */ };
// ui/GraphicBuffer.h
{ /*ommited, look at the gist */ };
class GraphicBuffer
{
public:
// ui/GraphicBuffer.h, hardware/gralloc.h
GraphicBuffer(uint32_t width, uint32_t height, PixelFormat format, uint32_t usage);
~GraphicBuffer();
status_t lock(uint32_t usage, void** vaddr);
status_t unlock();
ANativeWindowBuffer *getNativeBuffer() const;
uint32_t getStride() const;
private:
DynamicLibrary library;
GraphicBufferFunctions functions;
android::GraphicBuffer *impl = nullptr;
};
#include "GraphicBuffer.h"
And the implementation:
GraphicBuffer.cpp
#include <string>
#include <cstdlib>
#include <iostream>
#include <iostream>
#include <dlfcn.h>
const int GRAPHICBUFFER_SIZE = 1024;
using std::string;
DynamicLibrary::DynamicLibrary(const char *fileName)
{
libHandle = dlopen(fileName, RTLD_LAZY);
if (!libHandle) throw OpenLibFailedException();
}
DynamicLibrary::~DynamicLibrary()
{
if (libHandle) dlclose(libHandle);
}
void *DynamicLibrary::getFunctionPtr(const char *name) const
{
auto ret = (void *)dlsym(libHandle, name);
if (ret == nullptr) {
std::cerr << "Failed to get function " << name << std::endl;
}
return ret;
}
template<typename Func>
void setFuncPtr(Func *&funcPtr, const DynamicLibrary &lib, const string &symname) {
funcPtr = reinterpret_cast<Func *>(lib.getFunctionPtr(symname.c_str()));
}
#if defined(__aarch64__)
# define CPU_ARM_64
#elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
# define CPU_ARM
#elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
# define CPU_X86_64
#elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
# define CPU_X86
#else
# warning "target CPU does not support ABI"
#endif
template<typename RT, typename T1, typename T2, typename T3, typename T4>
RT *callConstructor4(void (*fptr)(), void *memory, T1 param1, T2 param2, T3 param3, T4 param4) {
#if defined(CPU_ARM)
// C1 constructors return pointer
typedef RT* (*ABIFptr)(void*, T1, T2, T3, T4);
(void)((ABIFptr)fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT*>(memory);
#elif defined(CPU_ARM_64)
// C1 constructors return void
typedef void (*ABIFptr)(void*, T1, T2, T3, T4);
((ABIFptr)fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT*>(memory);
#elif defined(CPU_X86) || defined(CPU_X86_64)
// ctor returns void
typedef void (*ABIFptr)(void *, T1, T2, T3, T4);
((ABIFptr) fptr)(memory, param1, param2, param3, param4);
return reinterpret_cast<RT *>(memory);
#else
return nullptr;
#endif
}
template<typename T>
void callDestructor(void (*fptr)(), T *obj) {
#if defined(CPU_ARM)
// D1 destructor returns ptr
typedef void* (*ABIFptr)(T* obj);
(void)((ABIFptr)fptr)(obj);
#elif defined(CPU_ARM_64)
// D1 destructor returns void
typedef void (*ABIFptr)(T* obj);
((ABIFptr)fptr)(obj);
#elif defined(CPU_X86) || defined(CPU_X86_64)
// dtor returns void
typedef void (*ABIFptr)(T *obj);
((ABIFptr) fptr)(obj);
#endif
}
template<typename T1, typename T2>
T1 *pointerToOffset(T2 *ptr, size_t bytes) {
return reinterpret_cast<T1 *>((uint8_t *) ptr + bytes);
}
static android::android_native_base_t *getAndroidNativeBase(android::GraphicBuffer *gb) {
return pointerToOffset<android::android_native_base_t>(gb, 2 * sizeof(void *));
}
GraphicBuffer::GraphicBuffer(uint32_t width, uint32_t height, PixelFormat format, uint32_t usage) :
library("libui.so") {
setFuncPtr(functions.constructor, library, "_ZN7android13GraphicBufferC1Ejjij");
setFuncPtr(functions.destructor, library, "_ZN7android13GraphicBufferD1Ev");
setFuncPtr(functions.getNativeBuffer, library,
"_ZNK7android13GraphicBuffer15getNativeBufferEv");
setFuncPtr(functions.lock, library, "_ZN7android13GraphicBuffer4lockEjPPv");
setFuncPtr(functions.unlock, library, "_ZN7android13GraphicBuffer6unlockEv");
setFuncPtr(functions.initCheck, library, "_ZNK7android13GraphicBuffer9initCheckEv");
// allocate memory for GraphicBuffer object
void *const memory = malloc(GRAPHICBUFFER_SIZE);
if (memory == nullptr) {
std::cerr << "Could not alloc for GraphicBuffer" << std::endl;
return;
}
try {
android::GraphicBuffer *const gb =
callConstructor4<android::GraphicBuffer, uint32_t, uint32_t, PixelFormat, uint32_t>(
functions.constructor,
memory,
width,
height,
format,
usage
);
android::android_native_base_t *const base = getAndroidNativeBase(gb);
status_t ctorStatus = functions.initCheck(gb);
if (ctorStatus) {
// ctor failed
callDestructor<android::GraphicBuffer>(functions.destructor, gb);
std::cerr << "GraphicBuffer ctor failed, initCheck returned " << ctorStatus <<
std::endl;
}
// check object layout
if (base->magic != 0x5f626672u) // "_bfr"
std::cerr << "GraphicBuffer layout unexpected" << std::endl;
// check object version
const uint32_t expectedVersion = sizeof(void *) == 4 ? 96 : 168;
if (base->version != expectedVersion)
std::cerr << "GraphicBuffer version unexpected" << std::endl;
base->incRef(base);
impl = gb;
} catch (...) {
free(memory);
throw;
}
}
GraphicBuffer::~GraphicBuffer() {
if (impl) {
android::android_native_base_t *const base = getAndroidNativeBase(impl);
base->decRef(base);
//no need to call it, decRef will do
//callDestructor<android::GraphicBuffer>(functions.destructor, impl);
}
}
status_t GraphicBuffer::lock(uint32_t usage, void **vaddr) {
return functions.lock(impl, usage, vaddr);
}
status_t GraphicBuffer::unlock() {
return functions.unlock(impl);
}
/// Here it is, the windowbuffer !!!
ANativeWindowBuffer *GraphicBuffer::getNativeBuffer() const {
return functions.getNativeBuffer(impl);
}
uint32_t GraphicBuffer::getStride() const {
return ((android::android_native_buffer_t *) getNativeBuffer())->stride;
}
References:

Android - IOCTL usage returns ENOTTY

I am trying to run a simple IOCTL example on Android. I am using kernel 2.6 and ICS. The module is properly registered/unregistered (insmod/rmmod). However, every time a try to execute ./user_app on the emulator, I always get
error: first ioctl: Not a typewriter
error: second ioctl: Not a typewriter
message: `�
This is clearly a ENOTTY. I debugged the application, and no fops procedure (device_ioctl, read_ioctl and write_ioctl) is being executed.
I would like to know if there is any restriction with the usage/implementation of IOCTL on Android. Thank you very much in advance.
--Raul
Here is the code:
module.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#define MY_MACIG 'G'
#define READ_IOCTL _IOR(MY_MACIG, 0, int)
#define WRITE_IOCTL _IOW(MY_MACIG, 1, int)
int main(){
char buf[200];
int fd = -1;
if ((fd = open("/data/local/afile.txt", O_RDWR)) < 0) {
perror("open");
return -1;
}
if(ioctl(fd, WRITE_IOCTL, "hello world") < 0)
perror("first ioctl");
if(ioctl(fd, READ_IOCTL, buf) < 0)
perror("second ioctl");
printf("message: %s\n", buf);
return 0;
}
user_app.c
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define MY_MACIG 'G'
#define READ_IOCTL _IOR(MY_MACIG, 0, int)
#define WRITE_IOCTL _IOW(MY_MACIG, 1, int)
static char msg[200];
static ssize_t device_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset)
{
...
}
static ssize_t device_write(struct file *filp, const char __user *buff, size_t len, loff_t *off)
{
...
}
char buf[200];
int device_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) {
int len = 200;
switch(cmd) {
case READ_IOCTL:
...
break;
case WRITE_IOCTL:
...
break;
default:
return -ENOTTY;
}
return len;
}
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
};
static int __init example_module_init(void)
{
printk("registering module");
return 0;
}
static void __exit example_module_exit(void)
{
printk("unregistering module");
}
module_init(example_module_init);
module_exit(example_module_exit);
MODULE_LICENSE("GPL");
It this the whole code that you've posted? You don't register a char device when initializing a module, so this can't work.
Also, be carefull when assigning IOCTLS numbers. When using reserved IOCTL on a wrong file, you will get ENOTTY. Consult this to make sure you don't have conflicts.
Read more about char drivers here.

Qt/Necessitas - reasonable QFileDialog replacement/skin?

I'm looking for a nice way to address porting Qt applications to Qt/Necessitas (Android).
Some of the QtGUI widgets are absolutely atrocious - unfortunately, including QFileDialog.
Do you know of any replacements with a proper look and feel?
Is making QFileDialog usable anywhere near high priority for Necessitas developers?
#include <QApplication>
#include <QFileDialog>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QString fileName = QFileDialog::getOpenFileName(NULL,
QObject::tr("Open Image"), "/home/jana", QObject::tr("Image Files (*.png *.jpg *.bmp)"));
a.exec();
}
Android does not have own, native file dialog. We can use QtAndroidExtras to invoke external application which is able to pick a file:
I wrote wrapper, that could be used for that. Here's full code:
androidfiledialog.h
#ifndef ANDROIDFILEDIALOG_H
#define ANDROIDFILEDIALOG_H
#include <QObject>
#include <QAndroidJniObject>
#include <QtAndroid>
#include <QAndroidActivityResultReceiver>
class AndroidFileDialog : public QObject
{
Q_OBJECT
public:
explicit AndroidFileDialog(QObject *parent = 0);
virtual ~AndroidFileDialog();
bool provideExistingFileName();
private:
class ResultReceiver : public QAndroidActivityResultReceiver {
AndroidFileDialog *_dialog;
public:
ResultReceiver(AndroidFileDialog *dialog);
virtual ~ResultReceiver();
void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
QString uriToPath(QAndroidJniObject uri);
};
static const int EXISTING_FILE_NAME_REQUEST = 1;
ResultReceiver *receiver;
void emitExistingFileNameReady(QString result);
signals:
void existingFileNameReady(QString result);
};
#endif // ANDROIDFILEDIALOG_H
androidfiledialog.cpp
#include "androidfiledialog.h"
AndroidFileDialog::ResultReceiver::ResultReceiver(AndroidFileDialog *dialog) : _dialog(dialog) {}
AndroidFileDialog::ResultReceiver::~ResultReceiver() {}
void AndroidFileDialog::ResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
{
jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
if (receiverRequestCode == EXISTING_FILE_NAME_REQUEST && resultCode == RESULT_OK) {
QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
QString path = uriToPath(uri);
_dialog->emitExistingFileNameReady(path);
} else {
_dialog->emitExistingFileNameReady(QString());
}
}
QString AndroidFileDialog::ResultReceiver::uriToPath(QAndroidJniObject uri)
{
if (uri.toString().startsWith("file:", Qt::CaseInsensitive)) {
return uri.callObjectMethod("getPath", "()Ljava/lang/String;").toString();
} else {
QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
QAndroidJniObject DATA = QAndroidJniObject::fromString("_data");
jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", DATA.object<jstring>());
cursor.callMethod<jboolean>("moveToFirst", "()Z");
QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
return result.isValid() ? result.toString() : QString();
}
}
AndroidFileDialog::AndroidFileDialog(QObject *parent) : QObject(parent)
{
receiver = new ResultReceiver(this);
}
AndroidFileDialog::~AndroidFileDialog()
{
delete receiver;
}
bool AndroidFileDialog::provideExistingFileName()
{
QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
QAndroidJniObject intent("android/content/Intent");
if (ACTION_GET_CONTENT.isValid() && intent.isValid()) {
intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_GET_CONTENT.object<jstring>());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("file/*").object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver);
return true;
} else {
return false;
}
}
void AndroidFileDialog::emitExistingFileNameReady(QString result)
{
emit existingFileNameReady(result);
}
You have to add to your *.pro file:
QT += androidextras
using example:
AndroidFileDialog *fileDialog = new AndroidFileDialog();
connect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
bool success = fileDialog->provideExistingFileName();
if (!success) {
qDebug() << "Problem with JNI or sth like that...";
disconnect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
//or just delete fileDialog instead of disconnect
}
slot implementation:
void MyClass::openFileNameReady(QString fileName)
{
if (!fileName.isNull()) {
qDebug() << "FileName: " << fileName;
} else {
qDebug() << "User did not choose file";
}
}
Please confirm this solution works properly on your device.
You could easily build your own file dialog either with QtWidgets or QML, by using the out-of-the-box QFileSystemModel class or the FolderListModel element.
As for whether it is priority or not, at this point it seems that Necessitas will be absorbed by Digia's efforts to support Android. I doubt there will be significant efforts to style QtWidgets appropriately, since the module is marked as DONE and all the focus for UI is on QML. So, I wouldn't hold by breath if I were you. Plus the stock Qt widgets look completely ugly on non-desktop platforms.

Categories

Resources