2016-07-27 15:50:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "tools/sk_app/RasterWindowContext.h"
|
|
|
|
#include "tools/sk_app/win/WindowContextFactory_win.h"
|
2016-07-27 15:50:12 +00:00
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
using sk_app::RasterWindowContext;
|
|
|
|
using sk_app::DisplayParams;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RasterWindowContext_win : public RasterWindowContext {
|
|
|
|
public:
|
|
|
|
RasterWindowContext_win(HWND, const DisplayParams&);
|
|
|
|
|
|
|
|
sk_sp<SkSurface> getBackbufferSurface() override;
|
|
|
|
void swapBuffers() override;
|
|
|
|
bool isValid() override { return SkToBool(fWnd); }
|
|
|
|
void resize(int w, int h) override;
|
|
|
|
void setDisplayParams(const DisplayParams& params) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkAutoMalloc fSurfaceMemory;
|
|
|
|
sk_sp<SkSurface> fBackbufferSurface;
|
|
|
|
HWND fWnd;
|
2017-05-02 20:15:53 +00:00
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = RasterWindowContext;
|
2016-07-27 15:50:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RasterWindowContext_win::RasterWindowContext_win(HWND wnd, const DisplayParams& params)
|
2017-05-02 20:15:53 +00:00
|
|
|
: INHERITED(params)
|
|
|
|
, fWnd(wnd) {
|
2016-07-27 15:50:12 +00:00
|
|
|
RECT rect;
|
2018-09-21 13:51:08 +00:00
|
|
|
GetClientRect(wnd, &rect);
|
2016-07-27 15:50:12 +00:00
|
|
|
this->resize(rect.right - rect.left, rect.bottom - rect.top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RasterWindowContext_win::setDisplayParams(const DisplayParams& params) {
|
|
|
|
fDisplayParams = params;
|
|
|
|
RECT rect;
|
2018-09-21 13:51:08 +00:00
|
|
|
GetClientRect(fWnd, &rect);
|
2016-07-27 15:50:12 +00:00
|
|
|
this->resize(rect.right - rect.left, rect.bottom - rect.top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RasterWindowContext_win::resize(int w, int h) {
|
|
|
|
fWidth = w;
|
|
|
|
fHeight = h;
|
|
|
|
fBackbufferSurface.reset();
|
|
|
|
const size_t bmpSize = sizeof(BITMAPINFOHEADER) + w * h * sizeof(uint32_t);
|
|
|
|
fSurfaceMemory.reset(bmpSize);
|
|
|
|
BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
|
|
|
|
ZeroMemory(bmpInfo, sizeof(BITMAPINFO));
|
|
|
|
bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
bmpInfo->bmiHeader.biWidth = w;
|
|
|
|
bmpInfo->bmiHeader.biHeight = -h; // negative means top-down bitmap. Skia draws top-down.
|
|
|
|
bmpInfo->bmiHeader.biPlanes = 1;
|
|
|
|
bmpInfo->bmiHeader.biBitCount = 32;
|
|
|
|
bmpInfo->bmiHeader.biCompression = BI_RGB;
|
|
|
|
void* pixels = bmpInfo->bmiColors;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremul_SkAlphaType,
|
|
|
|
fDisplayParams.fColorSpace);
|
|
|
|
fBackbufferSurface = SkSurface::MakeRasterDirect(info, pixels, sizeof(uint32_t) * w);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackbufferSurface; }
|
|
|
|
|
|
|
|
void RasterWindowContext_win::swapBuffers() {
|
|
|
|
BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get());
|
|
|
|
HDC dc = GetDC(fWnd);
|
|
|
|
StretchDIBits(dc, 0, 0, fWidth, fHeight, 0, 0, fWidth, fHeight, bmpInfo->bmiColors, bmpInfo,
|
|
|
|
DIB_RGB_COLORS, SRCCOPY);
|
|
|
|
ReleaseDC(fWnd, dc);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
namespace window_context_factory {
|
|
|
|
|
2019-08-01 20:21:49 +00:00
|
|
|
std::unique_ptr<WindowContext> MakeRasterForWin(HWND wnd, const DisplayParams& params) {
|
|
|
|
std::unique_ptr<WindowContext> ctx(new RasterWindowContext_win(wnd, params));
|
2016-07-27 15:50:12 +00:00
|
|
|
if (!ctx->isValid()) {
|
|
|
|
ctx = nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace window_context_factory
|
|
|
|
} // namespace sk_app
|