Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RasterWindowContext_win.cpp
Go to the documentation of this file.
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
12
13#include <Windows.h>
14
17
18namespace {
19
20class RasterWindowContext_win : public RasterWindowContext {
21public:
22 RasterWindowContext_win(HWND, const DisplayParams&);
23
25 bool isValid() override { return SkToBool(fWnd); }
26 void resize(int w, int h) override;
27 void setDisplayParams(const DisplayParams& params) override;
28
29protected:
30 void onSwapBuffers() override;
31
32 SkAutoMalloc fSurfaceMemory;
33 sk_sp<SkSurface> fBackbufferSurface;
34 HWND fWnd;
35};
36
37RasterWindowContext_win::RasterWindowContext_win(HWND wnd, const DisplayParams& params)
39 , fWnd(wnd) {
40 RECT rect;
41 GetClientRect(wnd, &rect);
42 this->resize(rect.right - rect.left, rect.bottom - rect.top);
43}
44
45void 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
52void 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
73sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackbufferSurface; }
74
75void 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
85namespace skwindow {
86
87std::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
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
virtual bool isValid()=0
virtual sk_sp< SkSurface > getBackbufferSurface()=0
virtual void setDisplayParams(const DisplayParams &params)=0
virtual void resize(int w, int h)=0
virtual void onSwapBuffers()=0
const EmbeddedViewParams * params
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
std::unique_ptr< WindowContext > MakeRasterForWin(HWND wnd, const DisplayParams &params)
SkScalar w
SkScalar h
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)