WebTexture2D.cpp 2.7 KB

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