skia_utils_win.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SKIA_EXT_SKIA_UTILS_WIN_H_
  5. #define SKIA_EXT_SKIA_UTILS_WIN_H_
  6. #include <vector>
  7. #include "base/win/scoped_gdi_object.h"
  8. #include "third_party/skia/include/core/SkBitmap.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "third_party/skia/include/core/SkImageInfo.h"
  11. #include "third_party/skia/include/core/SkMatrix.h"
  12. #include "third_party/skia/include/core/SkRefCnt.h"
  13. #include "build/build_config.h"
  14. #include <windows.h>
  15. struct SkIRect;
  16. struct SkPoint;
  17. struct SkRect;
  18. class SkSurface;
  19. typedef unsigned long DWORD;
  20. typedef DWORD COLORREF;
  21. typedef struct tagPOINT POINT;
  22. typedef struct tagRECT RECT;
  23. namespace skia {
  24. // Converts a Skia point to a Windows POINT.
  25. POINT SkPointToPOINT(const SkPoint& point);
  26. // Converts a Windows RECT to a Skia rect.
  27. SkRect RECTToSkRect(const RECT& rect);
  28. // Converts a Windows RECT to a Skia rect.
  29. // Both use same in-memory format. Verified by static_assert in
  30. // skia_utils_win.cc.
  31. inline const SkIRect& RECTToSkIRect(const RECT& rect) {
  32. return reinterpret_cast<const SkIRect&>(rect);
  33. }
  34. // Converts a Skia rect to a Windows RECT.
  35. // Both use same in-memory format. Verified by static_assert in
  36. // skia_utils_win.cc.
  37. inline const RECT& SkIRectToRECT(const SkIRect& rect) {
  38. return reinterpret_cast<const RECT&>(rect);
  39. }
  40. // Converts COLORREFs (0BGR) to the ARGB layout Skia expects.
  41. SK_API SkColor COLORREFToSkColor(COLORREF color);
  42. // Converts ARGB to COLORREFs (0BGR).
  43. SK_API COLORREF SkColorToCOLORREF(SkColor color);
  44. // Initializes the default settings and colors in a device context.
  45. SK_API void InitializeDC(HDC context);
  46. // Converts scale, skew, and translation to Windows format and sets it on the
  47. // HDC.
  48. SK_API void LoadTransformToDC(HDC dc, const SkMatrix& matrix);
  49. // Copies |src_rect| from source into destination.
  50. // Takes a potentially-slower path if |is_opaque| is false.
  51. // Sets |transform| on source afterwards!
  52. SK_API void CopyHDC(HDC source, HDC destination, int x, int y, bool is_opaque,
  53. const RECT& src_rect, const SkMatrix& transform);
  54. // Creates a surface writing to the pixels backing |context|'s bitmap.
  55. // Returns null on error.
  56. SK_API sk_sp<SkSurface> MapPlatformSurface(HDC context);
  57. // Creates a bitmap backed by the same pixels backing the HDC's bitmap.
  58. // Returns an empty bitmap on error. The HDC's bitmap is assumed to be formatted
  59. // as 32-bits-per-pixel XRGB8888, as created by CreateHBitmapXRGB8888().
  60. SK_API SkBitmap MapPlatformBitmap(HDC context);
  61. // Fills in a BITMAPINFOHEADER structure to hold the pixel data from |bitmap|.
  62. // The |bitmap| must be have a color type of kN32_SkColorType, and the header
  63. // will be for a bitmap with 32-bits-per-pixel RGB data (the high bits are
  64. // unused in each pixel).
  65. SK_API void CreateBitmapHeaderForN32SkBitmap(const SkBitmap& bitmap,
  66. BITMAPINFOHEADER* hdr);
  67. // Creates a globally allocated memory containing the given byte array. The
  68. // returned handle to the global memory is allocated by ::GlobalAlloc(), and
  69. // must be explicitly freed by ::GlobalFree(), unless ownership is passed to the
  70. // Win32 API. On failure, it returns null.
  71. SK_API HGLOBAL
  72. CreateHGlobalForByteArray(const std::vector<unsigned char>& byte_array);
  73. // Creates an HBITMAP backed by 32-bits-per-pixel RGB data (the high bits are
  74. // unused in each pixel), with dimensions and the RGBC pixel data from the
  75. // SkBitmap. Any alpha channel values are copied into the HBITMAP but are not
  76. // used. Can return a null HBITMAP on any failure to create the HBITMAP.
  77. SK_API base::win::ScopedBitmap CreateHBitmapFromN32SkBitmap(
  78. const SkBitmap& bitmap);
  79. // Creates an image in the DIBV5 format. On success this function returns a
  80. // handle to an allocated memory block containing a DIBV5 header followed by the
  81. // pixel data. If the bitmap creation fails, it returns null. This is preferred
  82. // in some cases over the HBITMAP format because it handles transparency better.
  83. // The returned handle to the global memory is allocated by ::GlobalAlloc(), and
  84. // must be explicitly freed by ::GlobalFree(), unless ownership is passed to the
  85. // Win32 API.
  86. SK_API HGLOBAL CreateDIBV5ImageDataFromN32SkBitmap(const SkBitmap& bitmap);
  87. // Fills in a BITMAPINFOHEADER structure given the bitmap's size. The header
  88. // will be for a bitmap with 32-bits-per-pixel RGB data (the high bits are
  89. // unused in each pixel).
  90. SK_API void CreateBitmapHeaderForXRGB888(int width,
  91. int height,
  92. BITMAPINFOHEADER* hdr);
  93. // Creates an HBITMAP backed by 32-bits-per-pixel RGB data (the high bits are
  94. // unused in each pixel).
  95. SK_API base::win::ScopedBitmap CreateHBitmapXRGB8888(
  96. int width,
  97. int height,
  98. HANDLE shared_section = nullptr,
  99. void** data = nullptr);
  100. } // namespace skia
  101. #endif // SKIA_EXT_SKIA_UTILS_WIN_H_