RasterWindowContext_win.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkSurface.h"
  8. #include "src/core/SkAutoMalloc.h"
  9. #include "tools/sk_app/RasterWindowContext.h"
  10. #include "tools/sk_app/win/WindowContextFactory_win.h"
  11. #include <Windows.h>
  12. using sk_app::RasterWindowContext;
  13. using sk_app::DisplayParams;
  14. namespace {
  15. class RasterWindowContext_win : public RasterWindowContext {
  16. public:
  17. RasterWindowContext_win(HWND, const DisplayParams&);
  18. sk_sp<SkSurface> getBackbufferSurface() override;
  19. void swapBuffers() override;
  20. bool isValid() override { return SkToBool(fWnd); }
  21. void resize(int w, int h) override;
  22. void setDisplayParams(const DisplayParams& params) override;
  23. protected:
  24. SkAutoMalloc fSurfaceMemory;
  25. sk_sp<SkSurface> fBackbufferSurface;
  26. HWND fWnd;
  27. private:
  28. typedef RasterWindowContext INHERITED;
  29. };
  30. RasterWindowContext_win::RasterWindowContext_win(HWND wnd, const DisplayParams& params)
  31. : INHERITED(params)
  32. , fWnd(wnd) {
  33. RECT rect;
  34. GetClientRect(wnd, &rect);
  35. this->resize(rect.right - rect.left, rect.bottom - rect.top);
  36. }
  37. void RasterWindowContext_win::setDisplayParams(const DisplayParams& params) {
  38. fDisplayParams = params;
  39. RECT rect;
  40. GetClientRect(fWnd, &rect);
  41. this->resize(rect.right - rect.left, rect.bottom - rect.top);
  42. }
  43. void RasterWindowContext_win::resize(int w, int h) {
  44. fWidth = w;
  45. fHeight = h;
  46. fBackbufferSurface.reset();
  47. const size_t bmpSize = sizeof(BITMAPINFOHEADER) + w * h * sizeof(uint32_t);
  48. fSurfaceMemory.reset(bmpSize);
  49. BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
  50. ZeroMemory(bmpInfo, sizeof(BITMAPINFO));
  51. bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  52. bmpInfo->bmiHeader.biWidth = w;
  53. bmpInfo->bmiHeader.biHeight = -h; // negative means top-down bitmap. Skia draws top-down.
  54. bmpInfo->bmiHeader.biPlanes = 1;
  55. bmpInfo->bmiHeader.biBitCount = 32;
  56. bmpInfo->bmiHeader.biCompression = BI_RGB;
  57. void* pixels = bmpInfo->bmiColors;
  58. SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
  59. fDisplayParams.fColorSpace);
  60. fBackbufferSurface = SkSurface::MakeRasterDirect(info, pixels, sizeof(uint32_t) * w);
  61. }
  62. sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackbufferSurface; }
  63. void RasterWindowContext_win::swapBuffers() {
  64. BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
  65. HDC dc = GetDC(fWnd);
  66. StretchDIBits(dc, 0, 0, fWidth, fHeight, 0, 0, fWidth, fHeight, bmpInfo->bmiColors, bmpInfo,
  67. DIB_RGB_COLORS, SRCCOPY);
  68. ReleaseDC(fWnd, dc);
  69. }
  70. } // anonymous namespace
  71. namespace sk_app {
  72. namespace window_context_factory {
  73. WindowContext* NewRasterForWin(HWND wnd, const DisplayParams& params) {
  74. WindowContext* ctx = new RasterWindowContext_win(wnd, params);
  75. if (!ctx->isValid()) {
  76. delete ctx;
  77. ctx = nullptr;
  78. }
  79. return ctx;
  80. }
  81. } // namespace window_context_factory
  82. } // namespace sk_app