surface render_scale_surface(const surface &surf, int w, int h) { tblend_none_lock lock(surf); uint32_t t1, t2, t3, t4, t5; t1 = SDL_GetTicks(); SDL_Texture* src = SDL_CreateTextureFromSurface(renderer, surf); SDL_Texture* target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h); SDL_SetRenderTarget(renderer, target); t2 = SDL_GetTicks(); SDL_RenderCopy(renderer, src, NULL, NULL); t3 = SDL_GetTicks(); surface dst(create_neutral_surface(w, h)); { surface_lock dst_lock(dst); t4 = SDL_GetTicks(); SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_ARGB8888, dst->pixels, 4 * w); } t5 = SDL_GetTicks(); posix_print("create: %i, RenderCopy: %i, create2: %i, ReadPixels: %i\n", t2 - t1, t3 - t2, t4 - t3, t5 - t4); SDL_SetRenderTarget(renderer, NULL); SDL_DestroyTexture(target); SDL_DestroyTexture(src); return dst; }
上面是一个执行缩功能的函数,它同时用到了这三个函数,并打印它们花费的时间。打印部分见后面。
create: 31, RenderCopy: 4, create2: 6, ReadPixels: 16 (750x1334)==>(640x1136), soft: 65, gpu: 61
以以上这条记录为例说明各字段。它们时间单位都是毫秒
字段 | |
create | 会调用SDL_CreateTexture,该函数要执行SDL_UpdateTexture,它花费的时间基本花费在SDL_UpdateTexture |
RenderCopy | 花费在SDL_RenderCopy的时间 |
create2 | 创建目标图面,和opengl无关 |
ReadPixels | 花费在SDL_RenderReadPixels上时间 |
(750x1334)==>(640x1136) | 指示这次缩放的(源图像尺寸)==>(目标图像尺寸) |
soft | 缩放这个图像,scale_surface花费了的时间。相比opengl使用GPU,把它称为软缩放 |
gpu | 缩放这个图像,render_scale_surface花费了的时间,就是以上那几个时间之和 |
根据察看打印得出的几个结论。
- 执行SDL_UpdateTexture,Android要比iOS快。不过iOS慢也不算离谱,还能接受。
- 执行SDL_RenderCopy,不论Android还是iOS,都快。
- 执行SDL_RenderReadPixels,都不算快,尤其Android,只能用慢到离谱来形容。
- 对缩放小图像,像<100x100的,因为GPU有固定消耗在那里,相比软缩放没优势,但图像越大,它的优势越明显。
- 程序应该尽量避免SDL_RenderReadPixels!
iOS
create: 31, RenderCopy: 4, create2: 6, ReadPixels: 16 (750x1334)==>(640x1136), soft: 65, gup: 61 create: 35, RenderCopy: 0, create2: 6, ReadPixels: 17 (750x1334)==>(640x1136), soft: 394, gup: 62 create: 4, RenderCopy: 0, create2: 1, ReadPixels: 2 (224x224)==>(220x220), soft: 6, gup: 8 create: 5, RenderCopy: 0, create2: 6, ReadPixels: 17 (750x133)==>(640x1136), soft: 45, gup: 30 2create: 2, RenderCopy: 0, create2: 5, ReadPixels: 15 (100x100)==>(640x1008), soft: 38, gup: 23 create: 2, RenderCopy: 0, create2: 1, ReadPixels: 2 (224x224)==>(220x220), soft: 6, gup: 6 create: 5, RenderCopy: 0, create2: 6, ReadPixels: 17 (750x133)==>(640x1136), soft: 43, gup: 30 create: 31, RenderCopy: 0, create2: 4, ReadPixels: 16 (750x1131)==>(640x780), soft: 234, gup: 54 create: 5, RenderCopy: 0, create2: 1, ReadPixels: 4 (266x600)==>(266x346), soft: 13, gup: 11 create: 1, RenderCopy: 1, create2: 1, ReadPixels: 5 (100x100)==>(640x244), soft: 9, gup: 9 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 1 (24x24)==>(48x48), soft: 1, gup: 3 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 2 (80x80)==>(76x76), soft: 1, gup: 4 create: 37, RenderCopy: 1, create2: 6, ReadPixels: 29 (750x1334)==>(640x1136), soft: 143, gup: 77 create: 1, RenderCopy: 1, create2: 0, ReadPixels: 1 (126x1)==>(368x1), soft: 0, gup: 3 create: 0, RenderCopy: 0, create2: 0, ReadPixels: 2 (24x24)==>(48x48), soft: 1, gup: 2 create: 0, RenderCopy: 0, create2: 1, ReadPixels: 1 (126x1)==>(368x1), soft: 0, gup: 2 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 1 (24x24)==>(48x48), soft: 0, gup: 3 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 2 (126x1)==>(368x1), soft: 0, gup: 3 create: 0, RenderCopy: 1, create2: 0, ReadPixels: 1 (24x24)==>(48x48), soft: 1, gup: 2 create: 20, RenderCopy: 0, create2: 4, ReadPixels: 14 (640x1136)==>(531x944), soft: 217, gup: 41 create: 33, RenderCopy: 0, create2: 6, ReadPixels: 38 (750x1334)==>(640x1136), soft: 143, gup: 82 create: 28, RenderCopy: 0, create2: 6, ReadPixels: 17 (750x1334)==>(640x1136), soft: 144, gup: 55
Android
create: 15, RenderCopy: 3, create2: 14, ReadPixels: 189 (750x1334)==>(1080x1860), soft: 73, gup: 224 create: 19, RenderCopy: 0, create2: 22, ReadPixels: 212 (750x1334)==>(1080x1860), soft: 286, gup: 257 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 3 (88x88)==>(132x132), soft: 1, gup: 4 create: 0, RenderCopy: 1, create2: 0, ReadPixels: 12 (224x224)==>(336x336), soft: 7, gup: 13 create: 1, RenderCopy: 0, create2: 1, ReadPixels: 10 (224x224)==>(336x336), soft: 6, gup: 13 create: 1, RenderCopy: 0, create2: 1, ReadPixels: 7 (88x88)==>(126x126), soft: 2, gup: 9 create: 0, RenderCopy: 0, create2: 1, ReadPixels: 2 (88x88)==>(132x132), soft: 2, gup: 3 create: 17, RenderCopy: 0, create2: 29, ReadPixels: 169 (750x1334)==>(1080x1860), soft: 133, gup: 218 create: 0, RenderCopy: 0, create2: 0, ReadPixels: 2 (126x1)==>(648x1), soft: 0, gup: 2 create: 0, RenderCopy: 0, create2: 0, ReadPixels: 1 (24x24)==>(72x72), soft: 1, gup: 1 create: 0, RenderCopy: 0, create2: 0, ReadPixels: 0 (126x1)==>(648x1), soft: 0, gup: 0 create: 0, RenderCopy: 0, create2: 0, ReadPixels: 9 (24x24)==>(72x72), soft: 0, gup: 9 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 1 (126x1)==>(648x1), soft: 1, gup: 2 create: 1, RenderCopy: 0, create2: 0, ReadPixels: 1 (24x24)==>(72x72), soft: 0, gup: 2 create: 32, RenderCopy: 0, create2: 8, ReadPixels: 200 (1080x1860)==>(912x1572), soft: 199, gup: 243 create: 16, RenderCopy: 0, create2: 14, ReadPixels: 160 (750x1334)==>(1080x1860), soft: 140, gup: 194 create: 0, RenderCopy: 0, create2: 1, ReadPixels: 2 (88x88)==>(132x132), soft: 2, gup: 3 create: 1, RenderCopy: 1, create2: 0, ReadPixels: 21 (224x224)==>(336x336), soft: 7, gup: 23 create: 1, RenderCopy: 0, create2: 1, ReadPixels: 24 (224x224)==>(336x336), soft: 14, gup: 26