performence #2 gpu
TRANSCRIPT
First,
Idan Felix
3
I’m 33 years old
VaronisAndroid Academy TLV
Yonatan LevinGoogle Developer
Expert & Android @ Gett
Idan FelixSenior Android &
Redhead Varonis
Jonathan Yarkoni
Android Developer & Advocate Ironsource
Android Academy Staff
Britt Barak
Android LeadReal
Muiriel Felix
Android Design
Logistics
https://www.facebook.com/groups/android.academy.ils/
What’s next?13/6 - Britt- View, Animations
4/7 - Yonatan- Networking, JSON, Batching, Location
10/8 - Felix- Battery & CPU
14/9 - Britt- Threading
30 / 10 / 2016New course coming
Register to Meetup,
Join our facebook
Learn Android
Be Awesomeרקורסיה#
What a hell did you do @ San Francisco?
Google IO 2016+
Firebase 2.0
Analytics
Cloud Messaging
Analytics
Analytics
Analytics
Seamless Update
No need to do anything.
Prompt only when download & Install is ready.Just reboot device.
Multi Window Mode and Picture in Picture
Multi Window Mode and Picture in Picture
Notifications
Quick Settings
Doze On the Go
Project Svelte: Background Optimizations
DEPRECATED:CONNECTIVITY_ACTIONACTION_NEW_PICTUREACTION_NEW_VIDEO
Direct boot
Till user open the deviceOnly apps that was configured allow to runDifferent storage
Java 8android { ... defaultConfig { ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}
Fragments
BugFixescommitNow()
Android Studio 2.2
Constrain Layout
APK Analyzer
Android Wear 2.0
Android Wear 2.0
Instant Apps
“With Instant Apps, tapping a link can take you with Deep Links into an Android app in just a few seconds without having to install the app,”
Michael Siliski
#PerfMatters
Colt
McAnlis
Layers of our talk(s)
- Understanding The Theory
- What Can Go Wrong?
- Weapons for the hunt
Warning
Sorry, (Almost) No code
today
But a lot of links!
TheoryA“Understanding is the first step to acceptance,
And only with acceptance can there be recovery.”
J. K. Rowling, Harry Potter and the Goblet of Fire
- Assess the problem and establish acceptable behavior.
- Measure perf b4 modification.
- Identify bottleneck.- Remove bottleneck.- Measure perf after
modification.- If better, adopt.
If worse, put it back.
Methods of Systematic performance improvement
https://en.wikipedia.org/wiki/Performance_tuning
- Figure Out Where You Need to Be
- Determine Where You Are Now
- Decide Whether You Can Achieve Your Objectives
- Develop a Plan for Achieving Your Objectives, and Execute
- Conduct an Economic Analysis
http://www.perfeng.com/papers/step5.pdf
A Word on Premature Optimizations
Meet Prof. Donald KunthProfessor at Stanford, Wrote “The art of computer programming”Few quotes:
“Beware of bugs in the above code; I have only proved it correct, not tried it.”https://en.wikipedia.org/wiki/Donald_Knuth
Meet Prof. Donald Kunth
https://en.wikipedia.org/wiki/Donald_Knuth
Professor at Stanford, Wrote “The art of computer programming”Few quotes:
The psychological profiling [of a programmer] is mostly the ability to shift levels of abstraction, from low level to high level.
Meet Prof. Donald Kunth
https://en.wikipedia.org/wiki/Donald_Knuth
Professor at Stanford, Wrote “The art of computer programming”Few quotes:
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
However...
Knowing these kind of things- Helps you avoid mistakes and bugs- Makes you a better developer- Teaches you the internal workings of the system
- Cost-Effectiveness-wise it’s just being smart:“...Yet we should not pass up our opportunities in that critical 3%”
Colt McAnlis: https://medium.com/google-developers/the-truth-about-preventative-optimizations-ccebadfd3eb5
Target Audience
This lecture is for you if…:- You have a custom view in your app- You have a lot of images in your app- You have an app, or developing an app,
and want to do a better job- You want to become a better developer
Memory
Battery
Radio
GPU
APK Size
CPU
Layout
The Magic<!-- Layout for weather forecast list item for today --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:orientation="horizontal" android:background="@drawable/today_touch_selector">
<LinearLayout android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="7" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:layout_marginLeft="60dp" android:orientation="vertical">
<TextView android:id="@+id/list_item_date_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceLarge" android:fontFamily="sans-serif-condensed" android:textColor="@color/white" />SRC:
https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/res/layout/list_item_forecast_today.xml
Magic
The Magic
Measure Layout
Inflate
Draw
Step 0: Inflate the View Tree- During onCreate() we call setContentView()
- The XML is parsedand a tree of objects is created
- The tree is traversed during nextsteps
Now we have a Data Structure to use
Things get interesting!
Step 1: Measure- Starts with the root- Recursively ask the views (+Children) to measure
themselves.
- This is done by calling onMeasure(int, int)- It’s a negotiation, so onMeasure may be called
multiple times.
- When it’s done, All the views in the tree know their size.
REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
Step 2: Layout- Starts with the root- Recursively position each child
- Done by onLayout(boolean, int, int, int, int)
- Stores the position, and set position for all children
- When it’s done, All the views in the tree knows their position
REF: http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)
- Now that a view knows its position, The onDraw(Canvas) method is called
- That’s where a view draws itself before asking its children to draw.
- The Canvas object generates (or Updates) a list of OpenGL-ES commands (draw-list) to send to the GPU.
Step 3: Draw (AKA Update)
REF: http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas) Guide: http://developer.android.com/training/custom-views/custom-drawing.html
Canvas Methods (and responsibility)drawARGB
drawArc
drawBitmap
drawColor
drawLine
drawPicture
drawText
clipPath
clipRect
quickReject
rotate
scale
skew
translate
Ref: https://developer.android.com/reference/android/graphics/Canvas.html
The Magic
Measure Layout
Inflate
Draw
Plus ça change, plus c'est la même chose
When things change (text, color, size, padding, margin, etc.), A view notifies the system, by callingInvalidate - which will call the onDraw again, orrequestLayout - which will call the entire process again.
Image Credit:Arpit Mathur
It doesn’t end there
Step 4: Execute
The GPU executes the command list,That was generated in onDraw()And was cached
But what if this takes too long?
Solution: Use a double buffer
http://openglbook.com/chapter-1-getting-started.html
Solution: Use a double buffer
http://openglbook.com/chapter-1-getting-started.html
Step 5: VSync
Old CRT screens had to be “synced”, so that the monitor starts a frame at the correct time.
Similarly, Android holds the copy from a back buffer if it is currently drawing to the screen.
https://en.wikipedia.org/wiki/Analog_television#Vertical_synchronization
Britt’s Lecture (13/6)
The Magic
Animate
Measure Layout
TodayInflate
Draw
Execute Sync
Let’s talk about timing
But How Does It Work?
SmoothMotion
60
No Difference
60+
Flip Book
12
Movies
Frames Per Second
Fluid Motion
24+effects
We Have A Winner!
SmoothMotion
60
No Difference
60+
Flip Book
12
Movies
Frames Per Second
Fluid Motion
24+effects
SmoothMotion
60
Colt McAnlis: https://youtu.be/CaMTIgxCSqU
Things to remember- Britt will give a similar explanation on her lecture
on 13/6
- Know thee process,Appreciate, and
Know that the entire process should take less than 16ms
- Things on the GPU:- Should get there as soon as possible
(early)
- Should get there as quickly as possible (small)
- Should stay there for as long as possible(late)
Any Questions?
Part 2:What can possibly
go wrong
Few things in this process can go wrong- Allocations in onDraw()
- A reminder from last week
- Avoiding Redundant Work- Overdraw
- ClipRect
- QuickReject
- Invalidations
- Bitmap Abuse- So many things can go wrong here, OMG...
Allocating objects (new XXX()) might cause a GC (blocking)
So you might drop a frame.
Allocations in onDraw()
But it goes much deeper. See Ian Ni-Lewis: https://youtu.be/HAK5acHQ53E
Solution and Avoidance
Solution: DON’T ALLOCATE OBJECTS IN onDraw() METHOD.
Avoidance: USE LINT (built into the Studio)
https://developer.android.com/studio/write/lint.html || Colt McAnlis: https://youtu.be/Z_huaXCsYyw
STUDIO
But wait -
How can I know if frames gets dropped?
The all-mighty LogCatI/Choreographer(1378): Skipped 55 frames! The application may be doing too much work on its main thread.
But there’s a much cooler tool
GPU Profiling
GPU ProfilingDisplays a graph for each visible app, showing how much time each frame took:The taller the bar, the longer it took to renderThe green line marks the 16 millisecond target. Every time a frame crosses it, your app is missing a frame.https://developer.android.com/studio/profile/dev-options-rendering.html
16ms line
Dropped Frames :(
14
https://developer.android.com/studio/profile/dev-options-rendering.html || Colt McAnlis https://youtu.be/VzYkVL1n4M8
OnDraw work
Copy the list
Execute
glSwapBuffers
Any Questions?
16ms line
Dropped Frames :(
14
accented if >16ms
Shiney colors!
https://youtu.be/erGJw8WDV74
This is where you tell them the biggest lie of the GPU profiler,
Before moving to the next part
BTW - Now for reals, Any questions?
BThe number of files that a given pixel is drawn in a frame.
Overdraw
Colt McAnlis: https://youtu.be/T52v50r-JfE
What is Overdraw
When a pixel is drawn multiple times - That’s overdraw.
it might waste time and energy.
When the GPU executes the display list,It can count how many times each pixel is drawn.
Not like this
Detecting Overdraw
Detecting Overdraw
https://developer.android.com/studio/profile/dev-options-overdraw.html
There’s even a Code-Lab online:https://io2015codelabs.appspot.com/codelabs/android-performance-debug-gpu-overdraw
Roman Nurik: http://www.curious-creature.com/docs/android-performance-case-study-1.html
Fixing Overdraw
There are 2 common reasons for overdraw:- Redundant backgrounds / Redundant transparency - Wasteful onDraw
- Things that aren’t visible at all gets drawn (not using quickReject)
- Things that will be overdrawn gets drawn (not using clipRect)
Colt McAnlis: https://youtu.be/vkTn3Ule4Ps
QuickReject
A method to tell if something can be not drawn at all.Call quickReject to see if you can skip drawing of things that will be off screen.
REF: https://developer.android.com/reference/android/graphics/Canvas.html
ClipRect
ClipRect is a way to avoid OverDraw, By keeping your GPU from drawing pixels that you know that will be obscured by other stuff, you refrain from overdraw.
Step 1:quickReject
Step 2:clipRect
ClipRect vs. QuickReject
Method return type:
Detects... Helps to...
QuickReject boolean Fully invisible stuff
Avoid redundant calls to drawXXX(), Keeping the drawlist short.
ClipRect void Fully and Partially invisible stuff
Avoid drawing pixels, but still executing the draw-list!
booleanBut is usually used as void
Any Questions?
InvalidationsC
Image Credit:Arpit Mathur
Plus ça change, plus c'est la même chose
When things change (text, color, size, padding, margin, etc.), A view notifies the system, by callingInvalidate - which will call the onDraw again, orrequestLayout - which will call the entire process again.
Simply Avoid calling
invalidate
unless you have to,And watch this: https://youtu.be/we6poP0kw6E
The horrible things that we do with bitmaps are
incredible, but there are ways to fix it!
Bitmap AbuseD
Bitmaps flow
Nothing here is accurate, but as an overview, it’s OK.
Image stored in the APK, or in media pack
Image downloaded from internet, kept in device storage
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
Bitmaps flow
Nothing here is accurate, but as an overview, it’s OK.
Image stored in the APK, or in media pack
Image downloaded from internet, kept in device storage
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
Uses PNG or JPEG formats, files are stored compressed
Optimized by AAPT
Uses one of 4 formats:ALPHA_8 1 byte per pixel, only alphaARGB_4444deprecatedARGB_8888 4 bytes per pixel, +alphaRGB_565 2 bytes per pixel, no alpha
Bitmaps flow - Downloading Image stored in the APK, or in media pack
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
Image downloaded from internet, kept in device storage
Which image format is downloaded?Which Quality?Which Size?Which network? WIFI? Metered?For how long are these images kept? Any size limits?
Image downloaded from internet, kept in device storage
Bitmaps flow - In Package
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
Which image format is stored?Which Quality?Which Size(s)?Which network? WIFI? Metered? → APK SIZEFor how long are these images kept? → ForeverAny size limits? → 100mb, but users will hate you!
Image stored in the APK, or in media pack
Image stored in the APK, or in media pack
Image downloaded from internet, kept in device storage
Bitmaps flow - In Memory
Image loaded into GPU
Content is drawn on screen
What Pixel-Format is used?Does the image need to re-scale?How to scale the image efficiently?Does the image has enough room?
Image loaded into memory (to the Heap)
Image stored in the APK, or in media pack
Image downloaded from internet, kept in device storage
Bitmaps flow - In GPU
Content is drawn on screen
Image loaded into memory (to the Heap)
How much time does it take to copy the image?Do you causing rendering to another buffer?
Image loaded into GPU
Why is it important?java.lang.OutofMemoryError: bitmap size exceeds VM budget.
Nexus 5x takes pictures at 3840x2160 resolution, at 24-bit.
That’s 33,177,600 bytes of data.
Well, good-luck!https://developer.android.com/training/displaying-bitmaps/index.html
Lucky us, We can optimize almost
everything,
And there’s A LOT of information out there
Bitmaps flow - Optimize download Image stored in the APK, or in media pack
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
- Work with your server folks to provide you with smaller files and better formats
- Use Cache (LRU Cache is great!)
- Use an image handling library, like Glide or Picasso
Image downloaded from internet, kept in device storage
Image downloaded from internet, kept in device storage
Bitmaps flow - Optimize APK size
Image loaded into memory (to the Heap)
Image loaded into GPU
Content is drawn on screen
- Choose the right format- Choose a good-enough quality- Add PNG/JPEG compression tools to your
build process- Remove unused resources
Image stored in the APK, or in media pack
Image stored in the APK, or in media pack
Image downloaded from internet, kept in device storage
Bitmaps flow - Optimize loading & memory
Image loaded into GPU
Content is drawn on screen
- Choose the right pixel format- Resize - Load on non-UI thread- Reuse memory- Use object pools if needed
Image loaded into memory (to the Heap)
Most Important Tip: Use a good image handling
library
and let’s check out *some* of the other things that we can do
Trick #1: Load images efficiently
Step 1: Don’t load the image at all, Only decode its size.BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Trick #1: Load images efficiently
Step 2: Don’t load the entire image, Sub-sample
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Trick #2: Cache and off-load
Use LRU-Cache to load bitmaps into memory and re-use them. This will help with list-views and similar, and help to limit the amount of memory used.
Move things off the UI thread. Use AsyncTask, but with caution, and handle concurrency, recycling, and lifecycle.https://developer.android.com/training/displaying-bitmaps/process-bitmap.html
https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Trick #3: Re-use bitmaps, in pools- Supported from 3.0 (API 11)- if possible, use the same memory.- Works only on mutable bitmaps
(BitmapFactory.Options)- Works only if image size is smaller than the buffer
(since KitKat, 4.4) Works only if image sizes are exactly the same
(before)
- Not easy workColt McAnlis: https://youtu.be/_ioFW3cyRV0 , and https://developer.android.com/training/displaying-bitmaps/manage-memory.html
Trick #4: Be smarter than AAPT with PNGs
AAPT optimizes PNG files - but uses only these 3 optimizations:
- Is the image Grayscale
- Is the image Transparent- Is the image cheap to index
All loss-less optimizations. Apply a lossy optimization tool.
Trick #4: Be smarter than AAPT with PNGs
You will probably want some more info.PNG:
How it works: https://medium.com/@duhroach/how-png-works-f1174e3cc7b7
Reducing size:https://medium.com/@duhroach/reducing-png-file-size-8473480d0476
AAPT:https://medium.com/@duhroach/smaller-pngs-and-android-s-aapt-tool-4ce38a24019d JPG:
How it works:https://medium.freecodecamp.com/how-jpg-works-a4dbd2316f35
Reducing size:https://medium.com/@duhroach/reducing-jpg-file-size-e5b27df3257c Both:
Watch this at home: https://youtu.be/r_LpCi6DQME (Google I/O 2016, Colt McAnlis)
From everything we learned, What you should remember
SummaryΣ
Theory
1.Systematic Performance Improvement2.Across-the-board impact3.Preventative / Premature optimizations4.The Getting-XML-to-Screen scheme
Animate
Measure Layout
Inflate
DrawExecut
e Sync
Profiling tools
Bitmap AbuseUse a good image handling libraryKnow the theoryPick the right formats
Try to reduce image sizeTry to reuse bitmaps if possible
Any Questions?
Thank you,Drive home safely
Thank you,Drive home safely