get_current_monitor_profile.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "include/core/SkStream.h"
  8. #if defined(SK_BUILD_FOR_MAC)
  9. #include <ApplicationServices/ApplicationServices.h>
  10. #elif defined(SK_BUILD_FOR_WIN)
  11. #include <windows.h>
  12. #endif
  13. int main(int argc, char** argv) {
  14. #if defined(SK_BUILD_FOR_MAC)
  15. CGColorSpaceRef cs = CGDisplayCopyColorSpace(CGMainDisplayID());
  16. CFDataRef dataRef = CGColorSpaceCopyICCProfile(cs);
  17. const uint8_t* data = CFDataGetBytePtr(dataRef);
  18. size_t size = CFDataGetLength(dataRef);
  19. SkFILEWStream file("monitor_0.icc");
  20. file.write(data, size);
  21. CFRelease(cs);
  22. CFRelease(dataRef);
  23. return 0;
  24. #elif defined(SK_BUILD_FOR_WIN)
  25. DISPLAY_DEVICE dd = { sizeof(DISPLAY_DEVICE) };
  26. SkString outputFilename;
  27. // Chrome's code for this currently just gets the primary monitor's profile. This code iterates
  28. // over all attached monitors, so it's "better" in that sense. Making intelligent use of this
  29. // information (via things like MonitorFromWindow or MonitorFromRect to pick the correct
  30. // profile for a particular window or region of a window), is an exercise left to the reader.
  31. for (int i = 0; EnumDisplayDevices(NULL, i, &dd, 0); ++i) {
  32. if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) {
  33. // There are other helpful things in dd at this point:
  34. // dd.DeviceString has a longer name for the adapter
  35. // dd.StateFlags indicates primary display, mirroring, etc...
  36. HDC dc = CreateDC(NULL, dd.DeviceName, NULL, NULL);
  37. if (dc) {
  38. char icmPath[MAX_PATH + 1];
  39. DWORD pathLength = MAX_PATH;
  40. if (GetICMProfile(dc, &pathLength, icmPath)) {
  41. // GetICMProfile just returns the path to the installed profile (not the data)
  42. outputFilename = SkStringPrintf("monitor_%d.icc", i);
  43. CopyFile(icmPath, outputFilename.c_str(), FALSE);
  44. }
  45. DeleteDC(dc);
  46. }
  47. }
  48. }
  49. return 0;
  50. #else
  51. SkDebugf("ERROR: Unsupported platform\n");
  52. return 1;
  53. #endif
  54. }