WebTexture2D.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <ThirdParty/CEF/include/cef_render_handler.h>
  2. #include <Atomic/Resource/ResourceCache.h>
  3. #include <Atomic/Graphics/Graphics.h>
  4. #include <Atomic/Graphics/Technique.h>
  5. #include "WebTexture2D.h"
  6. namespace Atomic
  7. {
  8. class WebTexture2DPrivate : public CefRenderHandler
  9. {
  10. friend class WebTexture2D;
  11. public:
  12. IMPLEMENT_REFCOUNTING(WebTexture2DPrivate)
  13. WebTexture2DPrivate(WebTexture2D* webTexture2D)
  14. {
  15. webTexture2D_ = webTexture2D;
  16. }
  17. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  18. {
  19. rect = CefRect(0, 0, webTexture2D_->GetCurrentWidth(), webTexture2D_->GetCurrentHeight());
  20. return true;
  21. }
  22. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  23. const void *buffer, int width, int height) OVERRIDE
  24. {
  25. if (type == PET_VIEW)
  26. {
  27. webTexture2D_->texture_->SetData(0, 0, 0, width, height, buffer);
  28. }
  29. /*
  30. if (dirtyRects.size() == 1 && width == )
  31. return;
  32. int vwidth = width;
  33. int vheight = height;
  34. if (vwidth > webTexture2D_->texture_->GetWidth())
  35. vwidth = webTexture2D_->texture_->GetWidth();
  36. if (vheight > webTexture2D_->texture_->GetHeight())
  37. vheight = webTexture2D_->texture_->GetHeight();
  38. */
  39. }
  40. private:
  41. WeakPtr<WebTexture2D> webTexture2D_;
  42. };
  43. WebTexture2D::WebTexture2D(Context* context, int width, int height) : WebRenderHandler(context)
  44. {
  45. d_ = new WebTexture2DPrivate(this);
  46. d_->AddRef();
  47. texture_ = new Texture2D(context_);
  48. texture_->SetNumLevels(1);
  49. texture_->SetFilterMode(FILTER_BILINEAR);
  50. SetCurrentSize(width, height);
  51. ResourceCache* cache = GetSubsystem<ResourceCache>();
  52. material_ = new Material(context_);
  53. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
  54. material_->SetTexture(TU_DIFFUSE, texture_);
  55. }
  56. WebTexture2D::~WebTexture2D()
  57. {
  58. //d_->Release();
  59. }
  60. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  61. {
  62. return d_;
  63. }
  64. void WebTexture2D::SetCurrentSize(unsigned width, unsigned height)
  65. {
  66. currentWidth_ = width;
  67. currentHeight_ = height;
  68. unsigned newMaxWidth = NextPowerOfTwo(width);
  69. unsigned newMaxHeight = NextPowerOfTwo(height);
  70. if (newMaxWidth != maxWidth_ || newMaxHeight != maxHeight_)
  71. {
  72. SetMaxSize(newMaxWidth, newMaxHeight);
  73. }
  74. }
  75. void WebTexture2D::SetMaxSize(unsigned width, unsigned height)
  76. {
  77. maxWidth_ = width;
  78. maxHeight_ = height;
  79. texture_->SetSize(maxWidth_, maxHeight_, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
  80. }
  81. }