Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Framing the Canvas (November 2014)

Framing the Canvas (November 2014)

[Presented at Berlin DevFest 2014 and Droidcon NL 2014]

This talk will guide you through a topic that is often ignored: the Canvas API. Even if there's only scarce documentation for it, the Canvas API is at the heart of everything that shows a UI on Android. You'll need to tame this powerful and mysterious creature if you want to create great custom views and brilliant, lightweight UIs for your apps.

What is a Canvas? How do you Paint? Can you draw Paths? What is Skia, and who invited it anyway? What actually happens during a drawing pass? What are Shaders? What are Filters? Is it true that text is basically impossible to measure correctly? What shape does a Shape have if noone's painting it? These and other questions will get an answer during this session. Well, some of them will not, probably.

Connect the dots in the spotty documentation the Android team has assembled, and become a true pixel pusher!

Sebastiano Poggi

November 22, 2014
Tweet

More Decks by Sebastiano Poggi

Other Decks in Programming

Transcript

  1. • Base for any Android UI • Drawing on a

    buffer • Hardware or Software • API: Canvas, Paint, … Canvas CC-BY Cara St.Hilaire on Flickr
  2. We should love Skia • Underneath Canvas • Native code

    • 2D graphics rendering toolkit • Basic painting • Advanced features walcor on DeviantArt
  3. • Text is a pain • Weird quirks • E.g.,

    no proper arcs • No documentation …but Skia kinda sucks Eugenio Marletti on Medium
  4. Google • Draw using HWUI • Subset of Skia •

    Honeycomb and later • With limitations (of course) • Revert to software buffer OK, Canvas is still cool
  5. Using the Canvas • Transforms using matrices and Cameras •

    Views must use it on the UI thread • Or go with a SurfaceView • Canvas is stateful
  6. State of the art 1.Call save() — or saveToCount() 2.Do

    your stuff 3.Restore original state: restore() — or restoreToCount()
  7. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state);
  8. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); canvas
  9. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); canvas
  10. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); canvas
  11. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); offscreen buffer
  12. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); offscreen buffer
  13. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); offscreen buffer PROFIT!
  14. Seb’s top tip Alpha composite overlaps, the framework way 1.int

    state = saveLayerAlpha(…); 2.Do your stuff 3.Blit back: restoreToCount(state); canvas PROFIT!
  15. Surfaces and flingers • Draw on surfaces • Buffers •

    SurfaceFlinger • SW or HW backing CC-BY-NC-SA Nicolas Hoizey on Flickr
  16. Layers • Offscreen buffers • Every Canvas has one •

    Can be blitted with a Paint • Different types of layer CC-BY-NC-SA Doug88888 on Flickr
  17. View Layer types (3.0+) • NONE: no buffer • SOFTWARE:

    bitmap buffer • HARDWARE: HW-backed texture • Can improve performances (e.g., animations)
  18. Canvas API Paint Shape Path Matrix Color Drawable Bitmap Camera

    Movie NinePatch Picture Interpolator SurfaceTexture Typeface
  19. Drawable Canvas API Shape Path Color Bitmap Camera Movie Picture

    Interpolator SurfaceTexture Typeface Matrix NinePatch Paint
  20. Paint it (black) • Knows how to draw the pixels

    • All drawing calls need a Paint • Handles text as well • TextPaint subclass CC-BY-NC-ND Mark Chadwick on Flickr
  21. Effective painting • Steps have two phases each • “Two-pass”

    strategy • Effects modify steps output • Second passes default to identity CC-BY-NC-SA ClintJCL on Flickr
  22. Mask filters • Rasterization phase • Affect alpha mask •

    Not HW accelerated • Blur and Emboss P S T R
  23. Shaders • Shading phase • Similar to OpenGL shaders •

    Not programmable • TileMode CC-BY-NC-SA Andreas Köberle on Flickr P S T R
  24. A Shady bunch Shader BitmapShader ComposeShader *Gradient Use a Bitmap

    as texture when painting Combine two different shaders and mix them with a Xfermode Paint using a Linear, Radial or Sweep gradient P S T R
  25. Color Filters • Adjust colours after Shaders • Uniform transformation

    for all pixels ColorFilter ColorMatrixColorFilter LightingColorFilter PorterDuffColorFilter Apply a 4x5 colour matrix transform Multiply SRC colours by a colour, and then add a second colour Apply a colour to the SRC colour using a Porter-Duff mode P S T R
  26. Transfer modes • Second step of Transfer phase • Blend

    SRC image onto DST through mask Xfermode AvoidXfermode PixelXorXfermode PorterDuffXfermode Draw (or don’t draw) pixels based on the “distance” from a reference Exclusive-OR source and destination pixels. Drops the alpha channel! Blends the source and destination colours using a Porter-Duff mode P S T R
  27. Q&A