Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #include "chrome/browser/media/window_icon_util.h" | |
| 6 | |
| 7 #include <X11/Xatom.h> | |
| 8 #include <X11/Xutil.h> | |
| 9 | |
| 10 #include "ui/gfx/x/x11_types.h" | |
| 11 | |
| 12 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) { | |
| 13 DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW); | |
| 14 | |
| 15 Display* display = gfx::GetXDisplay(); | |
| 16 Atom property = XInternAtom(display, "_NET_WM_ICON", True); | |
| 17 Atom actual_type; | |
| 18 int actual_format; | |
| 19 unsigned long bytes_after; // NOLINT: type required by XGetWindowProperty | |
| 20 unsigned long size; | |
| 21 long* data; | |
| 22 | |
| 23 int status = XGetWindowProperty(display, id.id, property, 0L, ~0L, False, | |
| 24 AnyPropertyType, &actual_type, &actual_format, | |
| 25 &size, &bytes_after, | |
| 26 reinterpret_cast<unsigned char**>(&data)); | |
| 27 if (status != Success) { | |
| 28 return gfx::ImageSkia(); | |
| 29 } | |
| 30 | |
| 31 // The format of |data| is concatenation of sections like | |
| 32 // [width, height, pixel data of size width * height], and the total bytes | |
| 33 // number of |data| is |size|. | |
|
Sergey Ulanov
2016/08/20 04:24:10
Also mention that we want to select the biggest ic
qiangchen
2016/08/22 18:16:44
Done.
| |
| 34 int width = 0; | |
| 35 int height = 0; | |
| 36 int start = 0; | |
| 37 int i = 0; | |
| 38 while (i + 1 < static_cast<int>(size)) { | |
| 39 if ((i == 0 || static_cast<int>(data[i] * data[i + 1]) > width * height) && | |
| 40 (i + 1 + data[i] * data[i + 1] < static_cast<int>(size))) { | |
| 41 width = static_cast<int>(data[i]); | |
| 42 height = static_cast<int>(data[i + 1]); | |
| 43 start = i + 2; | |
| 44 } | |
| 45 i = i + 2 + static_cast<int>(data[i] * data[i + 1]); | |
| 46 } | |
| 47 | |
| 48 SkBitmap result; | |
| 49 SkImageInfo info = SkImageInfo::MakeN32(width, height, kUnpremul_SkAlphaType); | |
| 50 result.allocPixels(info); | |
| 51 result.lockPixels(); | |
| 52 | |
| 53 uint32_t* pixels_data = reinterpret_cast<uint32_t*>(result.getPixels()); | |
| 54 | |
| 55 for (long y = 0; y < height; ++y) { | |
| 56 for (long x = 0; x < width; ++x) { | |
| 57 pixels_data[result.rowBytesAsPixels() * y + x] = | |
| 58 static_cast<uint32_t>(data[start + width * y + x]); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 XFree(data); | |
| 63 return gfx::ImageSkia::CreateFrom1xBitmap(result); | |
| 64 } | |
| OLD | NEW |