WebTexture2D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef ATOMIC_PLATFORM_OSX
  23. #include <ThirdParty/GLEW/glew.h>
  24. #endif
  25. #include <include/cef_render_handler.h>
  26. #include <Atomic/Math/Rect.h>
  27. #include <Atomic/IO/Log.h>
  28. #include <Atomic/Resource/ResourceCache.h>
  29. #include <Atomic/Graphics/Graphics.h>
  30. #include <Atomic/Graphics/GraphicsImpl.h>
  31. #include <Atomic/Graphics/Technique.h>
  32. #include "WebClient.h"
  33. #include "WebTexture2D.h"
  34. namespace Atomic
  35. {
  36. class WebTexture2DPrivate : public CefRenderHandler
  37. {
  38. friend class WebTexture2D;
  39. public:
  40. IMPLEMENT_REFCOUNTING(WebTexture2DPrivate)
  41. WebTexture2DPrivate(WebTexture2D* webTexture2D)
  42. {
  43. webTexture2D_ = webTexture2D;
  44. graphics_ = webTexture2D->GetSubsystem<Graphics>();
  45. ClearPopupRects();
  46. }
  47. void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) OVERRIDE
  48. {
  49. if (!show)
  50. {
  51. // Clear the popup rectangle.
  52. ClearPopupRects();
  53. }
  54. }
  55. void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) OVERRIDE
  56. {
  57. if (rect.width <= 0 || rect.height <= 0)
  58. {
  59. ClearPopupRects();
  60. return;
  61. }
  62. popupRectOriginal_ = rect;
  63. popupRect_ = GetPopupRectInWebView(popupRectOriginal_);
  64. }
  65. CefRect GetPopupRectInWebView(const CefRect& original_rect) {
  66. CefRect rc(original_rect);
  67. // if x or y are negative, move them to 0.
  68. if (rc.x < 0)
  69. rc.x = 0;
  70. if (rc.y < 0)
  71. rc.y = 0;
  72. // if popup goes outside the view, try to reposition origin
  73. if (rc.x + rc.width > webTexture2D_->GetWidth())
  74. rc.x = webTexture2D_->GetWidth() - rc.width;
  75. if (rc.y + rc.height > webTexture2D_->GetHeight())
  76. rc.y = webTexture2D_->GetHeight() - rc.height;
  77. // if x or y became negative, move them to 0 again.
  78. if (rc.x < 0)
  79. rc.x = 0;
  80. if (rc.y < 0)
  81. rc.y = 0;
  82. return rc;
  83. }
  84. void ClearPopupRects()
  85. {
  86. popupRect_.Set(0, 0, 0, 0);
  87. popupRectOriginal_.Set(0, 0, 0, 0);
  88. }
  89. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  90. {
  91. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  92. return true;
  93. }
  94. #ifdef ATOMIC_OPENGL
  95. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  96. const void *buffer, int width, int height) OVERRIDE
  97. {
  98. glEnable(GL_TEXTURE_2D);
  99. if (type == PET_VIEW)
  100. {
  101. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  102. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  103. if (dirtyRects.size() == 1 &&
  104. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  105. {
  106. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  107. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  108. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  109. }
  110. else
  111. {
  112. // Update just the dirty rectangles.
  113. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  114. for (; i != dirtyRects.end(); ++i)
  115. {
  116. const CefRect& rect = *i;
  117. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  118. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  119. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  120. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  121. buffer);
  122. }
  123. }
  124. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  125. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  126. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  127. glBindTexture(GL_TEXTURE_2D, 0);
  128. }
  129. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  130. {
  131. int skip_pixels = 0, x = popupRect_.x;
  132. int skip_rows = 0, y = popupRect_.y;
  133. int w = width;
  134. int h = height;
  135. int viewwidth = webTexture2D_->GetWidth();
  136. int viewheight = webTexture2D_->GetHeight();
  137. // Adjust the popup to fit inside the view.
  138. if (x < 0)
  139. {
  140. skip_pixels = -x;
  141. x = 0;
  142. }
  143. if (y < 0)
  144. {
  145. skip_rows = -y;
  146. y = 0;
  147. }
  148. if (x + w > viewwidth)
  149. {
  150. w -= x + w - viewwidth;
  151. }
  152. if (y + h > viewheight)
  153. {
  154. h -= y + h - viewheight;
  155. }
  156. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  157. // Update the popup rectangle.
  158. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  159. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  160. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  161. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  162. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  163. glBindTexture(GL_TEXTURE_2D, 0);
  164. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  165. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  166. }
  167. glDisable(GL_TEXTURE_2D);
  168. }
  169. #else
  170. #ifndef ATOMIC_OPENGL
  171. // Windows D3D blitting
  172. void D3DBlit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false)
  173. {
  174. #ifdef ATOMIC_D3D11
  175. if (!webTexture2D_ || !webTexture2D_->GetWidth() || !webTexture2D_->GetHeight() || !dstRect.Width() || !dstRect.Height())
  176. {
  177. return;
  178. }
  179. if ((dstRect.left_ + dstRect.Width() > webTexture2D_->GetWidth()) || (dstRect.top_ + dstRect.Height() > webTexture2D_->GetHeight()))
  180. return;
  181. D3D11_BOX box;
  182. box.left = dstRect.left_;
  183. box.right = dstRect.right_;
  184. box.top = dstRect.top_;
  185. box.bottom = dstRect.bottom_;
  186. box.front = 0;
  187. box.back = 1;
  188. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Resource*)webTexture2D_->GetTexture2D()->GetGPUObject(), 0, &box, src, srcStride, 0);
  189. #else
  190. RECT d3dRect;
  191. d3dRect.left = dstRect.left_;
  192. d3dRect.top = dstRect.top_;
  193. d3dRect.right = dstRect.right_;
  194. d3dRect.bottom = dstRect.bottom_;
  195. int level = 0;
  196. DWORD flags = discard ? D3DLOCK_DISCARD : 0;
  197. D3DLOCKED_RECT d3dLockedRect;
  198. IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject();
  199. if (!object || FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  200. {
  201. ATOMIC_LOGERROR("WebTexture2D - Could not lock texture");
  202. return;
  203. }
  204. int width = dstRect.Width();
  205. int height = dstRect.Height();
  206. for (int j = 0; j < height; ++j)
  207. {
  208. unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch;
  209. memcpy(dst, src, width * 4);
  210. src += srcStride;
  211. }
  212. object->UnlockRect(level);
  213. #endif
  214. }
  215. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  216. const void *buffer, int width, int height) OVERRIDE
  217. {
  218. if (type == PET_VIEW)
  219. {
  220. if (dirtyRects.size() == 1 &&
  221. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  222. {
  223. D3DBlit(IntRect(0, 0, width, height), (unsigned char*)buffer, width * 4, true);
  224. return;
  225. }
  226. // Update just the dirty rectangles
  227. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  228. for (; i != dirtyRects.end(); ++i)
  229. {
  230. const CefRect& rect = *i;
  231. unsigned char* src = (unsigned char*)buffer;
  232. src += rect.y * (width * 4) + (rect.x * 4);
  233. D3DBlit(IntRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height), src, width * 4, false);
  234. }
  235. }
  236. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  237. {
  238. int x = popupRect_.x;
  239. int y = popupRect_.y;
  240. int w = width;
  241. int h = height;
  242. int viewwidth = webTexture2D_->GetWidth();
  243. int viewheight = webTexture2D_->GetHeight();
  244. // Adjust the popup to fit inside the view.
  245. if (x < 0)
  246. {
  247. x = 0;
  248. }
  249. if (y < 0)
  250. {
  251. y = 0;
  252. }
  253. if (x + w > viewwidth)
  254. {
  255. w -= x + w - viewwidth;
  256. }
  257. if (y + h > viewheight)
  258. {
  259. h -= y + h - viewheight;
  260. }
  261. unsigned char* src = (unsigned char*)buffer;
  262. D3DBlit(IntRect(x, y, x + w, y + h), src, width * 4, false);
  263. }
  264. #endif
  265. }
  266. #endif
  267. private:
  268. CefRect popupRect_;
  269. CefRect popupRectOriginal_;
  270. WeakPtr<Graphics> graphics_;
  271. WeakPtr<WebTexture2D> webTexture2D_;
  272. };
  273. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context), clearColor_(Color::WHITE)
  274. {
  275. d_ = new WebTexture2DPrivate(this);
  276. d_->AddRef();
  277. texture_ = new Texture2D(context_);
  278. texture_->SetNumLevels(1);
  279. texture_->SetFilterMode(FILTER_NEAREST);
  280. }
  281. WebTexture2D::~WebTexture2D()
  282. {
  283. //d_->Release();
  284. }
  285. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  286. {
  287. return d_;
  288. }
  289. int WebTexture2D::GetWidth() const
  290. {
  291. return texture_->GetWidth();
  292. }
  293. int WebTexture2D::GetHeight() const
  294. {
  295. return texture_->GetHeight();
  296. }
  297. void WebTexture2D::SetSize(int width, int height)
  298. {
  299. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  300. return;
  301. // initialize to white (color should probably be an option)
  302. TextureUsage textureUsage = TEXTURE_DYNAMIC;
  303. unsigned format = Graphics::GetRGBAFormat();
  304. #ifdef ATOMIC_D3D11
  305. // D3D11 uses a static texture (in BGRA format), required for subresource update with rectangle
  306. textureUsage = TEXTURE_STATIC;
  307. format = DXGI_FORMAT_B8G8R8A8_UNORM;
  308. #endif
  309. if (!texture_->SetSize(width, height, format, textureUsage))
  310. {
  311. ATOMIC_LOGERRORF("Unable to set WebTexture2D size to %i x %i", width, height);
  312. return;
  313. }
  314. SharedArrayPtr<unsigned> cleardata(new unsigned[width * height]);
  315. unsigned color = clearColor_.ToUInt();
  316. unsigned* ptr = cleardata.Get();
  317. for (unsigned i = 0; i < width * height; i++, ptr++)
  318. {
  319. *ptr = color;
  320. }
  321. texture_->SetData(0, 0, 0, width, height, cleardata.Get());
  322. }
  323. }