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