PaintingSurface.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibGfx/PaintingSurface.h>
  8. #include <LibGfx/SkiaUtils.h>
  9. #include <core/SkColorSpace.h>
  10. #include <core/SkSurface.h>
  11. #include <gpu/GrBackendSurface.h>
  12. #include <gpu/GrDirectContext.h>
  13. #include <gpu/ganesh/SkSurfaceGanesh.h>
  14. #ifdef AK_OS_MACOS
  15. # include <gpu/ganesh/mtl/GrMtlBackendSurface.h>
  16. #endif
  17. namespace Gfx {
  18. struct PaintingSurface::Impl {
  19. IntSize size;
  20. sk_sp<SkSurface> surface;
  21. RefPtr<Bitmap> bitmap;
  22. RefPtr<SkiaBackendContext> context;
  23. };
  24. NonnullRefPtr<PaintingSurface> PaintingSurface::create_with_size(RefPtr<SkiaBackendContext> context, Gfx::IntSize size, Gfx::BitmapFormat color_type, Gfx::AlphaType alpha_type)
  25. {
  26. auto sk_color_type = to_skia_color_type(color_type);
  27. auto sk_alpha_type = alpha_type == Gfx::AlphaType::Premultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  28. auto image_info = SkImageInfo::Make(size.width(), size.height(), sk_color_type, sk_alpha_type);
  29. if (!context) {
  30. auto bitmap = Gfx::Bitmap::create(color_type, alpha_type, size).value();
  31. auto surface = SkSurfaces::WrapPixels(image_info, bitmap->begin(), bitmap->pitch());
  32. VERIFY(surface);
  33. return adopt_ref(*new PaintingSurface(make<Impl>(size, surface, bitmap, context)));
  34. }
  35. auto surface = SkSurfaces::RenderTarget(context->sk_context(), skgpu::Budgeted::kNo, image_info);
  36. VERIFY(surface);
  37. return adopt_ref(*new PaintingSurface(make<Impl>(size, surface, nullptr, context)));
  38. }
  39. NonnullRefPtr<PaintingSurface> PaintingSurface::wrap_bitmap(Bitmap& bitmap)
  40. {
  41. auto color_type = to_skia_color_type(bitmap.format());
  42. auto alpha_type = bitmap.alpha_type() == Gfx::AlphaType::Premultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  43. auto size = bitmap.size();
  44. auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type);
  45. auto surface = SkSurfaces::WrapPixels(image_info, bitmap.begin(), bitmap.pitch());
  46. return adopt_ref(*new PaintingSurface(make<Impl>(size, surface, bitmap, nullptr)));
  47. }
  48. #ifdef AK_OS_MACOS
  49. NonnullRefPtr<PaintingSurface> PaintingSurface::wrap_iosurface(Core::IOSurfaceHandle const& iosurface_handle, RefPtr<SkiaBackendContext> context)
  50. {
  51. auto metal_texture = context->metal_context().create_texture_from_iosurface(iosurface_handle);
  52. IntSize const size { metal_texture->width(), metal_texture->height() };
  53. auto image_info = SkImageInfo::Make(size.width(), size.height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType);
  54. GrMtlTextureInfo mtl_info;
  55. mtl_info.fTexture = sk_ret_cfp(metal_texture->texture());
  56. auto backend_render_target = GrBackendRenderTargets::MakeMtl(metal_texture->width(), metal_texture->height(), mtl_info);
  57. auto surface = SkSurfaces::WrapBackendRenderTarget(context->sk_context(), backend_render_target, kTopLeft_GrSurfaceOrigin, kBGRA_8888_SkColorType, nullptr, nullptr);
  58. return adopt_ref(*new PaintingSurface(make<Impl>(size, surface, nullptr, context)));
  59. }
  60. #endif
  61. PaintingSurface::PaintingSurface(NonnullOwnPtr<Impl>&& impl)
  62. : m_impl(move(impl))
  63. {
  64. }
  65. PaintingSurface::~PaintingSurface() = default;
  66. void PaintingSurface::read_into_bitmap(Gfx::Bitmap& bitmap)
  67. {
  68. auto color_type = to_skia_color_type(bitmap.format());
  69. auto alpha_type = bitmap.alpha_type() == Gfx::AlphaType::Premultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  70. auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type);
  71. SkPixmap const pixmap(image_info, bitmap.begin(), bitmap.pitch());
  72. m_impl->surface->readPixels(pixmap, 0, 0);
  73. }
  74. void PaintingSurface::write_from_bitmap(Gfx::Bitmap& bitmap)
  75. {
  76. auto color_type = to_skia_color_type(bitmap.format());
  77. auto alpha_type = bitmap.alpha_type() == Gfx::AlphaType::Premultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
  78. auto image_info = SkImageInfo::Make(bitmap.width(), bitmap.height(), color_type, alpha_type);
  79. SkPixmap const pixmap(image_info, bitmap.begin(), bitmap.pitch());
  80. m_impl->surface->writePixels(pixmap, 0, 0);
  81. }
  82. IntSize PaintingSurface::size() const
  83. {
  84. return m_impl->size;
  85. }
  86. IntRect PaintingSurface::rect() const
  87. {
  88. return { {}, m_impl->size };
  89. }
  90. SkCanvas& PaintingSurface::canvas() const
  91. {
  92. return *m_impl->surface->getCanvas();
  93. }
  94. SkSurface& PaintingSurface::sk_surface() const
  95. {
  96. return *m_impl->surface;
  97. }
  98. void PaintingSurface::notify_content_will_change()
  99. {
  100. m_impl->surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
  101. }
  102. template<>
  103. sk_sp<SkImage> PaintingSurface::sk_image_snapshot() const
  104. {
  105. return m_impl->surface->makeImageSnapshot();
  106. }
  107. void PaintingSurface::flush() const
  108. {
  109. if (auto context = m_impl->context) {
  110. context->flush_and_submit(m_impl->surface.get());
  111. }
  112. }
  113. }