| 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, std::unique_ptr<const DisplayParams>); |
| 23 |
|
| 24 |
sk_sp<SkSurface> getBackbufferSurface() override; |
| 25 |
bool isValid() override { return SkToBool(fWnd); } |
| 26 |
void resize(int w, int h) override; |
| 27 |
void setDisplayParams(std::unique_ptr<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 |
|
| 37 |
RasterWindowContext_win::RasterWindowContext_win(HWND wnd, |
| 38 |
std::unique_ptr<const DisplayParams> params) |
| 39 |
: RasterWindowContext(std::move(params)), fWnd(wnd) { |
| 40 |
RECT rect; |
| 41 |
GetClientRect(wnd, &rect); |
| 42 |
this->resize(rect.right - rect.left, rect.bottom - rect.top); |
| 43 |
} |
| 44 |
|
| 45 |
void RasterWindowContext_win::setDisplayParams(std::unique_ptr<const DisplayParams> params) { |
| 46 |
fDisplayParams = std::move(params); |
| 47 |
RECT rect; |
| 48 |
GetClientRect(fWnd, &rect); |
| 49 |
this->resize(rect.right - rect.left, rect.bottom - rect.top); |
| 50 |
} |
| 51 |
|
| 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( |
| 69 |
w, h, fDisplayParams->colorType(), kPremul_SkAlphaType, fDisplayParams->colorSpace()); |
| 70 |
fBackbufferSurface = SkSurfaces::WrapPixels(info, pixels, sizeof(uint32_t) * w); |
| 71 |
} |
| 72 |
|
| 73 |
sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackbufferSurface; } |
| 74 |
|
| 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 |
|
| 87 |
std::unique_ptr<WindowContext> MakeRasterForWin(HWND wnd, |
| 88 |
std::unique_ptr<const DisplayParams> params) { |
| 89 |
std::unique_ptr<WindowContext> ctx(new RasterWindowContext_win(wnd, std::move(params))); |
| 90 |
if (!ctx->isValid()) { |
| 91 |
ctx = nullptr; |
| 92 |
} |
| 93 |
return ctx; |
| 94 |
} |
| 95 |
|
| 96 |
} // namespace skwindow |