| 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 <ApplicationServices/ApplicationServices.h> | |
| 8 #include <Cocoa/Cocoa.h> | |
| 9 #include <CoreFoundation/CoreFoundation.h> | |
| 10 | |
| 11 #include "third_party/libyuv/include/libyuv/convert_argb.h" | |
| 12 | |
| 13 gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) { | |
| 14 DCHECK(id.type == content::DesktopMediaID::TYPE_WINDOW); | |
| 15 | |
| 16 CGWindowID ids[1]; | |
| 17 ids[0] = id.id; | |
| 18 CFArrayRef window_id_array = | |
| 19 CFArrayCreate(nullptr, reinterpret_cast<const void**>(&ids), 1, nullptr); | |
| 20 CFArrayRef window_array = | |
| 21 CGWindowListCreateDescriptionFromArray(window_id_array); | |
| 22 if (!window_array || 0 == CFArrayGetCount(window_array)) { | |
| 23 return gfx::ImageSkia(); | |
| 24 } | |
| 25 | |
| 26 CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>( | |
| 27 CFArrayGetValueAtIndex(window_array, 0)); | |
| 28 CFNumberRef pid_ref = reinterpret_cast<CFNumberRef>( | |
| 29 CFDictionaryGetValue(window, kCGWindowOwnerPID)); | |
| 30 | |
| 31 int pid; | |
| 32 CFNumberGetValue(pid_ref, kCFNumberIntType, &pid); | |
| 33 | |
| 34 NSImage* icon_image = | |
| 35 [[NSRunningApplication runningApplicationWithProcessIdentifier:pid] icon]; | |
| 36 | |
| 37 int width = [icon_image size].width; | |
| 38 int height = [icon_image size].height; | |
| 39 | |
| 40 CGImageRef cg_icon_image = | |
| 41 [icon_image CGImageForProposedRect:nil context:nil hints:nil]; | |
| 42 | |
| 43 int bits_per_pixel = CGImageGetBitsPerPixel(cg_icon_image); | |
| 44 if (bits_per_pixel != 32) { | |
| 45 return gfx::ImageSkia(); | |
| 46 } | |
| 47 | |
| 48 CGDataProviderRef provider = CGImageGetDataProvider(cg_icon_image); | |
| 49 CFDataRef cf_data = CGDataProviderCopyData(provider); | |
| 50 | |
| 51 int src_stride = CGImageGetBytesPerRow(cg_icon_image); | |
| 52 const uint8_t* src_data = CFDataGetBytePtr(cf_data); | |
| 53 | |
| 54 SkBitmap result; | |
| 55 result.allocN32Pixels(width, height, false); | |
| 56 result.lockPixels(); | |
| 57 | |
| 58 uint8_t* pixels_data = reinterpret_cast<uint8_t*>(result.getPixels()); | |
| 59 | |
| 60 libyuv::ABGRToARGB(src_data, src_stride, pixels_data, result.rowBytes(), | |
| 61 width, height); | |
| 62 | |
| 63 CFRelease(cf_data); | |
| 64 | |
| 65 return gfx::ImageSkia::CreateFrom1xBitmap(result); | |
| 66 } | |
| OLD | NEW |