Video.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Video.h"
  21. // LOVE
  22. #include "Shader.h"
  23. #include "Graphics.h"
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. love::Type Video::type("Video", &Drawable::type);
  29. Video::Video(Graphics *gfx, love::video::VideoStream *stream, float dpiscale)
  30. : stream(stream)
  31. , width(stream->getWidth() / dpiscale)
  32. , height(stream->getHeight() / dpiscale)
  33. , samplerState()
  34. {
  35. const SamplerState &defaultSampler = gfx->getDefaultSamplerState();
  36. samplerState.minFilter = defaultSampler.minFilter;
  37. samplerState.magFilter = defaultSampler.magFilter;
  38. samplerState.wrapU = defaultSampler.wrapU;
  39. samplerState.wrapV = defaultSampler.wrapV;
  40. samplerState.maxAnisotropy = defaultSampler.maxAnisotropy;
  41. stream->fillBackBuffer();
  42. for (int i = 0; i < 4; i++)
  43. vertices[i].color = Color32(255, 255, 255, 255);
  44. // Vertices are ordered for use with triangle strips:
  45. // 0---2
  46. // | / |
  47. // 1---3
  48. vertices[0].x = 0.0f;
  49. vertices[0].y = 0.0f;
  50. vertices[1].x = 0.0f;
  51. vertices[1].y = (float) height;
  52. vertices[2].x = (float) width;
  53. vertices[2].y = 0.0f;
  54. vertices[3].x = (float) width;
  55. vertices[3].y = (float) height;
  56. vertices[0].s = 0.0f;
  57. vertices[0].t = 0.0f;
  58. vertices[1].s = 0.0f;
  59. vertices[1].t = 1.0f;
  60. vertices[2].s = 1.0f;
  61. vertices[2].t = 0.0f;
  62. vertices[3].s = 1.0f;
  63. vertices[3].t = 1.0f;
  64. // Create the textures using the initial frame data.
  65. auto frame = (const love::video::VideoStream::Frame*) stream->getFrontBuffer();
  66. int widths[3] = {frame->yw, frame->cw, frame->cw};
  67. int heights[3] = {frame->yh, frame->ch, frame->ch};
  68. const unsigned char *data[3] = {frame->yplane, frame->cbplane, frame->crplane};
  69. Texture::Settings settings;
  70. for (int i = 0; i < 3; i++)
  71. {
  72. settings.width = widths[i];
  73. settings.height = heights[i];
  74. settings.format = PIXELFORMAT_R8_UNORM;
  75. Texture *tex = gfx->newTexture(settings, nullptr);
  76. tex->setSamplerState(samplerState);
  77. size_t bpp = getPixelFormatBlockSize(PIXELFORMAT_R8_UNORM);
  78. size_t size = bpp * widths[i] * heights[i];
  79. Rect rect = {0, 0, widths[i], heights[i]};
  80. tex->replacePixels(data[i], size, 0, 0, rect, false);
  81. textures[i].set(tex, Acquire::NORETAIN);
  82. }
  83. }
  84. Video::~Video()
  85. {
  86. if (source)
  87. source->stop();
  88. }
  89. love::video::VideoStream *Video::getStream()
  90. {
  91. return stream;
  92. }
  93. void Video::draw(Graphics *gfx, const Matrix4 &m)
  94. {
  95. update();
  96. // setVideoTextures may call flushBatchedDraws before setting the textures, so
  97. // we can't call it after requestBatchedDraw.
  98. auto shader = Shader::current;
  99. if (Shader::isDefaultActive())
  100. shader = Shader::standardShaders[Shader::STANDARD_VIDEO];
  101. if (shader != nullptr)
  102. shader->setVideoTextures(textures[0], textures[1], textures[2]);
  103. const Matrix4 &tm = gfx->getTransform();
  104. bool is2D = tm.isAffine2DTransform();
  105. Matrix4 t(tm, m);
  106. Graphics::BatchedDrawCommand cmd;
  107. cmd.formats[0] = getSinglePositionFormat(is2D);
  108. cmd.formats[1] = CommonFormat::STf_RGBAub;
  109. cmd.indexMode = TRIANGLEINDEX_QUADS;
  110. cmd.vertexCount = 4;
  111. cmd.standardShaderType = Shader::STANDARD_VIDEO;
  112. Graphics::BatchedVertexData data = gfx->requestBatchedDraw(cmd);
  113. if (is2D)
  114. t.transformXY((Vector2 *) data.stream[0], vertices, 4);
  115. else
  116. t.transformXY0((Vector3 *) data.stream[0], vertices, 4);
  117. STf_RGBAub *verts = (STf_RGBAub *) data.stream[1];
  118. Color32 c = toColor32(gfx->getColor());
  119. for (int i = 0; i < 4; i++)
  120. {
  121. verts[i].s = vertices[i].s;
  122. verts[i].t = vertices[i].t;
  123. verts[i].color = c;
  124. }
  125. gfx->flushBatchedDraws();
  126. }
  127. void Video::update()
  128. {
  129. bool bufferschanged = stream->swapBuffers();
  130. stream->fillBackBuffer();
  131. if (bufferschanged)
  132. {
  133. auto frame = (const love::video::VideoStream::Frame*) stream->getFrontBuffer();
  134. int widths[3] = {frame->yw, frame->cw, frame->cw};
  135. int heights[3] = {frame->yh, frame->ch, frame->ch};
  136. const unsigned char *data[3] = {frame->yplane, frame->cbplane, frame->crplane};
  137. for (int i = 0; i < 3; i++)
  138. {
  139. size_t bpp = getPixelFormatBlockSize(PIXELFORMAT_R8_UNORM);
  140. size_t size = bpp * widths[i] * heights[i];
  141. Rect rect = {0, 0, widths[i], heights[i]};
  142. textures[i]->replacePixels(data[i], size, 0, 0, rect, false);
  143. }
  144. }
  145. }
  146. love::audio::Source *Video::getSource()
  147. {
  148. return source;
  149. }
  150. void Video::setSource(love::audio::Source *source)
  151. {
  152. this->source = source;
  153. }
  154. int Video::getWidth() const
  155. {
  156. return width;
  157. }
  158. int Video::getHeight() const
  159. {
  160. return height;
  161. }
  162. int Video::getPixelWidth() const
  163. {
  164. return stream->getWidth();
  165. }
  166. int Video::getPixelHeight() const
  167. {
  168. return stream->getHeight();
  169. }
  170. void Video::setSamplerState(const SamplerState &s)
  171. {
  172. samplerState.minFilter = s.minFilter;
  173. samplerState.magFilter = s.magFilter;
  174. samplerState.wrapU = s.wrapU;
  175. samplerState.wrapV = s.wrapV;
  176. samplerState.maxAnisotropy = s.maxAnisotropy;
  177. for (const auto &texture : textures)
  178. texture->setSamplerState(samplerState);
  179. }
  180. const SamplerState &Video::getSamplerState() const
  181. {
  182. return samplerState;
  183. }
  184. } // graphics
  185. } // love