OLD | NEW |
(Empty) | |
| 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 "WindowContextFactory_win.h" |
| 9 #include "../RasterWindowContext.h" |
| 10 #include "SkSurface.h" |
| 11 |
| 12 #include <Windows.h> |
| 13 |
| 14 using sk_app::RasterWindowContext; |
| 15 using sk_app::DisplayParams; |
| 16 |
| 17 namespace { |
| 18 |
| 19 class RasterWindowContext_win : public RasterWindowContext { |
| 20 public: |
| 21 RasterWindowContext_win(HWND, const DisplayParams&); |
| 22 |
| 23 sk_sp<SkSurface> getBackbufferSurface() override; |
| 24 void swapBuffers() override; |
| 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 SkAutoMalloc fSurfaceMemory; |
| 31 sk_sp<SkSurface> fBackbufferSurface; |
| 32 HWND fWnd; |
| 33 }; |
| 34 |
| 35 RasterWindowContext_win::RasterWindowContext_win(HWND wnd, const DisplayParams&
params) |
| 36 : fWnd(wnd) { |
| 37 fDisplayParams = params; |
| 38 RECT rect; |
| 39 GetWindowRect(wnd, &rect); |
| 40 this->resize(rect.right - rect.left, rect.bottom - rect.top); |
| 41 } |
| 42 |
| 43 void RasterWindowContext_win::setDisplayParams(const DisplayParams& params) { |
| 44 fDisplayParams = params; |
| 45 RECT rect; |
| 46 GetWindowRect(fWnd, &rect); |
| 47 this->resize(rect.right - rect.left, rect.bottom - rect.top); |
| 48 } |
| 49 |
| 50 void RasterWindowContext_win::resize(int w, int h) { |
| 51 fWidth = w; |
| 52 fHeight = h; |
| 53 fBackbufferSurface.reset(); |
| 54 const size_t bmpSize = sizeof(BITMAPINFOHEADER) + w * h * sizeof(uint32_t); |
| 55 fSurfaceMemory.reset(bmpSize); |
| 56 BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get()); |
| 57 ZeroMemory(bmpInfo, sizeof(BITMAPINFO)); |
| 58 bmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 59 bmpInfo->bmiHeader.biWidth = w; |
| 60 bmpInfo->bmiHeader.biHeight = -h; // negative means top-down bitmap. Skia dr
aws top-down. |
| 61 bmpInfo->bmiHeader.biPlanes = 1; |
| 62 bmpInfo->bmiHeader.biBitCount = 32; |
| 63 bmpInfo->bmiHeader.biCompression = BI_RGB; |
| 64 void* pixels = bmpInfo->bmiColors; |
| 65 |
| 66 SkImageInfo info = SkImageInfo::Make(w, h, fDisplayParams.fColorType, kPremu
l_SkAlphaType, |
| 67 fDisplayParams.fColorSpace); |
| 68 fBackbufferSurface = SkSurface::MakeRasterDirect(info, pixels, sizeof(uint32
_t) * w); |
| 69 } |
| 70 |
| 71 sk_sp<SkSurface> RasterWindowContext_win::getBackbufferSurface() { return fBackb
ufferSurface; } |
| 72 |
| 73 void RasterWindowContext_win::swapBuffers() { |
| 74 BITMAPINFO* bmpInfo = reinterpret_cast<BITMAPINFO*>(fSurfaceMemory.get()); |
| 75 HDC dc = GetDC(fWnd); |
| 76 StretchDIBits(dc, 0, 0, fWidth, fHeight, 0, 0, fWidth, fHeight, bmpInfo->bmi
Colors, bmpInfo, |
| 77 DIB_RGB_COLORS, SRCCOPY); |
| 78 ReleaseDC(fWnd, dc); |
| 79 } |
| 80 |
| 81 } // anonymous namespace |
| 82 |
| 83 namespace sk_app { |
| 84 namespace window_context_factory { |
| 85 |
| 86 WindowContext* NewRasterForWin(HWND wnd, const DisplayParams& params) { |
| 87 WindowContext* ctx = new RasterWindowContext_win(wnd, params); |
| 88 if (!ctx->isValid()) { |
| 89 delete ctx; |
| 90 ctx = nullptr; |
| 91 } |
| 92 return ctx; |
| 93 } |
| 94 |
| 95 } // namespace window_context_factory |
| 96 } // namespace sk_app |
OLD | NEW |