buffer_format_util.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/buffer_format_util.h"
  5. #include "base/check_op.h"
  6. #include "base/notreached.h"
  7. #include "base/numerics/safe_math.h"
  8. #include "ui/gfx/switches.h"
  9. namespace gfx {
  10. namespace {
  11. const BufferFormat kBufferFormats[] = {
  12. BufferFormat::R_8, BufferFormat::R_16,
  13. BufferFormat::RG_88, BufferFormat::RG_1616,
  14. BufferFormat::BGR_565, BufferFormat::RGBA_4444,
  15. BufferFormat::RGBX_8888, BufferFormat::RGBA_8888,
  16. BufferFormat::BGRX_8888, BufferFormat::BGRA_1010102,
  17. BufferFormat::RGBA_1010102, BufferFormat::BGRA_8888,
  18. BufferFormat::RGBA_F16, BufferFormat::YUV_420_BIPLANAR,
  19. BufferFormat::YVU_420, BufferFormat::P010};
  20. static_assert(std::size(kBufferFormats) ==
  21. (static_cast<int>(BufferFormat::LAST) + 1),
  22. "BufferFormat::LAST must be last value of kBufferFormats");
  23. } // namespace
  24. std::vector<BufferFormat> GetBufferFormatsForTesting() {
  25. return std::vector<BufferFormat>(kBufferFormats,
  26. kBufferFormats + std::size(kBufferFormats));
  27. }
  28. size_t AlphaBitsForBufferFormat(BufferFormat format) {
  29. switch (format) {
  30. case BufferFormat::RGBA_4444:
  31. return 4;
  32. case BufferFormat::RGBA_8888:
  33. return 8;
  34. case BufferFormat::BGRA_1010102:
  35. case BufferFormat::RGBA_1010102:
  36. return 2;
  37. case BufferFormat::BGRA_8888:
  38. return 8;
  39. case BufferFormat::RGBA_F16:
  40. return 16;
  41. case BufferFormat::R_8:
  42. case BufferFormat::R_16:
  43. case BufferFormat::RG_88:
  44. case BufferFormat::RG_1616:
  45. case BufferFormat::BGR_565:
  46. case BufferFormat::RGBX_8888:
  47. case BufferFormat::BGRX_8888:
  48. case BufferFormat::YVU_420:
  49. case BufferFormat::YUV_420_BIPLANAR:
  50. case BufferFormat::P010:
  51. return 0;
  52. }
  53. NOTREACHED();
  54. return 0;
  55. }
  56. size_t NumberOfPlanesForLinearBufferFormat(BufferFormat format) {
  57. switch (format) {
  58. case BufferFormat::R_8:
  59. case BufferFormat::R_16:
  60. case BufferFormat::RG_88:
  61. case BufferFormat::RG_1616:
  62. case BufferFormat::BGR_565:
  63. case BufferFormat::RGBA_4444:
  64. case BufferFormat::RGBX_8888:
  65. case BufferFormat::RGBA_8888:
  66. case BufferFormat::BGRX_8888:
  67. case BufferFormat::BGRA_1010102:
  68. case BufferFormat::RGBA_1010102:
  69. case BufferFormat::BGRA_8888:
  70. case BufferFormat::RGBA_F16:
  71. return 1;
  72. case BufferFormat::YUV_420_BIPLANAR:
  73. case BufferFormat::P010:
  74. return 2;
  75. case BufferFormat::YVU_420:
  76. return 3;
  77. }
  78. NOTREACHED();
  79. return 0;
  80. }
  81. size_t SubsamplingFactorForBufferFormat(BufferFormat format, size_t plane) {
  82. switch (format) {
  83. case BufferFormat::R_8:
  84. case BufferFormat::R_16:
  85. case BufferFormat::RG_88:
  86. case BufferFormat::RG_1616:
  87. case BufferFormat::BGR_565:
  88. case BufferFormat::RGBA_4444:
  89. case BufferFormat::RGBX_8888:
  90. case BufferFormat::RGBA_8888:
  91. case BufferFormat::BGRX_8888:
  92. case BufferFormat::BGRA_1010102:
  93. case BufferFormat::RGBA_1010102:
  94. case BufferFormat::BGRA_8888:
  95. case BufferFormat::RGBA_F16:
  96. return 1;
  97. case BufferFormat::YVU_420: {
  98. constexpr size_t factor[] = {1, 2, 2};
  99. DCHECK_LT(plane, std::size(factor));
  100. return factor[plane];
  101. }
  102. case BufferFormat::YUV_420_BIPLANAR:
  103. case BufferFormat::P010: {
  104. constexpr size_t factor[] = {1, 2};
  105. DCHECK_LT(plane, std::size(factor));
  106. return factor[plane];
  107. }
  108. }
  109. NOTREACHED();
  110. return 0;
  111. }
  112. size_t PlaneWidthForBufferFormat(size_t width,
  113. BufferFormat format,
  114. size_t plane) {
  115. const size_t subsample = SubsamplingFactorForBufferFormat(format, plane);
  116. return (width + subsample - 1) / subsample;
  117. }
  118. size_t PlaneHeightForBufferFormat(size_t height,
  119. BufferFormat format,
  120. size_t plane) {
  121. const size_t subsample = SubsamplingFactorForBufferFormat(format, plane);
  122. return (height + subsample - 1) / subsample;
  123. }
  124. size_t BytesPerPixelForBufferFormat(BufferFormat format, size_t plane) {
  125. switch (format) {
  126. case BufferFormat::R_8:
  127. return 1;
  128. case BufferFormat::R_16:
  129. case BufferFormat::RG_88:
  130. case BufferFormat::BGR_565:
  131. case BufferFormat::RGBA_4444:
  132. return 2;
  133. case BufferFormat::RG_1616:
  134. case BufferFormat::BGRX_8888:
  135. case BufferFormat::BGRA_1010102:
  136. case BufferFormat::RGBA_1010102:
  137. case BufferFormat::RGBX_8888:
  138. case BufferFormat::RGBA_8888:
  139. case BufferFormat::BGRA_8888:
  140. return 4;
  141. case BufferFormat::RGBA_F16:
  142. return 8;
  143. case BufferFormat::YVU_420:
  144. return 1;
  145. case BufferFormat::YUV_420_BIPLANAR:
  146. return SubsamplingFactorForBufferFormat(format, plane);
  147. case BufferFormat::P010:
  148. return 2 * SubsamplingFactorForBufferFormat(format, plane);
  149. }
  150. NOTREACHED();
  151. return 0;
  152. }
  153. size_t RowByteAlignmentForBufferFormat(BufferFormat format, size_t plane) {
  154. switch (format) {
  155. case BufferFormat::R_8:
  156. case BufferFormat::R_16:
  157. case BufferFormat::RG_88:
  158. case BufferFormat::BGR_565:
  159. case BufferFormat::RGBA_4444:
  160. case BufferFormat::RG_1616:
  161. case BufferFormat::BGRX_8888:
  162. case BufferFormat::BGRA_1010102:
  163. case BufferFormat::RGBA_1010102:
  164. case BufferFormat::RGBX_8888:
  165. case BufferFormat::RGBA_8888:
  166. case BufferFormat::BGRA_8888:
  167. return 4;
  168. case BufferFormat::RGBA_F16:
  169. return 8;
  170. case BufferFormat::YVU_420:
  171. return 1;
  172. case BufferFormat::YUV_420_BIPLANAR:
  173. case BufferFormat::P010:
  174. return BytesPerPixelForBufferFormat(format, plane);
  175. }
  176. NOTREACHED();
  177. return 0;
  178. }
  179. size_t RowSizeForBufferFormat(size_t width, BufferFormat format, size_t plane) {
  180. size_t row_size = 0;
  181. bool valid = RowSizeForBufferFormatChecked(width, format, plane, &row_size);
  182. DCHECK(valid);
  183. return row_size;
  184. }
  185. bool RowSizeForBufferFormatChecked(size_t width,
  186. BufferFormat format,
  187. size_t plane,
  188. size_t* size_in_bytes) {
  189. base::CheckedNumeric<size_t> checked_size =
  190. PlaneWidthForBufferFormat(width, format, plane);
  191. checked_size *= BytesPerPixelForBufferFormat(format, plane);
  192. const size_t alignment = RowByteAlignmentForBufferFormat(format, plane);
  193. checked_size = (checked_size + alignment - 1) & ~(alignment - 1);
  194. if (!checked_size.IsValid())
  195. return false;
  196. *size_in_bytes = checked_size.ValueOrDie();
  197. return true;
  198. }
  199. size_t PlaneSizeForBufferFormat(const Size& size,
  200. BufferFormat format,
  201. size_t plane) {
  202. size_t plane_size = 0;
  203. bool valid =
  204. PlaneSizeForBufferFormatChecked(size, format, plane, &plane_size);
  205. DCHECK(valid);
  206. return plane_size;
  207. }
  208. bool PlaneSizeForBufferFormatChecked(const Size& size,
  209. BufferFormat format,
  210. size_t plane,
  211. size_t* size_in_bytes) {
  212. size_t row_size = 0;
  213. if (!RowSizeForBufferFormatChecked(size.width(), format, plane, &row_size))
  214. return false;
  215. base::CheckedNumeric<size_t> checked_plane_size = row_size;
  216. checked_plane_size *=
  217. PlaneHeightForBufferFormat(size.height(), format, plane);
  218. if (!checked_plane_size.IsValid())
  219. return false;
  220. *size_in_bytes = checked_plane_size.ValueOrDie();
  221. return true;
  222. }
  223. size_t BufferSizeForBufferFormat(const Size& size, BufferFormat format) {
  224. size_t buffer_size = 0;
  225. bool valid = BufferSizeForBufferFormatChecked(size, format, &buffer_size);
  226. DCHECK(valid);
  227. return buffer_size;
  228. }
  229. bool BufferSizeForBufferFormatChecked(const Size& size,
  230. BufferFormat format,
  231. size_t* size_in_bytes) {
  232. base::CheckedNumeric<size_t> checked_size = 0;
  233. size_t num_planes = NumberOfPlanesForLinearBufferFormat(format);
  234. for (size_t i = 0; i < num_planes; ++i) {
  235. size_t plane_size = 0;
  236. if (!PlaneSizeForBufferFormatChecked(size, format, i, &plane_size))
  237. return false;
  238. checked_size += plane_size;
  239. if (!checked_size.IsValid())
  240. return false;
  241. }
  242. *size_in_bytes = checked_size.ValueOrDie();
  243. return true;
  244. }
  245. size_t BufferOffsetForBufferFormat(const Size& size,
  246. BufferFormat format,
  247. size_t plane) {
  248. DCHECK_LT(plane, gfx::NumberOfPlanesForLinearBufferFormat(format));
  249. switch (format) {
  250. case BufferFormat::R_8:
  251. case BufferFormat::R_16:
  252. case BufferFormat::RG_88:
  253. case BufferFormat::RG_1616:
  254. case BufferFormat::BGR_565:
  255. case BufferFormat::RGBA_4444:
  256. case BufferFormat::RGBX_8888:
  257. case BufferFormat::RGBA_8888:
  258. case BufferFormat::BGRX_8888:
  259. case BufferFormat::BGRA_1010102:
  260. case BufferFormat::RGBA_1010102:
  261. case BufferFormat::BGRA_8888:
  262. case BufferFormat::RGBA_F16:
  263. return 0;
  264. case BufferFormat::YVU_420:
  265. case BufferFormat::YUV_420_BIPLANAR:
  266. case BufferFormat::P010: {
  267. size_t offset = 0;
  268. for (size_t i = 0; i < plane; i++) {
  269. offset += PlaneSizeForBufferFormat(size, format, i);
  270. }
  271. return offset;
  272. }
  273. }
  274. NOTREACHED();
  275. return 0;
  276. }
  277. const char* BufferFormatToString(BufferFormat format) {
  278. switch (format) {
  279. case BufferFormat::R_8:
  280. return "R_8";
  281. case BufferFormat::R_16:
  282. return "R_16";
  283. case BufferFormat::RG_88:
  284. return "RG_88";
  285. case BufferFormat::RG_1616:
  286. return "RG_1616";
  287. case BufferFormat::BGR_565:
  288. return "BGR_565";
  289. case BufferFormat::RGBA_4444:
  290. return "RGBA_4444";
  291. case BufferFormat::RGBX_8888:
  292. return "RGBX_8888";
  293. case BufferFormat::RGBA_8888:
  294. return "RGBA_8888";
  295. case BufferFormat::BGRX_8888:
  296. return "BGRX_8888";
  297. case BufferFormat::BGRA_1010102:
  298. return "BGRA_1010102";
  299. case BufferFormat::RGBA_1010102:
  300. return "RGBA_1010102";
  301. case BufferFormat::BGRA_8888:
  302. return "BGRA_8888";
  303. case BufferFormat::RGBA_F16:
  304. return "RGBA_F16";
  305. case BufferFormat::YVU_420:
  306. return "YVU_420";
  307. case BufferFormat::YUV_420_BIPLANAR:
  308. return "YUV_420_BIPLANAR";
  309. case BufferFormat::P010:
  310. return "P010";
  311. }
  312. NOTREACHED()
  313. << "Invalid BufferFormat: "
  314. << static_cast<typename std::underlying_type<BufferFormat>::type>(format);
  315. return "Invalid Format";
  316. }
  317. const char* BufferPlaneToString(BufferPlane format) {
  318. switch (format) {
  319. case BufferPlane::DEFAULT:
  320. return "DEFAULT";
  321. case BufferPlane::Y:
  322. return "Y";
  323. case BufferPlane::UV:
  324. return "UV";
  325. case BufferPlane::U:
  326. return "U";
  327. case BufferPlane::V:
  328. return "V";
  329. }
  330. NOTREACHED() << "Invalid BufferPlane: "
  331. << static_cast<typename std::underlying_type<BufferPlane>::type>(
  332. format);
  333. return "Invalid Plane";
  334. }
  335. bool IsOddHeightMultiPlanarBuffersAllowed() {
  336. return base::FeatureList::IsEnabled(features::kOddHeightMultiPlanarBuffers);
  337. }
  338. bool IsOddWidthMultiPlanarBuffersAllowed() {
  339. return base::FeatureList::IsEnabled(features::kOddWidthMultiPlanarBuffers);
  340. }
  341. } // namespace gfx