Simple iOS example

773 views
Skip to first unread message

Davy Wentzler

unread,
Aug 3, 2017, 7:52:31 AM8/3/17
to skia-discuss
Hi people,

I'm looking into skia, because drawing using CoreGraphics on iOS is extremely slow. Before delving into OpenGL directly, skia caught my eye. I like it because it resembles how we draw on Android. It was easy to build skia for iOS, but I cannot find a simple buildable example that just pops up a view and does a 'Hello world' kind of thing. Is there any? The documentation also lacks a 'how to start'.

The only two things I found was a SkiaDemoViewController, which does not build because these do not compile:

    GrRenderTarget * renderTarget = _context->wrapBackendRenderTarget(desc);

    SkSurface * surface = SkSurface::NewRenderTargetDirect(renderTarget);


and the iOSSampleApp and SimpleiOSApp in experimental do not feature an xcode project.


Any help appreciated to get me started!


Jim Van Verth

unread,
Sep 1, 2017, 2:03:52 PM9/1/17
to skia-discuss
Hi, sorry about the delayed response. I've updated our SkiaSDLExample to support iOS, so hopefully that should be enough to get you started -- it's in the example/ directory. Due to the way our GN files are set up you can't currently build for iOS directly from our Xcode project, but you can view the files, and build, package and deploy via command line, as shown on this page: https://skia.org/user/build (though you'll need to modify the package_ios.py script to search for your credentials rather than Google's).

To answer your narrower question, you'll want to do something like this to wrap the framebuffer and get an SkCanvas:

    // setup GrContext

    sk_sp<const GrGLInterface> interface(GrGLCreateNativeInterface());

    // setup contexts

    sk_sp<GrContext> grContext(GrContext::MakeGL(interface.get()));

    SkASSERT(grContext);

    // Wrap the frame buffer object attached to the screen in a Skia render target so Skia can

    // render to it

    GrGLint buffer;

    GR_GL_GetIntegerv(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);

    GrGLFramebufferInfo info;

    info.fFBOID = (GrGLuint) buffer;

    GrBackendRenderTarget target(dm.w, dm.h, kMsaaSampleCount, kStencilBits,

                                 kSkia8888_GrPixelConfig, info);

    // setup SkSurface

    SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);

    sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target,

                                                                    kBottomLeft_GrSurfaceOrigin,

                                                                    nullptr, &props));

    SkCanvas* canvas = surface->getCanvas();


Let me know if I can answer any other questions.

Jim


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

Davy Wentzler

unread,
Sep 4, 2017, 1:23:32 PM9/4/17
to skia-discuss
Hi Jim,

Thanks for the explanation. I meanwhile had it working a little differently, but I guess it boils down to the same thing. It actually all works nicely. There is however one thing I haven't figured out and that's solving a memory leak: I do this:

sk_sp<SkTypeface> eauTypeFace = SkTypeface::MakeFromFile([imagepath UTF8String]);


after making the surface and set the type face on my SkPaint object. However, even if I don't assign it to SkPaint with setTypeface and just let this go out of scope, it will lead to a memory leak. I create and remove the Skia/OpenGL views frequently in my app, and using the memory profiler in Xcode, I can see that this leaks. When removing the above line, there is no leak. I cannot call unref and reset does not help either.


Thoughts?

Jim Van Verth

unread,
Sep 8, 2017, 10:54:32 AM9/8/17
to skia-d...@googlegroups.com
I'm not sure what the issue is there -- as far as I can tell that should work. If you replace [imagepath UTF8String] with a C string constant, does it still happen? What refcount does eauTypeface have when you get it from MakeFromFile?

Davy Wentzler

unread,
Sep 25, 2017, 7:53:55 AM9/25/17
to skia-discuss
The refcount is 1. I cannot do a real string constant since the folder where the app is placed on the iPad changes with every run (it's probably a link). So what I did was in the constructor of one of my UI classes (no further Skia usage here), I looked up the path to the ttf font and stored it in a global static NSString. Then upon pressing a button in the UI, I do:

const char* str = [s_fontPath UTF8String];

sk_sp<SkTypeface> face = SkTypeface::MakeFromFile(str);

                                     

if (face && face.get())

{
    NSLog(@"refcount = %d", face->getRefCnt());
}
else
{
    NSLog(@"Face = %p", face.get());

}


refcount always prints out as 1 and every 3rd or 4th press I can see the memory increasing by 0.1 MB. I also see it in the memory profiler. It is never given back, so this really looks like a memory leak.

Jim Van Verth

unread,
Sep 25, 2017, 12:21:59 PM9/25/17
to skia-d...@googlegroups.com, Ben Wagner
Ben, do you have any insights here?
Reply all
Reply to author
Forward
0 new messages