gl_texture_mailbox_unittest.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // Copyright 2014 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 <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <GLES2/gl2extchromium.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include "build/build_config.h"
  10. #include "gpu/command_buffer/client/gles2_implementation.h"
  11. #include "gpu/command_buffer/client/gles2_lib.h"
  12. #include "gpu/command_buffer/common/mailbox.h"
  13. #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
  14. #include "gpu/command_buffer/service/mailbox_manager.h"
  15. #include "gpu/command_buffer/tests/gl_manager.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "ui/gl/gl_share_group.h"
  19. namespace gpu {
  20. namespace {
  21. GLcolorSpace GetGLColorSpace() {
  22. static gfx::ColorSpace color_space = gfx::ColorSpace::CreateSRGB();
  23. return color_space.AsGLColorSpace();
  24. }
  25. uint32_t ReadTexel(GLuint id, GLint x, GLint y) {
  26. GLint old_fbo = 0;
  27. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
  28. GLuint fbo;
  29. glGenFramebuffers(1, &fbo);
  30. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  31. glFramebufferTexture2D(GL_FRAMEBUFFER,
  32. GL_COLOR_ATTACHMENT0,
  33. GL_TEXTURE_2D,
  34. id,
  35. 0);
  36. // Some drivers (NVidia/SGX) require texture settings to be a certain way or
  37. // they won't report FRAMEBUFFER_COMPLETE.
  38. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  39. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  40. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  42. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  43. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  44. uint32_t texel = 0;
  45. glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &texel);
  46. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  47. glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
  48. glDeleteFramebuffers(1, &fbo);
  49. return texel;
  50. }
  51. }
  52. class GLTextureMailboxTest : public testing::Test {
  53. protected:
  54. void SetUpContexts() {
  55. gl1_.Initialize(GLManager::Options());
  56. GLManager::Options options;
  57. options.share_mailbox_manager = &gl1_;
  58. gl2_.Initialize(options);
  59. }
  60. void TearDown() override {
  61. gl1_.Destroy();
  62. gl2_.Destroy();
  63. }
  64. // The second GL context takes and consumes a mailbox from the first GL
  65. // context. Assumes that |gl1_| is current.
  66. Mailbox TakeAndConsumeMailbox() {
  67. glResizeCHROMIUM(10, 10, 1, GetGLColorSpace(), true);
  68. glClearColor(0, 1, 1, 1);
  69. glClear(GL_COLOR_BUFFER_BIT);
  70. ::gles2::GetGLContext()->SwapBuffers(1);
  71. Mailbox mailbox = Mailbox::Generate();
  72. gl1_.decoder()->TakeFrontBuffer(mailbox);
  73. gl2_.MakeCurrent();
  74. GLuint tex = glCreateAndConsumeTextureCHROMIUM(mailbox.name);
  75. glBindTexture(GL_TEXTURE_2D, tex);
  76. glDeleteTextures(1, &tex);
  77. glFlush();
  78. gl1_.MakeCurrent();
  79. return mailbox;
  80. }
  81. void AllocateTextureBackedFramebuffer(GLuint* tex, GLuint* fbo) {
  82. glGenTextures(1, tex);
  83. glBindTexture(GL_TEXTURE_2D, *tex);
  84. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  85. nullptr);
  86. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  87. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  90. glGenFramebuffers(1, fbo);
  91. glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
  92. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  93. *tex, 0);
  94. ASSERT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  95. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  96. }
  97. GLManager gl1_;
  98. GLManager gl2_;
  99. };
  100. TEST_F(GLTextureMailboxTest, ProduceAndConsumeTexture) {
  101. SetUpContexts();
  102. gl1_.MakeCurrent();
  103. GLuint tex1;
  104. glGenTextures(1, &tex1);
  105. glBindTexture(GL_TEXTURE_2D, tex1);
  106. uint32_t source_pixel = 0xFF0000FF;
  107. glTexImage2D(GL_TEXTURE_2D,
  108. 0,
  109. GL_RGBA,
  110. 1, 1,
  111. 0,
  112. GL_RGBA,
  113. GL_UNSIGNED_BYTE,
  114. &source_pixel);
  115. GLbyte mailbox1[GL_MAILBOX_SIZE_CHROMIUM];
  116. glProduceTextureDirectCHROMIUM(tex1, mailbox1);
  117. glFlush();
  118. gl2_.MakeCurrent();
  119. GLuint tex2 = glCreateAndConsumeTextureCHROMIUM(mailbox1);
  120. glBindTexture(GL_TEXTURE_2D, tex2);
  121. EXPECT_EQ(source_pixel, ReadTexel(tex2, 0, 0));
  122. GLbyte mailbox2[GL_MAILBOX_SIZE_CHROMIUM];
  123. glProduceTextureDirectCHROMIUM(tex2, mailbox2);
  124. glFlush();
  125. gl1_.MakeCurrent();
  126. tex1 = glCreateAndConsumeTextureCHROMIUM(mailbox2);
  127. glBindTexture(GL_TEXTURE_2D, tex1);
  128. EXPECT_EQ(source_pixel, ReadTexel(tex1, 0, 0));
  129. }
  130. TEST_F(GLTextureMailboxTest, ProduceAndConsumeTextureRGB) {
  131. SetUpContexts();
  132. gl1_.MakeCurrent();
  133. GLuint tex1;
  134. glGenTextures(1, &tex1);
  135. glBindTexture(GL_TEXTURE_2D, tex1);
  136. uint32_t source_pixel = 0xFF000000;
  137. glTexImage2D(GL_TEXTURE_2D,
  138. 0,
  139. GL_RGB,
  140. 1, 1,
  141. 0,
  142. GL_RGB,
  143. GL_UNSIGNED_BYTE,
  144. &source_pixel);
  145. GLbyte mailbox1[GL_MAILBOX_SIZE_CHROMIUM];
  146. glProduceTextureDirectCHROMIUM(tex1, mailbox1);
  147. glFlush();
  148. gl2_.MakeCurrent();
  149. GLuint tex2 = glCreateAndConsumeTextureCHROMIUM(mailbox1);
  150. glBindTexture(GL_TEXTURE_2D, tex2);
  151. EXPECT_EQ(source_pixel, ReadTexel(tex2, 0, 0));
  152. GLbyte mailbox2[GL_MAILBOX_SIZE_CHROMIUM];
  153. glProduceTextureDirectCHROMIUM(tex2, mailbox2);
  154. glFlush();
  155. gl1_.MakeCurrent();
  156. tex1 = glCreateAndConsumeTextureCHROMIUM(mailbox2);
  157. glBindTexture(GL_TEXTURE_2D, tex1);
  158. EXPECT_EQ(source_pixel, ReadTexel(tex1, 0, 0));
  159. }
  160. TEST_F(GLTextureMailboxTest, SharedTextures) {
  161. SetUpContexts();
  162. gl1_.MakeCurrent();
  163. GLuint tex1;
  164. glGenTextures(1, &tex1);
  165. glBindTexture(GL_TEXTURE_2D, tex1);
  166. uint32_t source_pixel = 0xFF0000FF;
  167. glTexImage2D(GL_TEXTURE_2D,
  168. 0,
  169. GL_RGBA,
  170. 1, 1,
  171. 0,
  172. GL_RGBA,
  173. GL_UNSIGNED_BYTE,
  174. &source_pixel);
  175. GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
  176. glProduceTextureDirectCHROMIUM(tex1, mailbox);
  177. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  178. glFlush();
  179. gl2_.MakeCurrent();
  180. GLuint tex2 = glCreateAndConsumeTextureCHROMIUM(mailbox);
  181. glBindTexture(GL_TEXTURE_2D, tex2);
  182. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  183. // Change texture in context 2.
  184. source_pixel = 0xFF00FF00;
  185. glTexSubImage2D(GL_TEXTURE_2D,
  186. 0,
  187. 0, 0,
  188. 1, 1,
  189. GL_RGBA,
  190. GL_UNSIGNED_BYTE,
  191. &source_pixel);
  192. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  193. glFlush();
  194. // Check it in context 1.
  195. gl1_.MakeCurrent();
  196. EXPECT_EQ(source_pixel, ReadTexel(tex1, 0, 0));
  197. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  198. // Change parameters (note: ReadTexel will reset those).
  199. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  202. GL_LINEAR_MIPMAP_NEAREST);
  203. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  204. glFlush();
  205. // Check in context 2.
  206. gl2_.MakeCurrent();
  207. GLint parameter = 0;
  208. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &parameter);
  209. EXPECT_EQ(GL_REPEAT, parameter);
  210. parameter = 0;
  211. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &parameter);
  212. EXPECT_EQ(GL_LINEAR, parameter);
  213. parameter = 0;
  214. glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &parameter);
  215. EXPECT_EQ(GL_LINEAR_MIPMAP_NEAREST, parameter);
  216. // Delete texture in context 1.
  217. gl1_.MakeCurrent();
  218. glDeleteTextures(1, &tex1);
  219. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  220. // Check texture still exists in context 2.
  221. gl2_.MakeCurrent();
  222. EXPECT_EQ(source_pixel, ReadTexel(tex2, 0, 0));
  223. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  224. // The mailbox should still exist too.
  225. GLuint tex3 = glCreateAndConsumeTextureCHROMIUM(mailbox);
  226. glBindTexture(GL_TEXTURE_2D, tex3);
  227. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  228. // Delete both textures.
  229. glDeleteTextures(1, &tex2);
  230. glDeleteTextures(1, &tex3);
  231. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  232. // Mailbox should be gone now.
  233. tex2 = glCreateAndConsumeTextureCHROMIUM(mailbox);
  234. glBindTexture(GL_TEXTURE_2D, tex2);
  235. EXPECT_EQ(static_cast<GLenum>(GL_INVALID_OPERATION), glGetError());
  236. glDeleteTextures(1, &tex2);
  237. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  238. }
  239. TEST_F(GLTextureMailboxTest, OrderingBarrierImpliesFlush) {
  240. SetUpContexts();
  241. gl2_.MakeCurrent();
  242. // If the current platform requires the use of virtualized GL contexts
  243. // for correctness, this harness does not respect that flag, so skip
  244. // this test.
  245. if (gl2_.workarounds().use_virtualized_gl_contexts) {
  246. SUCCEED() << "Platform requires virtualized GL contexts; skipping.";
  247. return;
  248. }
  249. GLuint fbo2 = 0;
  250. glGenFramebuffers(1, &fbo2);
  251. glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
  252. gl1_.MakeCurrent();
  253. GLuint tex1 = 0;
  254. GLuint fbo1 = 0;
  255. AllocateTextureBackedFramebuffer(&tex1, &fbo1);
  256. // Many times:
  257. // - Set the texture to one color in context 1
  258. // - Mailbox the texture to context 2
  259. // - Set the texture to another color in context 2
  260. // - Mailbox the texture to context 1
  261. // - Read back the framebuffer in context 1
  262. // - Assert it's the color set by context 2
  263. GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
  264. glProduceTextureDirectCHROMIUM(tex1, mailbox);
  265. for (int i = 0; i < 1000; ++i) {
  266. // Assume context 1 is current.
  267. // Clear to red
  268. glClearColor(1.0, 0.0, 0.0, 1.0);
  269. glClear(GL_COLOR_BUFFER_BIT);
  270. // Enforce ordering with respect to context 2.
  271. gl1_.gles2_implementation()->OrderingBarrierCHROMIUM();
  272. // Consume texture in context 2.
  273. gl2_.MakeCurrent();
  274. GLuint tex2 = glCreateAndConsumeTextureCHROMIUM(mailbox);
  275. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  276. // Set up framebuffer in context 2 (strictly, not necessary, could
  277. // do this just once).
  278. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  279. tex2, 0);
  280. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  281. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  282. // Clear to green.
  283. glClearColor(0.0, 1.0, 0.0, 1.0);
  284. glClear(GL_COLOR_BUFFER_BIT);
  285. glDeleteTextures(1, &tex2);
  286. // Enforce ordering with respect to context 2.
  287. gl2_.gles2_implementation()->OrderingBarrierCHROMIUM();
  288. // Consume texture in context 1.
  289. gl1_.MakeCurrent();
  290. // Read back framebuffer.
  291. GLubyte pixel[4];
  292. glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel[0]);
  293. EXPECT_EQ(0, pixel[0]);
  294. EXPECT_EQ(255, pixel[1]);
  295. EXPECT_EQ(0, pixel[2]);
  296. EXPECT_EQ(255, pixel[3]);
  297. }
  298. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  299. glDeleteFramebuffers(1, &fbo1);
  300. glBindTexture(GL_TEXTURE_2D, 0);
  301. glDeleteTextures(1, &tex1);
  302. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  303. gl2_.MakeCurrent();
  304. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  305. glDeleteFramebuffers(1, &fbo2);
  306. glBindTexture(GL_TEXTURE_2D, 0);
  307. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  308. }
  309. TEST_F(GLTextureMailboxTest, TakeFrontBuffer) {
  310. SetUpContexts();
  311. gl2_.MakeCurrent();
  312. glResizeCHROMIUM(10, 10, 1, GetGLColorSpace(), true);
  313. glClearColor(0, 1, 1, 1);
  314. glClear(GL_COLOR_BUFFER_BIT);
  315. ::gles2::GetGLContext()->SwapBuffers(1);
  316. Mailbox mailbox = Mailbox::Generate();
  317. gl2_.decoder()->TakeFrontBuffer(mailbox);
  318. gl1_.MakeCurrent();
  319. GLuint tex1 = glCreateAndConsumeTextureCHROMIUM(mailbox.name);
  320. glBindTexture(GL_TEXTURE_2D, tex1);
  321. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  322. EXPECT_EQ(0xFFFFFF00u, ReadTexel(tex1, 0, 0));
  323. gl2_.MakeCurrent();
  324. glClearColor(1, 0, 0, 1);
  325. glClear(GL_COLOR_BUFFER_BIT);
  326. ::gles2::GetGLContext()->SwapBuffers(1);
  327. gl1_.MakeCurrent();
  328. EXPECT_EQ(0xFFFFFF00u, ReadTexel(tex1, 0, 0));
  329. glDeleteTextures(1, &tex1);
  330. gl2_.MakeCurrent();
  331. gl2_.decoder()->ReturnFrontBuffer(mailbox, false);
  332. // Flushing doesn't matter, only SwapBuffers(1).
  333. glClearColor(0, 1, 0, 1);
  334. glClear(GL_COLOR_BUFFER_BIT);
  335. glFlush();
  336. Mailbox mailbox2 = Mailbox::Generate();
  337. gl2_.decoder()->TakeFrontBuffer(mailbox2);
  338. gl1_.MakeCurrent();
  339. tex1 = glCreateAndConsumeTextureCHROMIUM(mailbox2.name);
  340. glBindTexture(GL_TEXTURE_2D, tex1);
  341. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  342. EXPECT_EQ(0xFF0000FFu, ReadTexel(tex1, 0, 0));
  343. gl2_.MakeCurrent();
  344. gl2_.Destroy();
  345. gl1_.MakeCurrent();
  346. EXPECT_EQ(0xFF0000FFu, ReadTexel(tex1, 0, 0));
  347. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  348. glDeleteTextures(1, &tex1);
  349. }
  350. // The client, represented by |gl2_|, will request 5 frontbuffers, and then
  351. // start returning them.
  352. TEST_F(GLTextureMailboxTest, FrontBufferCache) {
  353. SetUpContexts();
  354. gl1_.MakeCurrent();
  355. std::vector<Mailbox> mailboxes;
  356. for (int i = 0; i < 5; ++i) {
  357. Mailbox mailbox = TakeAndConsumeMailbox();
  358. mailboxes.push_back(mailbox);
  359. }
  360. EXPECT_EQ(5u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  361. EXPECT_EQ(5u, gl1_.decoder()->GetCreatedBackTextureCountForTest());
  362. // If the textures aren't lost, they're reused.
  363. for (int i = 0; i < 100; ++i) {
  364. gl1_.decoder()->ReturnFrontBuffer(mailboxes[0], false);
  365. mailboxes.erase(mailboxes.begin());
  366. Mailbox mailbox = TakeAndConsumeMailbox();
  367. mailboxes.push_back(mailbox);
  368. }
  369. EXPECT_EQ(5u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  370. EXPECT_EQ(5u, gl1_.decoder()->GetCreatedBackTextureCountForTest());
  371. // If the textures are lost, they're not reused.
  372. for (int i = 0; i < 100; ++i) {
  373. gl1_.decoder()->ReturnFrontBuffer(mailboxes[0], true);
  374. mailboxes.erase(mailboxes.begin());
  375. Mailbox mailbox = TakeAndConsumeMailbox();
  376. mailboxes.push_back(mailbox);
  377. }
  378. EXPECT_EQ(5u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  379. EXPECT_EQ(105u, gl1_.decoder()->GetCreatedBackTextureCountForTest());
  380. }
  381. // The client, represented by |gl2_|, will request and return 5 frontbuffers.
  382. // Then the size of the buffer will be changed. All cached frontbuffers should
  383. // be discarded.
  384. TEST_F(GLTextureMailboxTest, FrontBufferChangeSize) {
  385. SetUpContexts();
  386. gl1_.MakeCurrent();
  387. std::vector<Mailbox> mailboxes;
  388. for (int i = 0; i < 5; ++i) {
  389. Mailbox mailbox = TakeAndConsumeMailbox();
  390. mailboxes.push_back(mailbox);
  391. }
  392. EXPECT_EQ(5u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  393. for (int i = 0; i < 5; ++i) {
  394. gl1_.decoder()->ReturnFrontBuffer(mailboxes[i], false);
  395. }
  396. mailboxes.clear();
  397. EXPECT_EQ(5u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  398. glResizeCHROMIUM(21, 31, 1, GetGLColorSpace(), true);
  399. ::gles2::GetGLContext()->SwapBuffers(1);
  400. EXPECT_EQ(0u, gl1_.decoder()->GetSavedBackTextureCountForTest());
  401. }
  402. // The client, represented by |gl2_|, will request and return 5 frontbuffers.
  403. // Then |gl1_| will start drawing with a different color. The returned
  404. // frontbuffers should pick up the new color.
  405. TEST_F(GLTextureMailboxTest, FrontBufferChangeColor) {
  406. GLManager::Options options1;
  407. options1.multisampled = true;
  408. gl1_.Initialize(options1);
  409. GLManager::Options options2;
  410. options2.share_mailbox_manager = &gl1_;
  411. gl2_.Initialize(options2);
  412. gl1_.MakeCurrent();
  413. std::vector<Mailbox> mailboxes;
  414. for (int i = 0; i < 5; ++i) {
  415. Mailbox mailbox = TakeAndConsumeMailbox();
  416. mailboxes.push_back(mailbox);
  417. }
  418. for (int i = 0; i < 5; ++i) {
  419. gl1_.decoder()->ReturnFrontBuffer(mailboxes[i], false);
  420. }
  421. mailboxes.clear();
  422. for (int i = 0; i < 5; ++i) {
  423. glClearColor(1, 0, 0, 1);
  424. glClear(GL_COLOR_BUFFER_BIT);
  425. ::gles2::GetGLContext()->SwapBuffers(1);
  426. Mailbox mailbox = Mailbox::Generate();
  427. gl1_.decoder()->TakeFrontBuffer(mailbox);
  428. // Normally, consumers of TakeFrontBuffer() must supply their own
  429. // synchronization mechanism. For this test, just use a glFinish().
  430. glFinish();
  431. gl2_.MakeCurrent();
  432. GLuint tex = glCreateAndConsumeTextureCHROMIUM(mailbox.name);
  433. glBindTexture(GL_TEXTURE_2D, tex);
  434. EXPECT_EQ(0xFF0000FFu, ReadTexel(tex, 0, 0));
  435. glDeleteTextures(1, &tex);
  436. glFlush();
  437. gl1_.MakeCurrent();
  438. }
  439. }
  440. // Verify that front buffer textures have sampler parameters that will not cause
  441. // them to be incomplete when sampled
  442. TEST_F(GLTextureMailboxTest, FrontBufferSamplerParameters) {
  443. SetUpContexts();
  444. gl2_.MakeCurrent();
  445. glResizeCHROMIUM(10, 10, 1, GetGLColorSpace(), true);
  446. glClearColor(0, 1, 1, 1);
  447. glClear(GL_COLOR_BUFFER_BIT);
  448. ::gles2::GetGLContext()->SwapBuffers(1);
  449. Mailbox mailbox = Mailbox::Generate();
  450. gl2_.decoder()->TakeFrontBuffer(mailbox);
  451. gl1_.MakeCurrent();
  452. GLuint tex1 = glCreateAndConsumeTextureCHROMIUM(mailbox.name);
  453. glBindTexture(GL_TEXTURE_2D, tex1);
  454. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  455. constexpr std::pair<GLenum, GLint> expected_parameters[] = {
  456. {GL_TEXTURE_MAG_FILTER, GL_LINEAR},
  457. {GL_TEXTURE_MIN_FILTER, GL_LINEAR},
  458. {GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE},
  459. {GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE},
  460. };
  461. for (const auto& expected_parameter : expected_parameters) {
  462. GLint value = 0;
  463. glGetTexParameteriv(GL_TEXTURE_2D, expected_parameter.first, &value);
  464. EXPECT_EQ(expected_parameter.second, value);
  465. }
  466. }
  467. // http://crbug.com/281565
  468. #if !BUILDFLAG(IS_ANDROID)
  469. TEST_F(GLTextureMailboxTest, TakeFrontBufferMultipleContexts) {
  470. SetUpContexts();
  471. Mailbox mailbox[2];
  472. GLuint tex[2];
  473. GLManager::Options options;
  474. options.share_mailbox_manager = &gl1_;
  475. GLManager other_gl[2];
  476. for (size_t i = 0; i < 2; ++i) {
  477. other_gl[i].Initialize(options);
  478. other_gl[i].MakeCurrent();
  479. glResizeCHROMIUM(10, 10, 1, GetGLColorSpace(), true);
  480. glClearColor(1 - i % 2, i % 2, 0, 1);
  481. glClear(GL_COLOR_BUFFER_BIT);
  482. ::gles2::GetGLContext()->SwapBuffers(0, 1);
  483. mailbox[i] = Mailbox::Generate();
  484. other_gl[i].decoder()->TakeFrontBuffer(mailbox[i]);
  485. // Make sure both "other gl" are in the same share group.
  486. if (!options.share_group_manager)
  487. options.share_group_manager = other_gl+i;
  488. }
  489. gl1_.MakeCurrent();
  490. for (size_t i = 0; i < 2; ++i) {
  491. tex[i] = glCreateAndConsumeTextureCHROMIUM(mailbox[i].name);
  492. glBindTexture(GL_TEXTURE_2D, tex[i]);
  493. EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
  494. }
  495. gl1_.MakeCurrent();
  496. EXPECT_EQ(0xFF0000FFu, ReadTexel(tex[0], 0, 0));
  497. EXPECT_EQ(0xFF00FF00u, ReadTexel(tex[1], 9, 9));
  498. for (size_t i = 0; i < 2; ++i) {
  499. other_gl[i].MakeCurrent();
  500. other_gl[i].Destroy();
  501. }
  502. gl1_.MakeCurrent();
  503. glDeleteTextures(2, tex);
  504. }
  505. #endif
  506. } // namespace gpu