Skia & FreeType Android 2D Graphics Essentials Kyungmin Lee Software Platform Lab., LG Electronics [email protected] The 10th Kandroid Conference Goal S/W 플랫폼에 있어 GUI(Graphical User Interface) 시스템은 해당 플랫폼의 성능을 결정짓는 매우 중요한 요소이다. Android 역시 새로운 버전을 발표할 때마다 GUI 시스템의 성능을 꾸준히 개선하여 최근에 릴리즈된 Jelly Bean에 와서는 iOS에 필적할만한 성능을 보여주고 있다. 이번 세션에서는 이벤트 처리 기술과 더불어 GUI 시스템의 한 축을 이루고 있는 2D Graphics 기술을 Android에서 어떻게 활용하고 발전시켜 왔는지에 대해 살펴본다. 1. Skia: 2D Graphics Library Skia in Android Skia API Overview Skia Rendering Pipeline Skia Backends: Raster, OpenGL, PDF, ... 2. FreeType: Font Engine FreeType in Android FreeType API Overview Font Rasterization: Scan-conversion, Hinting, … 3. Android 2D Graphics Essentials Evolution of Drawing Models: GPUI (UI on the GPU), Display List, Display List Properties, View Layers, … Android 2D Graphics Architecture (From Conference Program Overview) The 9th Kandroid Conference Skia is Greek for “shadow” http://code.google.com/p/skia/ Skia is a complete 2D graphic library for drawing Text, Geometries, and Images. • 3x3 matrices w/ perspective • antialiasing, transparency, filters • shaders, xfermodes, maskfilters, patheffects Mike Reed • subpixel text Device backends for Skia currently include: • Raster • OpenGL • PDF • XPS • Picture (for recording and then playing back into another Canvas) (From Project‟s Home) The Skia Graphics Engine is a compact open source graphics library written in C++. It was originally developed by Skia Inc., which was subsequently acquired by Google in 2005, who then released the software as open source licensed under the New BSD License free software license. Now known as skia, it is currently used in Mozilla Firefox, Google Chrome, Chrome OS, Chromium OS, and Android; and the Skia library is present on the BlackBerry PlayBook though the extent of its usage is unclear. Skia's drawing has back-ends for a standard CPU-based software rasterizer, PDF, and OpenGL (From Wikipedia) The 10th Kandroid Conference Skia in Android Skia (svn) Skia in Android 2006-09 r1 2006-09 r2 2006-11 r3 2008-10 2008-11 r4 … 2009-03 r100 2011-03 r1000 2011-07 r2000 … 2012-01 r3000 ??? 2012-05 r4000 2012-08 r5000 ~700 2012-10 r6022 total commit # Added Removed The 10th Kandroid Conference Skia API Overview A Canvas encapsulates all of the state about drawing into a device (bitmap). This includes a reference to the device itself, and a stack of matrix/clip values. For any given draw call (e.g. drawRect), the geometry of the object being drawn is transformed by the concatenation of all the matrices in the stack. The transformed geometry is clipped by the intersection of all of the clips in the stack. While the Canvas holds the state of the drawing device, the state (style) of the object being drawn is held by the Paint, which is provided as a parameter to each of the draw() methods. The Paint holds attributes such as color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns), etc. Drawing basic primitives include rectangles, rounded rectangles, ovals, circles, arcs, paths, lines, text, bitmaps and sprites. Paths allow for the creation of more advanced shapes. Each path can be made up of multiple contours (or continuous sections), each consisting of linear, quadratic, and cubic segments Drawing Primitives Canvas Bitmap Paint The 10th Kandroid Conference Skia API Overview SkRect rect; rect.set(0, 0, 150, 120); canvas->drawRect (rect, shapePaint); canvas->drawRoundRect (rect, 15, 15, shapePaint); canvas->drawOval(rect, shapePaint); canvas->drawCircle(60, 60, 60, shapePaint); // Arc without a wedge canvas->drawArc (rect, 0, 255, false, shapePaint); // Arc with a wedge from the center canvas->drawArc (rect, 0, 255, true, shapePaint); canvas->drawLine(0, 0, 150, 120, shapePaint); const char str[] = "Hello World!"; canvas->drawText(str, strlen(str), 75, 145, textPaint); // Load a bitmap from an image and draw it only if the load succeeds SkBitmap bitmap; if (SkImageDecoder::DecodeFile("app/native/icon.png", bitmap)) { canvas->drawBitmap(*bitmap, 0, 0, NULL); } SkPath path; path.moveTo(50, 50); // Specify endpoint using absolute coordinates path.lineTo(100, 100); // Specify endpoint using a relative offset from the last point path.rLineTo(50, 50); // Specify endpoint using absolute coordinates path.quadTo(120, 125, 150, 150); // Specify endpoint using a relative offset from the last point path.rQuadTo(20, 25, 50, 50); // Specify endpoint using absolute coordinates path.cubicTo(175, 175, 140, 120, 200, 200); // Specify endpoint using a relative offset from the last point path.TherQuadTo 10th(25 Kandroid, 25, -10, Conference -30, 50, 50 ); canvas->drawPath(path, shapePaint); Skia Rendering Pipeline Rasterization Shading SkCanvas draw*(…, SkPaint) Apply Scan Shaders Conversion Initial Path Initial Initial Mask Rasterizer SRC Image Apply Apply Apply Path Effects Mask Filters Color Filters Apply Stroke Styles Mask SRC Image Path Generation Transfer Path Intermediate Apply Blending Image XferMode Modified DST Image DST Image http://www.xenomachina.com/2011/05/androids-2d-canvas-rendering-pipeline.html The 10th Kandroid Conference Skia Backends To render with software Skia 1) Create a native window and then 2) Wrap a pointer to its buffer as an SkBitmap 3) Initialize an SkCanvas with the bitmap SkCanvas SkDevice SkBitmap To render with hardware-accelerated Skia 1) Create a GLES2 window or framebuffer and 2) Create the appropriate GrContext, SkGpuDevice, and SkGpuCanvas SkGpuCanvas SkGpuDevice GrContext The 10th Kandroid Conference FreeType?A Free, High-Quality, and Portable Font Engine http://code.google.com/p/skia/ FreeType is a software library written in C that implements a font rasterization engine. It is used to render text on to bitmaps and provides support for other font- related operations. (From Wikipedia) FreeType 2 is a software font engine that is designed to be small, efficient, highly customizable, and portable while capable of producing high-quality output (glyph images). It can be used in graphics libraries, display servers, font conversion tools, text image generation tools, and many other products as well. Note that FreeType 2 is a font service and doesn't provide APIs to perform higher- level features like text layout or graphics processing (e.g., colored text rendering, „hollowing‟, etc.). However, it greatly simplifies these tasks by providing a simple, easy to use, and uniform interface to access the content of font files. By default, FreeType 2 supports the following font formats: TrueType (and collections), Type 1, CID-keyed Type 1, CFF, OpenType (both , TrueType and CFF variants), SFNT-based bitmap, X11 PCF, Windows FNT, BDF (including anti-aliased ones), PFR, Type 42 (limited support) (From Project‟s Home) David Turner Robert Wilhelm Werner Lemberg The 10th Kandroid Conference FreeType in Android FreeType2 FreeType2 in Android 2000-11 2.0.0 … … 2007-07 2.3.5 2008-06 2.3.6 ??? 2009-03 2.3.9 2008-10 2009-06 2010-02 2.3.12 2010-03 2010-08 2.4.2 2010-09 2010-11 2.4.4 2011-01 2010-07 2.4.6 2011-08 Added Removed 2010-10 2.4.7 2011-11 2010-11 2.4.8 2011-12 2012-03 2.4.9 2012-03 2012-06 2.4.10 commit # ~1100 ~50 since 2007-07 The 10th Kandroid Conference FreeType API Glyph Outline (Lines + Curves) Quadratic Bé zier Curve Cubic Bé zier Curve The 10th Kandroid Conference FreeType API FT_Library library; FT_Face face; 1 Initialize the library FT_GlyphSlot slot; FT_Error error; char* filename, text; int pen_x, pen_y, n, num_chars; 2 Load a font face ... /* initilization code */ error = FT_Init_FreeType( &library ); if ( error ) ... /* error handling code */ Set the character size error = FT_New_Face( library, filename, 0, &face ); 3 if ( error ) ... /* error handling code */ (e.g., 50pt at 100dpi) error = FT_Set_Char_Size( face, 50 * 64, 0, 100, 0 ); if ( error ) ... /* error handling code */ Load a glyph image slot = face->glyph; 4 for ( n = 0; n < num_chars; n++ ) { into the slot error = FT_Load_Char( face, text[n], FT_LOAD_DEFAULT ); if ( error ) continue; /* ignore errors */ error = FT_Render_Glyph( face, text[n], FT_RENDER_MODE_NORMAL ); Convert the glyph‟s if ( error ) continue; /* ignore errors */ 5 draw_bitmap( &slot->bitmap, pen_x + slot->bitmap_left, outline to a bitmap pen_y - slot->bitmap_top ); pen.x += slot->advance.x >> 6; Draw the bitmap to } 6 a target surface show_image(); FT_Done_Face ( face ); FT_Done_FreeType( library ); 7 Release resources The 10th Kandroid Conference Font Rasterization: Hinting Font hinting (also known as instructing) is the use of mathematical instructions to adjust the display of an outline font so that it lines up with a rasterized grid. At low screen resolutions, hinting is critical for producing a clear, legible text. It can be accompanied by antialiasing and (on liquid crystal displays) subpixel rendering for further clarity. Hints are usually created in a font editor during the typeface design process and embedded in the font. A font can be hinted either automatically (through processed algorithms based on the character outlines) or set manually. Most font editors are able to do automatic hinting, and this approach is suitable for many fonts. However, commercial fonts of the highest quality are often manually hinted to provide the sharpest appearance on computer displays. Verdana is one example of a font that contains a large amount of hinting data, much of which was accomplished manually by type engineer Tom Rickner. (From Wikipedia) The 10th Kandroid Conference How Views are Drawn in Android pre-3.0? CPU Rasterization + GPU Composition
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages25 Page
-
File Size-