• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkSurface.h"
9 #include "src/base/SkAutoMalloc.h"
10 #include "tools/window/RasterWindowContext.h"
11 #include "tools/window/win/WindowContextFactory_win.h"
12 
13 #include <Windows.h>
14 
15 using skwindow::DisplayParams;
16 using skwindow::internal::RasterWindowContext;
17 
18 namespace {
19 
20 class RasterWindowContext_win : public RasterWindowContext {
21 public:
22     RasterWindowContext_win(HWND, const DisplayParams&);
23 
24     sk_sp<SkSurface> getBackbufferSurface() override;
isValid()25     bool isValid() override { return SkToBool(fWnd); }
26     void resize(int w, int h) override;
27     void setDisplayParams(const DisplayParams& params) override;
28 
29 protected:
30     void onSwapBuffers() override;
31 
32     SkAutoMalloc fSurfaceMemory;
33     sk_sp<SkSurface> fBackbufferSurface;
34     HWND fWnd;
35 };
36 
RasterWindowContext_win(HWND wnd,const DisplayParams & params)37 RasterWindowContext_win::RasterWindowContext_win(HWND wnd, const DisplayParams& params)
38     : RasterWindowContext(params)
39     , fWnd(wnd) {
40     RECT rect;
41     GetClientRect(wnd, &rect);
42     this->resize(rect.right - rect.left, rect.bottom - rect.top);
43 }
44 
setDisplayParams(const DisplayParams & params)45 void RasterWindowContext_win::setDisplayParams(const DisplayParams& params) {
46     fDisplayParams = params;
47     RECT rect;
48     GetClientRect(fWnd, &rect);
49     this->resize(rect.right - rect.left, rect.bottom - rect.top);
50 }
51 
resize(int w,int h)52 void RasterWindowContext_win::resize(int w, int h) {
53     fWidth = w;
54     fHeight = h;
55     fBackbufferSurface.reset();
56     const size_t bmpSize = sizeof(BITMAPINFOHEADER) + w * h * sizeof(uint32_t);
57     fSurfaceMemory.reset(bmpSize);
58     BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
59     ZeroMemory(bmpInfo, sizeof(BITMAPINFO));
60     bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
61     bmpInfo->bmiHeader.biWidth = w;
62     bmpInfo->bmiHeader.biHeight = -h; // negative means top-down bitmap. Skia draws top-down.
63     bmpInfo->bmiHeader.biPlanes = 1;
64     bmpInfo->bmiHeader.biBitCount = 32;
65     bmpInfo->bmiHeader.biCompression = BI_RGB;
66     void* pixels = bmpInfo->bmiColors;
67 
68     SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
69                                          fDisplayParams.fColorSpace);
70     fBackbufferSurface = SkSurfaces::WrapPixels(info, pixels, sizeof(uint32_t) * w);
71 }
72 
getBackbufferSurface()73 sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackbufferSurface; }
74 
onSwapBuffers()75 void RasterWindowContext_win::onSwapBuffers() {
76     BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
77     HDC dc = GetDC(fWnd);
78     StretchDIBits(dc, 0, 0, fWidth, fHeight, 0, 0, fWidth, fHeight, bmpInfo->bmiColors, bmpInfo,
79                   DIB_RGB_COLORS, SRCCOPY);
80     ReleaseDC(fWnd, dc);
81 }
82 
83 }  // anonymous namespace
84 
85 namespace skwindow {
86 
MakeRasterForWin(HWND wnd,const DisplayParams & params)87 std::unique_ptr<WindowContext> MakeRasterForWin(HWND wnd, const DisplayParams& params) {
88     std::unique_ptr<WindowContext> ctx(new RasterWindowContext_win(wnd, params));
89     if (!ctx->isValid()) {
90         ctx = nullptr;
91     }
92     return ctx;
93 }
94 
95 }  // namespace skwindow
96