Re: Surface flinger permission error

1,245 views
Skip to first unread message
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

JC

unread,
Oct 4, 2010, 6:18:03 AM10/4/10
to android-ndk
Hello Dianne H,

I have tried to use Surface & SurfaceView class as follows:

I have created my JAVA class from SurfaceView, also implementing
SurfaceHolder.Callback

JAVA Code:
===========

// class which will be passed to jni
class MySurface
{
Object mysurface;
};


class Test extends SurfaceView implements SurfaceHolder.Callback
{
...

public void surfaceCreated(SurfaceHolder holder)
{
//get surface from surface holder.
Surface sur = holder.getSurface();

// init class for jni
JNIClass objJNIClass = new JNIClass();

// init param for jni call
MySurface testSurface = new MySurface();

// setting surface in class
testSurface.mysurface = sur;

// calling jni method of class
objJNIClass.SetSurface(testSurface);
}

// JNIClass.c
===============
JNIEXPORT jint JNICALL Java_com_picus_utils_CJNIClass_1SetSurface
(JNIEnv *env, jobject objThis, jobject objTestSurface)
{

jclass class_OutputList = env->GetObjectClass(objTestSurface);
jfieldID id_object = env->GetFieldID
(class_OutputList,"mysurface","Ljava/leng/object;");
jobject surface = env->GetObjectField(objTestSurface,id_object);

// calling c function
return SetSurface((void*)surface);

}


// Actual C function
int SetSurface(void* pSurface)
{

sp<Surface> surf = (Surface*)pSurface;


}

It is crashing when doing above assignment in actual C function.
Please guide me how to use pSurface in C code to post buffers to
display !!!

I have taken surface object's type as void* because in JNI code I
wasn't able to find Surface definition. Do i need to typecast in JNI
code?

Thanks,
JC


On Sep 14, 9:39 am, Dianne Hackborn <hack...@android.com> wrote:
> The API is Java (SurfaceView and Surface are indeed what you want).  You'll
> just need to use JNI to call through to the Java API.
>
>
>
>
>
> On Mon, Sep 13, 2010 at 8:24 PM, JC <j...@oriolesoftware.com> wrote:
> > Thanks David,
>
> > I have gone thru the classes provided on website.
> > classes that seems to be useful are
> > Surface, SurfaceView... but there is no direct class/API to use
> > flinger.
>
> > can u point me correct direction by psudo/example code/method. It
> > would be greatful.
>
> > Thanks in advance.
>
> > JC
>
> > On Sep 13, 6:39 pm, David Turner <di...@android.com> wrote:
> > > On Mon, Sep 13, 2010 at 11:51 AM, JC <j...@oriolesoftware.com> wrote:
>
> > > > David,
> > > > What could be other way to work with Surface flinger? From JAVA-JNI?
>
> > > Only use the public APIs documented on developer.android.com. For
> > > SurfaceFlinger, you should only use the Java APIs (even if you access
> > > through JNI in your native code)
>
> > > > Mic_
> > > > I am using Eclipse, as per document it will create .keystore file and
> > > > signed APK file before loading onto emulator. (http://
> > > > developer.android.com/guide/publishing/app-signing.html#setup)
> > > > How can application get signed?
>
> > > > Ganesh,
> > > > I am checking mentioned thing.
>
> > > > Thanks in advance
> > > > JC
>
> > > > On Sep 13, 2:40 pm, David Turner <di...@android.com> wrote:
> > > > > Don't do that, SurfaceFlinger is a very volatile part of the system.
> > > > Unless
> > > > > you're considering a custom system image (instead of an application),
> > > > > chances that your code is going to work properly or even simply
> > link/run
> > > > on
> > > > > all devices or on future platform releases are pretty weak.
>
> > > > > On Mon, Sep 13, 2010 at 10:04 AM, JC <j...@oriolesoftware.com>
> > wrote:
> > > > > > Hello All,
>
> > > > > > My C code uses surface flinger code, we have written C wrapper for
> > > > > > this based on (http://people.debian.org.tw/~olv/surfaceflinger/
> > > > > > demo.tar.gz )
>
> > > > > > My jni SO file built successfully.
>
> > > > > > In AndroidMenifest.xml, I have added following:
>
> > > > > > <uses-permission
> > > > > > android:name="android.permission.ACCESS_SURFACE_FLINGER" />
>
> > > > > > But still during runtime i am getting following error..
>
> > > > > > 09-13 07:46:34.973: WARN/ServiceManager(57): Permission failure:
> > > > > > android.permission.ACCESS_SURFACE_FLINGER from uid=10033 pid=22849
> > > > > > 09-13 07:46:34.973: ERROR/SurfaceFlinger(57): Permission Denial:
> > can't
> > > > > > access SurfaceFlinger pid=22849, uid=10033
> > > > > > 09-13 07:46:34.983: ERROR/Demo(22849): failed to create surface
>
> > > > > > What could be reason and solution?
>
> > > > > > Is there any other way to do to initialize surface flinger from
> > JAVA &
> > > > > > access it in C module? Please provide reference to it?, if any
>
> > > > > > Thanks in advance,
> > > > > > JC
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > Groups
> > > > > > "android-ndk" group.
> > > > > > To post to this group, send email to andro...@googlegroups.com.
> > > > > > To unsubscribe from this group, send email to
> > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "android-ndk" group.
> > > > To post to this group, send email to andro...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.

Tim Mensch

unread,
Oct 4, 2010, 11:14:26 AM10/4/10
to andro...@googlegroups.com
On 10/4/2010 4:18 AM, JC wrote:

> // JNIClass.c =============== JNIEXPORT jint JNICALL
> Java_com_picus_utils_CJNIClass_1SetSurface (JNIEnv *env, jobject
> objThis, jobject objTestSurface) {
>
> jclass class_OutputList = env->GetObjectClass(objTestSurface);

> jfieldID id_object = env->GetFieldID(class_OutputList,"mysurface","Ljava/leng/object;");

> jobject surface = env->GetObjectField(objTestSurface,id_object);
>
> // calling c function return SetSurface((void*)surface);
>
> }
>
> // Actual C function int SetSurface(void* pSurface) {
> sp<Surface> surf = (Surface*)pSurface;
> }
>
> It is crashing when doing above assignment in actual C function.
> Please guide me how to use pSurface in C code to post buffers to
> display !!!


A Java Surface class is not castable to a Surface*. No idea what your
sp<> class does (some kind of smart or shared pointer I assume?), but
the original cast is just a Bad Idea -- and if it is a shared pointer,
then presumably it will end up trying to call delete on pSurface, which
is an Even Worse Idea.

> I have taken surface object's type as void* because in JNI code I
> wasn't able to find Surface definition. Do i need to typecast in JNI
> code?


The Surface definition is here:

http://developer.android.com/reference/android/view/Surface.html

...and requires that you use ALL Java (or JNI) calls, not C++ calls
(which is what Dianne already said). You should keep the surface as a
jobject. See the JNI docs to see how to call Java from C++ using that
jobject.

Tim

Onur Cinar

unread,
Oct 4, 2010, 1:06:42 PM10/4/10
to andro...@googlegroups.com

Hi JC,

First of all, this is not directly supported by the NDK, and it will break your application since it won't be compatible with the new versions of Android OS. So I would suggest using the Java based Surface drawing, and only generating the Bitmap in the native space.

However, as the answer to your question:

You are using the wrong value as the Surface pointer.  The Surface class itself is not the one you need. You will need to get the "mSurface" field from the Surface object, which is in type Surface *. You can then use it from the native code.   Here is a quick example:

In java:
Field field = clazz.getDeclaredField("mSurface");
field.setAccessible(true);
int surfacePointer = field.getInt(surfaceObject);

In native:
sp<Surface> surface = (Surface *) surfacePointer;

Then you can do lock/unlockAndPost.


Regards,

-onur


To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

Tim Mensch

unread,
Oct 4, 2010, 1:51:44 PM10/4/10
to andro...@googlegroups.com
On 10/4/2010 11:06 AM, Onur Cinar wrote:
> First of all, this is not directly supported by the NDK, and it will
> break your application since it won't be compatible with the new
> versions of Android OS.
Using the C++ pointer could break on existing phones; there's no
guarantee that the C++ Surface class being used by any vendor has the
fields in the same order, even. They can change anything they want
that's not part of the public API.

Tim

Onur Cinar

unread,
Oct 4, 2010, 2:04:19 PM10/4/10
to andro...@googlegroups.com

Hi Tim,

I'm totally agree.

And also based on my earlier tests, I didn't see any huge performance improvement using the native Surface also.  Using the Java based Surface for drawing, and the native Bitmap interface to generate the bitmap is the most efficient way for now.

Regards,

-onur




--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

JC

unread,
Oct 5, 2010, 2:26:31 AM10/5/10
to android-ndk
Hello onur,

I have tried following in JAVA,
.java file
=============

Surface sur = holder.getSurface();
Field fi = sur.getClass().getDeclaredField("mSurface");
// Field fi = this.getClass().getDeclaredField("mSurface"); // where
this is surfaceview derived class
fi.setAccessible(true);
int surfactptr = fi.getInt(sur);

But eclipse builder gives error no such field exception. In android
\view\Surface.java mSurface is private member. Is error due to this?

Please guide further.

Thanks,
JC
> >http://people.debian.org.tw/~olv/surfaceflinger/<http://people.debian.org.tw/%7Eolv/surfaceflinger/>
> > > > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > > .
> > > > > > > > For more options, visit this group at
> > > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > --
> > > > > > You received this message because you are subscribed to the Google
> > > > Groups
> > > > > > "android-ndk" group.
> > > > > > To post to this group, send email to andro...@googlegroups.com.
> > > > > > To unsubscribe from this group, send email to
> > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > .
> > > > > > For more options, visit this group at
> > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "android-ndk" group.
> > > > To post to this group, send email to andro...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > hack...@android.com
>
> > > Note: please don't send private questions to me, as I don't have time to
> > > provide private support, and so won't reply to such e-mails.  All such
> > > questions should be posted on public forums, where I and others can see
> > and
> > > answer them.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> ---www.zdo.com

JC

unread,
Oct 5, 2010, 7:30:36 AM10/5/10
to android-ndk
Hello Onur & All,

Thanks for your valuable support, We are able to get surface in JNI-
native & c.

We are posting code for reference of others.

.JAVA - SurfaceView derived class
=================================
... OnCreated(holder)
{
...
Surface javaSurface = holder.getSurface();
...
}

JNI-CPP file
=============
JNIMethod(jobject javaSurface)
{
// get java surface class
jclass surface = env->FindClass("android/view/Surface");
if (surface == NULL)
{
__android_log_write(ANDROID_LOG_DEBUG,TAG,"FindClass failed");
return -1;
}

/* Access mSurface member integer in our Surface class */
jfieldID surfaceID = env->GetFieldID(surface, "mSurface", "I");
if (surfaceID == NULL)
{
__android_log_write(ANDROID_LOG_DEBUG,TAG,"GetFieldId mSurface
failed");
return -1;
}

// get surface ptr from javasurface as per surfaceview field id
void* pSurfaceObj = (void*)env->GetIntField(javaSurface, surfaceID);
if (DisplayInfo.pSurfaceObj == NULL)
{
__android_log_write(ANDROID_LOG_DEBUG,CF_TAG,"DisplayInfo#GetIntField
last failed");
return -1;
}
}
// call c method
SetSurface(pSurfaceObj);

Thanks,
JC
> > > > > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > > > .
> > > > > > > > > For more options, visit this group at
> > > > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > > --
> > > > > > > You received this message because you are subscribed to the Google
> > > > > Groups
> > > > > > > "android-ndk" group.
> > > > > > > To post to this group, send email to andro...@googlegroups.com.
> > > > > > > To unsubscribe from this group, send email to
> > > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > .
> > > > > > > For more options, visit this group at
> > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > "android-ndk" group.
> > > > > To post to this group, send email to andro...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > --
> > > > Dianne Hackborn
> > > > Android framework engineer
> > > > hack...@android.com
>
> > > > Note: please don't send private questions to me, as I don't have time to
> > > > provide private support, and so won't reply to such e-mails.  All such
> > > > questions should be posted on public forums, where I and others can see
> > > and
> > > > answer them.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "android-ndk" group.
> > > To post to this group, send email to andro...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-ndk?hl=en.
>
> > ---www.zdo.com

Onur Cinar

unread,
Oct 5, 2010, 12:04:09 PM10/5/10
to andro...@googlegroups.com

Hi JC,

Yes, the mSurface is private in the Surface class, but the setAccessible should be taking care of that. Strange, let me copy/paste my both Java and JNI code for that, hopefully one of them would work.

In the following code, holder is the android.view.SurfaceHolder, and I'm calling this method in/after SurfaceHolder.Callback#surfaceCreated.

{
        int instance = 0;
       
        Surface surface = holder.getSurface();
        Class<? extends Surface> clazz = surface.getClass();
       
        try
        {

            Field field = clazz.getDeclaredField("mSurface");
            field.setAccessible(true);
           
            instance = field.getInt(surface);
        }
        catch (Exception ex)
        {
            Log.e(LOG_TAG, "Unable to get surface", ex);
        }
}

As an alternative, here is my old JNI code for that, where surface is a jobject.

{
    jclass clazz = env->FindClass("android/view/Surface");

    jfieldID fieldId = env->GetFieldID(clazz, "mSurface", "I");

    env->GetIntField(surface, fieldId);
}

If you can e-mail the portion of your code, I can try it here also.

Regards,

-onur


To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

JC

unread,
Oct 6, 2010, 5:20:34 AM10/6/10
to android-ndk
Hello Onur,

I am facing access problem while doing register buffer from isurface.
Please use my previous JAVA/JNI code to get mSurface to my C code. I
am posting further code when i m using surface.

.C file
=======

// global
sp<Surface> surface ;
int frame_offset[2];

/*
* The only purpose of class "MediaPlayer" is to call
Surface::getISurface()
* in frameworks/base/include/ui/Surface.h, which is private function
and accessed
* by friend class MediaPlayer.
*
* We define a fake one to cheat compiler
*/
namespace android {
class MediaPlayer
{
public:
static const sp<ISurface> getSurface(const sp<Surface>& surface)
{
return surface->getISurface();
};
};
};


// c method
void SetSurface(void *pSurface)
{
surface = (Surface *)pSurface;

/* get ISurface interface */
sp<ISurface> isurface = MediaPlayer::getSurface(surface);

if(isurface != NULL)
{
frameSize = 320*2*240;
/* create frame buffer heap base */
frame_heap = new MemoryHeapBase(frameSize * 2);
if (frame_heap->heapID() < 0)
{
LOG("Error creating frame buffer heap!");
return -1;
}
LOG("buffers in!");
/* create frame buffer heap and register with surfaceflinger */
ISurface::BufferHeap buffers(
320,
240,
320,
240,
RGB_565,
frame_heap);

isurface->registerBuffers(buffers);

frame_offset[0]=frame_offset[1] = frameSize ;

// get filled buffer in frame_offset[0]

isurface->postBuffer(frame_offset[0]);

}

Now problem is when i am doing registerbuffer it gives me following
permission error ... and nothing appear on screen. I have given
ACCESS_SURFACE_FLINGER in my application manifest file.


09-13 07:46:34.973: WARN/ServiceManager(57): Permission failure:
android.permission.ACCESS_SURFACE_FLINGER from uid=10033 pid=22849
09-13 07:46:34.973: ERROR/SurfaceFlinger(57): Permission Denial: can't
access SurfaceFlinger pid=22849, uid=10033

Please guide how to resolve it.

Thanks in advance,
JC
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > > > > .
> > > > > > > > > > For more options, visit this group at
> > > > > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > > > --
> > > > > > > > You received this message because you are subscribed to the
> > Google
> > > > > > Groups
> > > > > > > > "android-ndk" group.
> > > > > > > > To post to this group, send email to
> > andro...@googlegroups.com.
> > > > > > > > To unsubscribe from this group, send email to
> > > > > > > > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>
>
> > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > <android-ndk%2Bunsubscribe@googlegr oups.com>
> > > > > > > > .
> > > > > > > > For more options, visit this group at
> > > > > > > >http://groups.google.com/group/android-ndk?hl=en.
>
> > > > > > --
> > > > > > You received
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages