WebTexture2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. ClearPopupRects();
  45. }
  46. void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) OVERRIDE
  47. {
  48. if (!show)
  49. {
  50. // Clear the popup rectangle.
  51. ClearPopupRects();
  52. }
  53. }
  54. void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) OVERRIDE
  55. {
  56. if (rect.width <= 0 || rect.height <= 0)
  57. {
  58. ClearPopupRects();
  59. return;
  60. }
  61. popupRectOriginal_ = rect;
  62. popupRect_ = GetPopupRectInWebView(popupRectOriginal_);
  63. }
  64. CefRect GetPopupRectInWebView(const CefRect& original_rect) {
  65. CefRect rc(original_rect);
  66. // if x or y are negative, move them to 0.
  67. if (rc.x < 0)
  68. rc.x = 0;
  69. if (rc.y < 0)
  70. rc.y = 0;
  71. // if popup goes outside the view, try to reposition origin
  72. if (rc.x + rc.width > webTexture2D_->GetWidth())
  73. rc.x = webTexture2D_->GetWidth() - rc.width;
  74. if (rc.y + rc.height > webTexture2D_->GetHeight())
  75. rc.y = webTexture2D_->GetHeight() - rc.height;
  76. // if x or y became negative, move them to 0 again.
  77. if (rc.x < 0)
  78. rc.x = 0;
  79. if (rc.y < 0)
  80. rc.y = 0;
  81. return rc;
  82. }
  83. void ClearPopupRects()
  84. {
  85. popupRect_.Set(0, 0, 0, 0);
  86. popupRectOriginal_.Set(0, 0, 0, 0);
  87. }
  88. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  89. {
  90. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  91. return true;
  92. }
  93. #ifndef ATOMIC_PLATFORM_WINDOWS
  94. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  95. const void *buffer, int width, int height) OVERRIDE
  96. {
  97. glEnable(GL_TEXTURE_2D);
  98. if (type == PET_VIEW)
  99. {
  100. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  101. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  102. if (dirtyRects.size() == 1 &&
  103. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  104. {
  105. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  106. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  107. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  108. }
  109. else
  110. {
  111. // Update just the dirty rectangles.
  112. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  113. for (; i != dirtyRects.end(); ++i)
  114. {
  115. const CefRect& rect = *i;
  116. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  117. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  118. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  119. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  120. buffer);
  121. }
  122. }
  123. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  124. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  125. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  126. glBindTexture(GL_TEXTURE_2D, 0);
  127. }
  128. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  129. {
  130. int skip_pixels = 0, x = popupRect_.x;
  131. int skip_rows = 0, y = popupRect_.y;
  132. int w = width;
  133. int h = height;
  134. int viewwidth = webTexture2D_->GetWidth();
  135. int viewheight = webTexture2D_->GetHeight();
  136. // Adjust the popup to fit inside the view.
  137. if (x < 0)
  138. {
  139. skip_pixels = -x;
  140. x = 0;
  141. }
  142. if (y < 0)
  143. {
  144. skip_rows = -y;
  145. y = 0;
  146. }
  147. if (x + w > viewwidth)
  148. {
  149. w -= x + w - viewwidth;
  150. }
  151. if (y + h > viewheight)
  152. {
  153. h -= y + h - viewheight;
  154. }
  155. glBindTexture(GL_TEXTURE_2D, (GLuint) webTexture2D_->GetTexture2D()->GetGPUObjectName());
  156. // Update the popup rectangle.
  157. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  158. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  159. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  160. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  161. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  162. glBindTexture(GL_TEXTURE_2D, 0);
  163. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  164. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  165. }
  166. glDisable(GL_TEXTURE_2D);
  167. }
  168. #else
  169. void D3D9Blit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false)
  170. {
  171. #ifndef ATOMIC_D3D11
  172. #ifndef ATOMIC_OPENGL
  173. RECT d3dRect;
  174. d3dRect.left = dstRect.left_;
  175. d3dRect.top = dstRect.top_;
  176. d3dRect.right = dstRect.right_;
  177. d3dRect.bottom = dstRect.bottom_;
  178. int level = 0;
  179. DWORD flags = discard ? D3DLOCK_DISCARD : 0;
  180. D3DLOCKED_RECT d3dLockedRect;
  181. IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject();
  182. if (!object || FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  183. {
  184. ATOMIC_LOGERROR("WebTexture2D - Could not lock texture");
  185. return;
  186. }
  187. int width = dstRect.Width();
  188. int height = dstRect.Height();
  189. for (int j = 0; j < height; ++j)
  190. {
  191. unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch;
  192. memcpy(dst, src, width * 4);
  193. src += srcStride;
  194. }
  195. object->UnlockRect(level);
  196. #endif
  197. #endif
  198. }
  199. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  200. const void *buffer, int width, int height) OVERRIDE
  201. {
  202. #ifndef ATOMIC_D3D11
  203. #ifndef ATOMIC_OPENGL
  204. if (type == PET_VIEW)
  205. {
  206. if (dirtyRects.size() == 1 &&
  207. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  208. {
  209. D3D9Blit(IntRect(0, 0, width, height), (unsigned char*) buffer, width * 4, true);
  210. return;
  211. }
  212. // Update just the dirty rectangles
  213. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  214. for (; i != dirtyRects.end(); ++i)
  215. {
  216. const CefRect& rect = *i;
  217. unsigned char* src = (unsigned char*) buffer;
  218. src += rect.y * (width * 4) + (rect.x * 4);
  219. D3D9Blit(IntRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height), src, width * 4, false);
  220. }
  221. }
  222. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  223. {
  224. int x = popupRect_.x;
  225. int y = popupRect_.y;
  226. int w = width;
  227. int h = height;
  228. int viewwidth = webTexture2D_->GetWidth();
  229. int viewheight = webTexture2D_->GetHeight();
  230. // Adjust the popup to fit inside the view.
  231. if (x < 0)
  232. {
  233. x = 0;
  234. }
  235. if (y < 0)
  236. {
  237. y = 0;
  238. }
  239. if (x + w > viewwidth)
  240. {
  241. w -= x + w - viewwidth;
  242. }
  243. if (y + h > viewheight)
  244. {
  245. h -= y + h - viewheight;
  246. }
  247. unsigned char* src = (unsigned char*) buffer;
  248. D3D9Blit(IntRect(x, y, x + w, y + h), src, width * 4, false);
  249. }
  250. #endif
  251. #endif
  252. }
  253. #endif
  254. private:
  255. CefRect popupRect_;
  256. CefRect popupRectOriginal_;
  257. WeakPtr<WebTexture2D> webTexture2D_;
  258. };
  259. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context), clearColor_(Color::WHITE)
  260. {
  261. d_ = new WebTexture2DPrivate(this);
  262. d_->AddRef();
  263. texture_ = new Texture2D(context_);
  264. texture_->SetNumLevels(1);
  265. texture_->SetFilterMode(FILTER_NEAREST);
  266. }
  267. WebTexture2D::~WebTexture2D()
  268. {
  269. //d_->Release();
  270. }
  271. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  272. {
  273. return d_;
  274. }
  275. int WebTexture2D::GetWidth() const
  276. {
  277. return texture_->GetWidth();
  278. }
  279. int WebTexture2D::GetHeight() const
  280. {
  281. return texture_->GetHeight();
  282. }
  283. void WebTexture2D::SetSize(int width, int height)
  284. {
  285. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  286. return;
  287. // initialize to white (color should probably be an option)
  288. if (texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC))
  289. {
  290. SharedArrayPtr<unsigned> cleardata(new unsigned[width * height]);
  291. unsigned color = clearColor_.ToUInt();
  292. unsigned* ptr = cleardata.Get();
  293. for (unsigned i = 0; i < width * height; i++, ptr++)
  294. {
  295. *ptr = color;
  296. }
  297. texture_->SetData(0, 0, 0, width, height, cleardata.Get());
  298. }
  299. else
  300. {
  301. ATOMIC_LOGERRORF("Unable to set WebTexture2D size to %i x %i", width, height);
  302. }
  303. }
  304. }