SigreCommandBuffer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) 2014 Sony Computer Entertainment Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "SigreCommandBuffer.h"
  27. #if USE(ACCELERATED_COMPOSITING) && USE(ACAGI)
  28. #include "AcagiConfig.h"
  29. #include "FloatRect.h"
  30. #include "NotImplemented.h"
  31. #include <stdio.h>
  32. #define DBG_TRACE printf("%s %d\n", __PRETTY_FUNCTION__, __LINE__)
  33. #if DEBUG_TILEDBACKEND_V2
  34. static const WebCore::FloatRect MemoryBarArea(30, 480, 800, 20);
  35. #endif
  36. // Converters
  37. namespace tilebackend {
  38. IntRect fromCore(const WebCore::IntRect &o)
  39. {
  40. IntRect r;
  41. r.x = o.x();
  42. r.y = o.y();
  43. r.w = o.width();
  44. r.h = o.height();
  45. return r;
  46. }
  47. FloatRect fromCore(const WebCore::FloatRect &o)
  48. {
  49. FloatRect r;
  50. r.x = o.x();
  51. r.y = o.y();
  52. r.w = o.width();
  53. r.h = o.height();
  54. return r;
  55. }
  56. TransformationMatrix fromCore(const WebCore::TransformationMatrix &o)
  57. {
  58. float floatMatrix4[16];
  59. o.toColumnMajorFloatArray(floatMatrix4);
  60. TransformationMatrix r;
  61. for (int i = 0; i < 4; i++)
  62. for (int j = 0; j < 4; j++)
  63. r.m[i][j] = floatMatrix4[i + j * 4];
  64. return r;
  65. }
  66. }
  67. namespace WebCore {
  68. PassOwnPtr<SigreCommandBuffer> SigreCommandBuffer::create(Allocator* allocator, PlatformGraphics* gfx)
  69. {
  70. ASSERT(allocator);
  71. ASSERT(gfx);
  72. SigreCommandBuffer* context = new SigreCommandBuffer(allocator, gfx);
  73. return adoptPtr(context);
  74. }
  75. PassRefPtr<SigreTexture> SigreCommandBuffer::createTexture(IntSize textureSize)
  76. {
  77. return SigreTexture::create(m_allocator, textureSize);
  78. }
  79. SigreCommandBuffer::SigreCommandBuffer(Allocator* allocator, PlatformGraphics* gfx)
  80. : m_allocator(allocator)
  81. , m_gfx(gfx)
  82. {
  83. }
  84. SigreCommandBuffer::~SigreCommandBuffer()
  85. {
  86. }
  87. void SigreCommandBuffer::clearCommands()
  88. {
  89. DBG_TRACE;
  90. m_back.clear();
  91. m_frontLocker.lock();
  92. m_front.clear();
  93. m_frontLocker.unlock();
  94. }
  95. struct SigreCommand {
  96. enum Type {
  97. BeginPainting,
  98. EndPainting,
  99. DrawTexture,
  100. DrawSolidColor,
  101. Scissor
  102. };
  103. SigreCommand(Type type) : type(type) { }
  104. virtual ~SigreCommand() { }
  105. const Type type;
  106. };
  107. struct SigreCommandBeginPainting : public SigreCommand {
  108. SigreCommandBeginPainting() : SigreCommand(BeginPainting) { }
  109. };
  110. struct SigreCommandEndPainting : public SigreCommand {
  111. SigreCommandEndPainting() : SigreCommand(EndPainting) { }
  112. };
  113. struct SigreCommandDrawTexture : public SigreCommand {
  114. SigreCommandDrawTexture(PassRefPtr<SigreTexture> texture, const FloatRect& target, const TransformationMatrix& modelViewMatrix, float opacity)
  115. : SigreCommand(DrawTexture)
  116. , texture(texture)
  117. , target(target)
  118. , modelViewMatrix(modelViewMatrix)
  119. , opacity(opacity)
  120. { }
  121. RefPtr<SigreTexture> texture;
  122. FloatRect target;
  123. TransformationMatrix modelViewMatrix;
  124. float opacity;
  125. };
  126. struct SigreCommandDrawSolidColor : public SigreCommand {
  127. SigreCommandDrawSolidColor(const FloatRect& target, const TransformationMatrix& modelViewMatrix, const Color& color)
  128. : SigreCommand(DrawSolidColor)
  129. , target(target)
  130. , modelViewMatrix(modelViewMatrix)
  131. , color(color)
  132. { }
  133. FloatRect target;
  134. TransformationMatrix modelViewMatrix;
  135. Color color;
  136. };
  137. struct SigreCommandScissor : public SigreCommand {
  138. SigreCommandScissor(const FloatRect& scissorBox, bool enable)
  139. : SigreCommand(Scissor)
  140. , scissorBox(scissorBox)
  141. , enable(enable)
  142. { }
  143. FloatRect scissorBox;
  144. bool enable;
  145. };
  146. // Drawing API
  147. void SigreCommandBuffer::beginPainting()
  148. {
  149. m_back = adoptPtr(new CommandList);
  150. SigreCommandBeginPainting* command = new SigreCommandBeginPainting;
  151. m_back->append(adoptPtr(command));
  152. }
  153. void SigreCommandBuffer::endPainting()
  154. {
  155. SigreCommandEndPainting* command = new SigreCommandEndPainting;
  156. m_back->append(adoptPtr(command));
  157. // Move back command buffer to front
  158. m_frontLocker.lock();
  159. m_front = m_back.release();
  160. m_frontLocker.unlock();
  161. }
  162. void SigreCommandBuffer::drawBorder(const Color&, float borderWidth, const FloatRect&, const TransformationMatrix&)
  163. {
  164. }
  165. void SigreCommandBuffer::drawNumber(int number, const Color&, const FloatPoint&, const TransformationMatrix&)
  166. {
  167. }
  168. void SigreCommandBuffer::drawTexture(PassRefPtr<SigreTexture> texture, const FloatRect& target, const TransformationMatrix& modelViewMatrix, float opacity)
  169. {
  170. SigreCommandDrawTexture* command = new SigreCommandDrawTexture(texture, target, modelViewMatrix, opacity);
  171. m_back->append(adoptPtr(command));
  172. }
  173. void SigreCommandBuffer::drawSolidColor(const FloatRect& target, const TransformationMatrix& modelViewMatrix, const Color& color)
  174. {
  175. SigreCommandDrawSolidColor* command = new SigreCommandDrawSolidColor(target, modelViewMatrix, color);
  176. m_back->append(adoptPtr(command));
  177. }
  178. void SigreCommandBuffer::bindSurface(PassRefPtr<SigreTexture> surface)
  179. {
  180. }
  181. void SigreCommandBuffer::scissor(const FloatRect& scissorBox, bool enable)
  182. {
  183. SigreCommandScissor* command = new SigreCommandScissor(scissorBox, enable);
  184. m_back->append(adoptPtr(command));
  185. }
  186. //
  187. void SigreCommandBuffer::patchAndSubmitDrawCommands(const IntRect& clipRect, TargetSurface* targetSurface)
  188. {
  189. if (clipRect.isEmpty())
  190. return;
  191. float scaleX = (float)targetSurface->width / clipRect.width();
  192. float scaleY = (float)targetSurface->height / clipRect.height();
  193. TransformationMatrix transformation;
  194. #if DBG_SCALE_DOWN
  195. transformation.translate(targetSurface->width * 0.25, targetSurface->height * 0.25);
  196. transformation.scale(0.5);
  197. #endif
  198. transformation.scale3d(scaleX, scaleY, 1);
  199. transformation.translate(-clipRect.x(), -clipRect.y());
  200. MutexLocker locker(m_frontLocker);
  201. if (!m_front)
  202. return;
  203. size_t commandNum = m_front->size();
  204. if (!commandNum)
  205. return;
  206. for (size_t i = 0; i < commandNum; i++) {
  207. SigreCommand* command = m_front->at(i).get();
  208. switch (command->type) {
  209. case SigreCommand::BeginPainting:
  210. {
  211. m_gfx->beginPaint(targetSurface);
  212. // Clear surface for debugging.
  213. tilebackend::FloatRect target;
  214. target.x = 0;
  215. target.y = 0;
  216. target.w = targetSurface->width;
  217. target.h = targetSurface->height;
  218. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(TransformationMatrix());
  219. tilebackend::Color color;
  220. color.rgba[0] = 1;
  221. color.rgba[1] = 1;
  222. color.rgba[2] = 1;
  223. color.rgba[3] = 1;
  224. m_gfx->drawSolidColor(target, matrix, color);
  225. }
  226. break;
  227. case SigreCommand::EndPainting:
  228. {
  229. #if DBG_SCALE_DOWN
  230. {
  231. tilebackend::FloatRect target;
  232. target.x = targetSurface->width * 0.25;
  233. target.y = targetSurface->height * 0.25;
  234. target.w = targetSurface->width * 0.5;
  235. target.h = targetSurface->height * 0.5;
  236. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(TransformationMatrix());
  237. tilebackend::Color color;
  238. color.rgba[0] = 1;
  239. color.rgba[1] = 1;
  240. color.rgba[2] = 1;
  241. color.rgba[3] = 1;
  242. m_gfx->drawBorder(target, matrix, color);
  243. }
  244. #endif
  245. #if DEBUG_TILEDBACKEND_V2 && DBG_SHOW_MEMORY_USAGE
  246. // Show memory usage
  247. tilebackend::FloatRect target;
  248. target.x = MemoryBarArea.x();
  249. target.y = MemoryBarArea.y();
  250. target.w = MemoryBarArea.width();
  251. target.h = MemoryBarArea.height();
  252. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(TransformationMatrix());
  253. tilebackend::Color color;
  254. color.rgba[0] = 0;
  255. color.rgba[1] = 0;
  256. color.rgba[2] = 0;
  257. color.rgba[3] = 0.5;
  258. m_gfx->drawSolidColor(target, matrix, color);
  259. m_allocator->reportMemoryChunkInfo(memoryChunkInfoCallback, this);
  260. #endif
  261. m_gfx->endPaint();
  262. }
  263. break;
  264. case SigreCommand::DrawTexture:
  265. {
  266. SigreCommandDrawTexture* c = (SigreCommandDrawTexture*)command;
  267. if (!c->texture->isValid())
  268. continue;
  269. tilebackend::Texture texture = c->texture->texture();
  270. tilebackend::FloatRect target = tilebackend::fromCore(c->target);
  271. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(transformation * c->modelViewMatrix);
  272. m_gfx->drawTexture(texture, target, matrix, c->opacity);
  273. #if DBG_DRAW_BORDER
  274. tilebackend::Color color;
  275. color.rgba[0] = 1;
  276. color.rgba[1] = 0;
  277. color.rgba[2] = 0;
  278. color.rgba[3] = 0.5;
  279. m_gfx->drawBorder(target, matrix, color);
  280. #endif
  281. }
  282. break;
  283. case SigreCommand::DrawSolidColor:
  284. {
  285. SigreCommandDrawSolidColor* c = (SigreCommandDrawSolidColor*)command;
  286. tilebackend::FloatRect target = tilebackend::fromCore(c->target);
  287. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(transformation * c->modelViewMatrix);
  288. tilebackend::Color color;
  289. color.rgba[0] = c->color.red() / 255.0f;
  290. color.rgba[1] = c->color.green() / 255.0f;
  291. color.rgba[2] = c->color.blue() / 255.0f;
  292. color.rgba[3] = c->color.alpha() / 255.0f;
  293. m_gfx->drawSolidColor(target, matrix, color);
  294. }
  295. break;
  296. case SigreCommand::Scissor:
  297. {
  298. SigreCommandScissor* c = (SigreCommandScissor*)command;
  299. tilebackend::FloatRect scissorBox = tilebackend::fromCore(transformation.mapRect(c->scissorBox));
  300. m_gfx->scissor(scissorBox, c->enable);
  301. }
  302. break;
  303. default:
  304. ASSERT_NOT_REACHED();
  305. }
  306. }
  307. }
  308. #if DEBUG_TILEDBACKEND_V2
  309. bool SigreCommandBuffer::memoryChunkInfoCallback(float pos, float size, bool inuse, void* usrdata)
  310. {
  311. if (!inuse)
  312. return true;
  313. SigreCommandBuffer* that = (SigreCommandBuffer*)usrdata;
  314. tilebackend::FloatRect target;
  315. target.x = MemoryBarArea.x() + MemoryBarArea.width() * pos;
  316. target.y = MemoryBarArea.y();
  317. target.w = MemoryBarArea.width() * size;
  318. target.h = MemoryBarArea.height();
  319. tilebackend::TransformationMatrix matrix = tilebackend::fromCore(TransformationMatrix());
  320. tilebackend::Color color;
  321. color.rgba[0] = 0;
  322. color.rgba[1] = 0.7;
  323. color.rgba[2] = 0;
  324. color.rgba[3] = 0.8;
  325. that->m_gfx->drawSolidColor(target, matrix, color);
  326. tilebackend::Color color2;
  327. color2.rgba[0] = 0;
  328. color2.rgba[1] = 1;
  329. color2.rgba[2] = 0;
  330. color2.rgba[3] = 0.5;
  331. that->m_gfx->drawBorder(target, matrix, color2);
  332. return true;
  333. }
  334. #endif
  335. }
  336. #endif