Reputation: 2550
I have implemented some screen capture code which can be viewed here
I'm using GetTimeInNanoseconds from this article
The function is called every 100ms. I've timed how many milliseconds it takes to execute part of the function, namely:
screenShotStart = GetTimeInNanoseconds();
CGImageRef image = CGWindowListCreateImage(rect, kCGWindowListOptionIncludingWindow|kCGWindowListOptionOnScreenBelowWindow, 0, kCGWindowImageDefault);
CGImageRelease(image);
screenShotStop = GetTimeInNanoseconds();
screenShotElapsed = screenShotStop - screenShotStart;
/* Current Delta Time */
screenShotElapsedF = ((float) screenShotElapsed)/1000000.0f;
/* Smooth Delta Time */
screenShotTime = 0.9f*screenShotTime + 0.1f*screenShotElapsedF;
The time measurements below show the results. I'm amazed at how slow it is and I'm wondering why. Is there an experienced Cocoa/Core Graphics OSX developer out there who can explain it to me? And possibly tell me how it should be fixed?
Smooth : Averaged delta time in milliseconds. Current: Latest, most current delta time in milliseconds.
Screenshot profile : 0.242122ms (smooth) | 2.421224ms (current)
Screenshot profile : 0.970972ms (smooth) | 7.530621ms (current)
Screenshot profile : 1.421471ms (smooth) | 5.475956ms (current)
Screenshot profile : 2.172416ms (smooth) | 8.930922ms (current)
Screenshot profile : 2.840984ms (smooth) | 8.858095ms (current)
Screenshot profile : 3.633541ms (smooth) | 10.766552ms (current)
Screenshot profile : 4.383532ms (smooth) | 11.133454ms (current)
Screenshot profile : 4.876161ms (smooth) | 9.309816ms (current)
Screenshot profile : 5.043479ms (smooth) | 6.549345ms (current)
Screenshot profile : 5.465037ms (smooth) | 9.259055ms (current)
Screenshot profile : 5.709429ms (smooth) | 7.908960ms (current)
Screenshot profile : 5.971393ms (smooth) | 8.329066ms (current)
Screenshot profile : 6.127830ms (smooth) | 7.535762ms (current)
Screenshot profile : 6.533566ms (smooth) | 10.185198ms (current)
Screenshot profile : 6.661420ms (smooth) | 7.812104ms (current)
Screenshot profile : 6.648901ms (smooth) | 6.536235ms (current)
Screenshot profile : 6.564128ms (smooth) | 5.801169ms (current)
Screenshot profile : 6.946393ms (smooth) | 10.386786ms (current)
Screenshot profile : 7.231448ms (smooth) | 9.796943ms (current)
Screenshot profile : 7.408576ms (smooth) | 9.002735ms (current)
Screenshot profile : 7.901027ms (smooth) | 12.333084ms (current)
Screenshot profile : 7.832513ms (smooth) | 7.215887ms (current)
Screenshot profile : 8.077989ms (smooth) | 10.287273ms (current)
Screenshot profile : 8.032367ms (smooth) | 7.621767ms (current)
Screenshot profile : 8.203003ms (smooth) | 9.738728ms (current)
Screenshot profile : 8.525432ms (smooth) | 11.427292ms (current)
Screenshot profile : 8.672282ms (smooth) | 9.993936ms (current)
Screenshot profile : 8.818027ms (smooth) | 10.129727ms (current)
Screenshot profile : 8.968993ms (smooth) | 10.327700ms (current)
Screenshot profile : 9.755873ms (smooth) | 16.837791ms (current)
Screenshot profile : 10.098254ms (smooth) | 13.179691ms (current)
Screenshot profile : 10.007771ms (smooth) | 9.193431ms (current)
Screenshot profile : 9.730317ms (smooth) | 7.233225ms (current)
Screenshot profile : 9.923961ms (smooth) | 11.666759ms (current)
Screenshot profile : 10.171064ms (smooth) | 12.395005ms (current)
Screenshot profile : 10.436771ms (smooth) | 12.828136ms (current)
Screenshot profile : 10.752213ms (smooth) | 13.591184ms (current)
Screenshot profile : 10.677605ms (smooth) | 10.006127ms (current)
Screenshot profile : 10.696332ms (smooth) | 10.864876ms (current)
Screenshot profile : 10.836714ms (smooth) | 12.100148ms (current)
Screenshot profile : 10.384002ms (smooth) | 6.309598ms (current)
Screenshot profile : 10.512785ms (smooth) | 11.671834ms (current)
Screenshot profile : 10.594671ms (smooth) | 11.331652ms (current)
Screenshot profile : 11.022106ms (smooth) | 14.869019ms (current)
Screenshot profile : 11.056261ms (smooth) | 11.363660ms (current)
Screenshot profile : 13.173499ms (smooth) | 32.228638ms (current)
Screenshot profile : 12.901811ms (smooth) | 10.456617ms (current)
Screenshot profile : 12.511471ms (smooth) | 8.998418ms (current)
Screenshot profile : 60.684856ms (smooth) | 494.245331ms (current)
Screenshot profile : 111.287766ms (smooth) | 566.713928ms (current)
Screenshot profile : 118.577721ms (smooth) | 184.187302ms (current)
Screenshot profile : 126.834038ms (smooth) | 201.140869ms (current)
Screenshot profile : 233.015961ms (smooth) | 1188.653198ms (current)
Screenshot profile : 293.934875ms (smooth) | 842.205017ms (current)
Screenshot profile : 282.354095ms (smooth) | 178.127045ms (current)
Screenshot profile : 297.604187ms (smooth) | 434.854919ms (current)
Screenshot profile : 300.026642ms (smooth) | 321.828796ms (current)
Screenshot profile : 298.798157ms (smooth) | 287.741913ms (current)
Screenshot profile : 307.527008ms (smooth) | 386.086761ms (current)
Screenshot profile : 294.194061ms (smooth) | 174.197708ms (current)
Screenshot profile : 286.482117ms (smooth) | 217.074570ms (current)
Screenshot profile : 277.750549ms (smooth) | 199.166428ms (current)
Screenshot profile : 271.708069ms (smooth) | 217.325623ms (current)
Screenshot profile : 245.042664ms (smooth) | 5.053972ms (current)
Screenshot profile : 221.163406ms (smooth) | 6.250124ms (current)
Screenshot profile : 208.384155ms (smooth) | 93.371033ms (current)
Screenshot profile : 207.589813ms (smooth) | 200.440811ms (current)
Screenshot profile : 223.785492ms (smooth) | 369.546631ms (current)
Screenshot profile : 221.480774ms (smooth) | 200.738419ms (current)
Screenshot profile : 230.090012ms (smooth) | 307.573181ms (current)
Screenshot profile : 254.507935ms (smooth) | 474.269287ms (current)
Screenshot profile : 283.468353ms (smooth) | 544.112122ms (current)
Screenshot profile : 285.878754ms (smooth) | 307.572601ms (current)
Screenshot profile : 300.691986ms (smooth) | 434.011261ms (current)
Screenshot profile : 301.684082ms (smooth) | 310.613220ms (current)
Screenshot profile : 272.227661ms (smooth) | 7.119990ms (current)
Screenshot profile : 246.098129ms (smooth) | 10.932533ms (current)
Screenshot profile : 281.438873ms (smooth) | 599.505737ms (current)
Screenshot profile : 291.068756ms (smooth) | 377.737671ms (current)
Screenshot profile : 262.161163ms (smooth) | 1.992869ms (current)
Screenshot profile : 236.614410ms (smooth) | 6.693653ms (current)
Screenshot profile : 243.460983ms (smooth) | 305.080231ms (current)
Screenshot profile : 240.609909ms (smooth) | 214.950256ms (current)
Screenshot profile : 266.002258ms (smooth) | 494.533417ms (current)
Screenshot profile : 276.689453ms (smooth) | 372.874268ms (current)
Screenshot profile : 278.445312ms (smooth) | 294.248047ms (current)
Screenshot profile : 268.703094ms (smooth) | 181.023148ms (current)
Screenshot profile : 242.027161ms (smooth) | 1.943824ms (current)
Screenshot profile : 218.654465ms (smooth) | 8.300381ms (current)
Screenshot profile : 211.597733ms (smooth) | 148.087173ms (current)
Screenshot profile : 191.547241ms (smooth) | 11.092879ms (current)
Screenshot profile : 181.838150ms (smooth) | 94.456367ms (current)
Screenshot profile : 164.788208ms (smooth) | 11.338873ms (current)
Screenshot profile : 151.486023ms (smooth) | 31.766415ms (current)
Screenshot profile : 159.313858ms (smooth) | 229.764404ms (current)
Screenshot profile : 220.536041ms (smooth) | 771.535706ms (current)
Screenshot profile : 198.653290ms (smooth) | 1.708489ms (current)
Screenshot profile : 180.131805ms (smooth) | 13.438550ms (current)
Screenshot profile : 307.717621ms (smooth) | 1455.989990ms (current)
Screenshot profile : 477.049713ms (smooth) | 2001.038452ms (current)
Screenshot profile : 434.271057ms (smooth) | 49.263458ms (current)
Screenshot profile : 392.732391ms (smooth) | 18.884504ms (current)
Screenshot profile : 396.588654ms (smooth) | 431.295227ms (current)
Screenshot profile : 410.529907ms (smooth) | 536.001404ms (current)
Screenshot profile : 420.648346ms (smooth) | 511.714478ms (current)
Screenshot profile : 421.814819ms (smooth) | 432.313385ms (current)
Screenshot profile : 398.381866ms (smooth) | 187.485214ms (current)
Screenshot profile : 359.150482ms (smooth) | 6.068115ms (current)
Screenshot profile : 395.477631ms (smooth) | 722.422058ms (current)
Screenshot profile : 356.116364ms (smooth) | 1.864945ms (current)
Screenshot profile : 321.195648ms (smooth) | 6.909203ms (current)
Screenshot profile : 313.617981ms (smooth) | 245.419037ms (current)
Screenshot profile : 291.778534ms (smooth) | 95.223839ms (current)
Screenshot profile : 275.232025ms (smooth) | 126.313583ms (current)
Screenshot profile : 299.395660ms (smooth) | 516.868530ms (current)
Screenshot profile : 289.037140ms (smooth) | 195.810669ms (current)
Screenshot profile : 472.922241ms (smooth) | 2127.887939ms (current)
Screenshot profile : 425.895874ms (smooth) | 2.658584ms (current)
Screenshot profile : 404.271606ms (smooth) | 209.653427ms (current)
EDIT: Download profile results
Upvotes: 0
Views: 2203
Reputation: 90641
The CGWindow API is not intended for high-frame-rate capturing. For one thing, it's not capturing the currently-displayed screen image. It is generalized to potentially capture various portions of the content. It can include or exclude windows, desktop elements, shadows, etc. That means it's composing a new image for every call. And, yes, that means that it has to lock the backing buffer of all of the windows.
Apple provides a pair of technical Q&A articles about capturing the screen contents. One is for capturing single images, where they recommend the use of CGDisplayCreateImage()
. The other is for capturing a movie of screen activity over time, where they recommend the use of AV Foundation.
Technical Q&A QA1741: How to take an image snapshot of the screen on Mac OS X Lion
Technical Q&A QA1740: How to capture screen activity to a movie file using AV Foundation on Mac OS X Lion
Upvotes: 3