WebTexture2D.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #ifdef ATOMIC_D3D11
  44. stagingTexture_ = 0;
  45. stagingWidth_ = 0;
  46. stagingHeight_ = 0;
  47. #endif
  48. webTexture2D_ = webTexture2D;
  49. graphics_ = webTexture2D->GetSubsystem<Graphics>();
  50. ClearPopupRects();
  51. }
  52. void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) OVERRIDE
  53. {
  54. if (!show)
  55. {
  56. // Clear the popup rectangle.
  57. ClearPopupRects();
  58. }
  59. }
  60. void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) OVERRIDE
  61. {
  62. if (rect.width <= 0 || rect.height <= 0)
  63. {
  64. ClearPopupRects();
  65. return;
  66. }
  67. popupRectOriginal_ = rect;
  68. popupRect_ = GetPopupRectInWebView(popupRectOriginal_);
  69. }
  70. CefRect GetPopupRectInWebView(const CefRect& original_rect) {
  71. CefRect rc(original_rect);
  72. // if x or y are negative, move them to 0.
  73. if (rc.x < 0)
  74. rc.x = 0;
  75. if (rc.y < 0)
  76. rc.y = 0;
  77. // if popup goes outside the view, try to reposition origin
  78. if (rc.x + rc.width > webTexture2D_->GetWidth())
  79. rc.x = webTexture2D_->GetWidth() - rc.width;
  80. if (rc.y + rc.height > webTexture2D_->GetHeight())
  81. rc.y = webTexture2D_->GetHeight() - rc.height;
  82. // if x or y became negative, move them to 0 again.
  83. if (rc.x < 0)
  84. rc.x = 0;
  85. if (rc.y < 0)
  86. rc.y = 0;
  87. return rc;
  88. }
  89. void ClearPopupRects()
  90. {
  91. popupRect_.Set(0, 0, 0, 0);
  92. popupRectOriginal_.Set(0, 0, 0, 0);
  93. }
  94. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  95. {
  96. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  97. return true;
  98. }
  99. #ifndef ATOMIC_PLATFORM_WINDOWS
  100. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  101. const void *buffer, int width, int height) OVERRIDE
  102. {
  103. glEnable(GL_TEXTURE_2D);
  104. if (type == PET_VIEW)
  105. {
  106. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  107. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  108. if (dirtyRects.size() == 1 &&
  109. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  110. {
  111. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  112. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  113. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  114. }
  115. else
  116. {
  117. // Update just the dirty rectangles.
  118. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  119. for (; i != dirtyRects.end(); ++i)
  120. {
  121. const CefRect& rect = *i;
  122. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  123. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  124. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  125. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  126. buffer);
  127. }
  128. }
  129. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  130. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  131. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  132. glBindTexture(GL_TEXTURE_2D, 0);
  133. }
  134. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  135. {
  136. int skip_pixels = 0, x = popupRect_.x;
  137. int skip_rows = 0, y = popupRect_.y;
  138. int w = width;
  139. int h = height;
  140. int viewwidth = webTexture2D_->GetWidth();
  141. int viewheight = webTexture2D_->GetHeight();
  142. // Adjust the popup to fit inside the view.
  143. if (x < 0)
  144. {
  145. skip_pixels = -x;
  146. x = 0;
  147. }
  148. if (y < 0)
  149. {
  150. skip_rows = -y;
  151. y = 0;
  152. }
  153. if (x + w > viewwidth)
  154. {
  155. w -= x + w - viewwidth;
  156. }
  157. if (y + h > viewheight)
  158. {
  159. h -= y + h - viewheight;
  160. }
  161. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  162. // Update the popup rectangle.
  163. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  164. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  165. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  166. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  167. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  168. glBindTexture(GL_TEXTURE_2D, 0);
  169. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  170. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  171. }
  172. glDisable(GL_TEXTURE_2D);
  173. }
  174. #else
  175. #ifndef ATOMIC_OPENGL
  176. // Windows D3D blitting
  177. #ifdef ATOMIC_D3D11
  178. void ReleaseStagingTexture()
  179. {
  180. if (stagingTexture_)
  181. {
  182. stagingTexture_->Release();
  183. }
  184. stagingTexture_ = 0;
  185. stagingWidth_ = 0;
  186. stagingHeight_ = 0;
  187. }
  188. void UpdateStagingTexture()
  189. {
  190. if (stagingTexture_)
  191. {
  192. ReleaseStagingTexture();
  193. }
  194. D3D11_TEXTURE2D_DESC textureDesc;
  195. memset(&textureDesc, 0, sizeof textureDesc);
  196. textureDesc.Width = (UINT)webTexture2D_->GetWidth();
  197. textureDesc.Height = (UINT)webTexture2D_->GetHeight();
  198. textureDesc.MipLevels = 1;
  199. textureDesc.ArraySize = 1;
  200. textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  201. textureDesc.SampleDesc.Count = 1;
  202. textureDesc.SampleDesc.Quality = 0;
  203. textureDesc.Usage = D3D11_USAGE_DEFAULT;
  204. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture2D(&textureDesc, nullptr, &stagingTexture_);
  205. if (!SUCCEEDED(hr))
  206. {
  207. stagingTexture_ = 0;
  208. return;
  209. }
  210. stagingWidth_ = webTexture2D_->GetWidth();
  211. stagingHeight_ = webTexture2D_->GetHeight();
  212. }
  213. #endif
  214. void D3DBlit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false)
  215. {
  216. #ifdef ATOMIC_D3D11
  217. if (!webTexture2D_ || !webTexture2D_->GetWidth() || !webTexture2D_->GetHeight() || !dstRect.Width() || !dstRect.Height())
  218. {
  219. return;
  220. }
  221. if (!stagingTexture_ || (stagingWidth_ != webTexture2D_->GetWidth()) || (stagingHeight_ != webTexture2D_->GetHeight()))
  222. {
  223. return;
  224. }
  225. D3D11_BOX box;
  226. box.left = dstRect.left_;
  227. box.right = dstRect.right_;
  228. box.top = dstRect.top_;
  229. box.bottom = dstRect.bottom_;
  230. box.front = 0;
  231. box.back = 1;
  232. if ((dstRect.left_ + dstRect.Width() > stagingWidth_) || (dstRect.top_ + dstRect.Height() > stagingHeight_))
  233. return;
  234. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource(stagingTexture_, 0, &box, src, srcStride, 0);
  235. graphics_->GetImpl()->GetDeviceContext()->CopyResource((ID3D11Resource*)webTexture2D_->GetTexture2D()->GetGPUObject(), stagingTexture_);
  236. #else
  237. RECT d3dRect;
  238. d3dRect.left = dstRect.left_;
  239. d3dRect.top = dstRect.top_;
  240. d3dRect.right = dstRect.right_;
  241. d3dRect.bottom = dstRect.bottom_;
  242. int level = 0;
  243. DWORD flags = discard ? D3DLOCK_DISCARD : 0;
  244. D3DLOCKED_RECT d3dLockedRect;
  245. IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject();
  246. if (!object || FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  247. {
  248. ATOMIC_LOGERROR("WebTexture2D - Could not lock texture");
  249. return;
  250. }
  251. int width = dstRect.Width();
  252. int height = dstRect.Height();
  253. for (int j = 0; j < height; ++j)
  254. {
  255. unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch;
  256. memcpy(dst, src, width * 4);
  257. src += srcStride;
  258. }
  259. object->UnlockRect(level);
  260. #endif
  261. }
  262. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  263. const void *buffer, int width, int height) OVERRIDE
  264. {
  265. if (type == PET_VIEW)
  266. {
  267. if (dirtyRects.size() == 1 &&
  268. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  269. {
  270. D3DBlit(IntRect(0, 0, width, height), (unsigned char*) buffer, width * 4, true);
  271. return;
  272. }
  273. // Update just the dirty rectangles
  274. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  275. for (; i != dirtyRects.end(); ++i)
  276. {
  277. const CefRect& rect = *i;
  278. unsigned char* src = (unsigned char*) buffer;
  279. src += rect.y * (width * 4) + (rect.x * 4);
  280. D3DBlit(IntRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height), src, width * 4, false);
  281. }
  282. }
  283. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  284. {
  285. int x = popupRect_.x;
  286. int y = popupRect_.y;
  287. int w = width;
  288. int h = height;
  289. int viewwidth = webTexture2D_->GetWidth();
  290. int viewheight = webTexture2D_->GetHeight();
  291. // Adjust the popup to fit inside the view.
  292. if (x < 0)
  293. {
  294. x = 0;
  295. }
  296. if (y < 0)
  297. {
  298. y = 0;
  299. }
  300. if (x + w > viewwidth)
  301. {
  302. w -= x + w - viewwidth;
  303. }
  304. if (y + h > viewheight)
  305. {
  306. h -= y + h - viewheight;
  307. }
  308. unsigned char* src = (unsigned char*) buffer;
  309. D3DBlit(IntRect(x, y, x + w, y + h), src, width * 4, false);
  310. }
  311. #endif
  312. }
  313. #endif
  314. private:
  315. #ifdef ATOMIC_D3D11
  316. ID3D11Texture2D* stagingTexture_;
  317. int stagingWidth_;
  318. int stagingHeight_;
  319. #endif
  320. CefRect popupRect_;
  321. CefRect popupRectOriginal_;
  322. WeakPtr<Graphics> graphics_;
  323. WeakPtr<WebTexture2D> webTexture2D_;
  324. };
  325. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context), clearColor_(Color::WHITE)
  326. {
  327. d_ = new WebTexture2DPrivate(this);
  328. d_->AddRef();
  329. texture_ = new Texture2D(context_);
  330. texture_->SetNumLevels(1);
  331. texture_->SetFilterMode(FILTER_NEAREST);
  332. }
  333. WebTexture2D::~WebTexture2D()
  334. {
  335. //d_->Release();
  336. #ifdef ATOMIC_D3D11
  337. d_->ReleaseStagingTexture();
  338. #endif
  339. }
  340. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  341. {
  342. return d_;
  343. }
  344. int WebTexture2D::GetWidth() const
  345. {
  346. return texture_->GetWidth();
  347. }
  348. int WebTexture2D::GetHeight() const
  349. {
  350. return texture_->GetHeight();
  351. }
  352. void WebTexture2D::SetSize(int width, int height)
  353. {
  354. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  355. return;
  356. // initialize to white (color should probably be an option)
  357. #ifndef ATOMIC_D3D11
  358. if (!texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC))
  359. {
  360. ATOMIC_LOGERRORF("Unable to set WebTexture2D size to %i x %i", width, height);
  361. return;
  362. }
  363. #else
  364. if (!texture_->SetSize(width, height, DXGI_FORMAT_B8G8R8A8_UNORM, TEXTURE_STATIC))
  365. {
  366. ATOMIC_LOGERRORF("Unable to set WebTexture2D size to %i x %i", width, height);
  367. return;
  368. }
  369. d_->UpdateStagingTexture();
  370. #endif
  371. SharedArrayPtr<unsigned> cleardata(new unsigned[width * height]);
  372. unsigned color = clearColor_.ToUInt();
  373. unsigned* ptr = cleardata.Get();
  374. for (unsigned i = 0; i < width * height; i++, ptr++)
  375. {
  376. *ptr = color;
  377. }
  378. texture_->SetData(0, 0, 0, width, height, cleardata.Get());
  379. }
  380. }