OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "ui/gfx/mac/io_surface_manager.h" | |
6 | |
7 #include <IOSurface/IOSurface.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/mac/scoped_cftyperef.h" | |
11 #include "ui/gfx/buffer_format_util.h" | |
12 | |
13 namespace gfx { | |
14 namespace { | |
15 | |
16 IOSurfaceManager* g_instance = NULL; | |
17 | |
18 void AddIntegerValue(CFMutableDictionaryRef dictionary, | |
19 const CFStringRef key, | |
20 int32 value) { | |
21 base::ScopedCFTypeRef<CFNumberRef> number( | |
22 CFNumberCreate(NULL, kCFNumberSInt32Type, &value)); | |
23 CFDictionaryAddValue(dictionary, key, number.get()); | |
24 } | |
25 | |
26 int32 BytesPerElement(gfx::BufferFormat format, int plane) { | |
27 switch (format) { | |
28 case gfx::BufferFormat::R_8: | |
29 DCHECK_EQ(plane, 0); | |
30 return 1; | |
31 case gfx::BufferFormat::BGRA_8888: | |
32 case gfx::BufferFormat::RGBA_8888: | |
33 DCHECK_EQ(plane, 0); | |
34 return 4; | |
35 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
36 static int32 bytes_per_element[] = {1, 2}; | |
37 DCHECK_LT(static_cast<size_t>(plane), arraysize(bytes_per_element)); | |
38 return bytes_per_element[plane]; | |
39 case gfx::BufferFormat::UYVY_422: | |
40 DCHECK_EQ(plane, 0); | |
41 return 2; | |
42 case gfx::BufferFormat::ATC: | |
43 case gfx::BufferFormat::ATCIA: | |
44 case gfx::BufferFormat::DXT1: | |
45 case gfx::BufferFormat::DXT5: | |
46 case gfx::BufferFormat::ETC1: | |
47 case gfx::BufferFormat::RGBA_4444: | |
48 case gfx::BufferFormat::RGBX_8888: | |
49 case gfx::BufferFormat::BGRX_8888: | |
50 case gfx::BufferFormat::YUV_420: | |
51 NOTREACHED(); | |
52 return 0; | |
53 } | |
54 | |
55 NOTREACHED(); | |
56 return 0; | |
57 } | |
58 | |
59 int32 PixelFormat(gfx::BufferFormat format) { | |
60 switch (format) { | |
61 case gfx::BufferFormat::R_8: | |
62 return 'L008'; | |
63 case gfx::BufferFormat::BGRA_8888: | |
64 case gfx::BufferFormat::RGBA_8888: | |
65 return 'BGRA'; | |
66 case gfx::BufferFormat::YUV_420_BIPLANAR: | |
67 return '420v'; | |
68 case gfx::BufferFormat::UYVY_422: | |
69 return '2vuy'; | |
70 case gfx::BufferFormat::ATC: | |
71 case gfx::BufferFormat::ATCIA: | |
72 case gfx::BufferFormat::DXT1: | |
73 case gfx::BufferFormat::DXT5: | |
74 case gfx::BufferFormat::ETC1: | |
75 case gfx::BufferFormat::RGBA_4444: | |
76 case gfx::BufferFormat::RGBX_8888: | |
77 case gfx::BufferFormat::BGRX_8888: | |
78 case gfx::BufferFormat::YUV_420: | |
79 NOTREACHED(); | |
80 return 0; | |
81 } | |
82 | |
83 NOTREACHED(); | |
84 return 0; | |
85 } | |
86 | |
87 } // namespace | |
88 | |
89 // static | |
90 IOSurfaceManager* IOSurfaceManager::GetInstance() { | |
91 DCHECK(g_instance); | |
92 return g_instance; | |
93 } | |
94 | |
95 // static | |
96 void IOSurfaceManager::SetInstance(IOSurfaceManager* instance) { | |
97 DCHECK(!g_instance || !instance); | |
98 g_instance = instance; | |
99 } | |
100 | |
101 // static | |
102 IOSurfaceRef IOSurfaceManager::CreateIOSurface(const gfx::Size& size, | |
103 gfx::BufferFormat format) { | |
104 size_t num_planes = gfx::NumberOfPlanesForBufferFormat(format); | |
105 base::ScopedCFTypeRef<CFMutableArrayRef> planes(CFArrayCreateMutable( | |
106 kCFAllocatorDefault, num_planes, &kCFTypeArrayCallBacks)); | |
107 | |
108 // Don't specify plane information unless there are indeed multiple planes | |
109 // because DisplayLink drivers do not support this. | |
110 // http://crbug.com/527556 | |
111 if (num_planes > 1) { | |
112 for (size_t plane = 0; plane < num_planes; ++plane) { | |
113 size_t factor = gfx::SubsamplingFactorForBufferFormat(format, plane); | |
114 | |
115 base::ScopedCFTypeRef<CFMutableDictionaryRef> plane_info( | |
116 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
117 &kCFTypeDictionaryKeyCallBacks, | |
118 &kCFTypeDictionaryValueCallBacks)); | |
119 AddIntegerValue(plane_info, kIOSurfacePlaneWidth, size.width() / factor); | |
120 AddIntegerValue(plane_info, kIOSurfacePlaneHeight, | |
121 size.height() / factor); | |
122 AddIntegerValue(plane_info, kIOSurfacePlaneBytesPerElement, | |
123 BytesPerElement(format, plane)); | |
124 | |
125 CFArrayAppendValue(planes, plane_info); | |
126 } | |
127 } | |
128 | |
129 base::ScopedCFTypeRef<CFMutableDictionaryRef> properties( | |
130 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
131 &kCFTypeDictionaryKeyCallBacks, | |
132 &kCFTypeDictionaryValueCallBacks)); | |
133 AddIntegerValue(properties, kIOSurfaceWidth, size.width()); | |
134 AddIntegerValue(properties, kIOSurfaceHeight, size.height()); | |
135 AddIntegerValue(properties, kIOSurfacePixelFormat, PixelFormat(format)); | |
136 if (num_planes > 1) { | |
137 CFDictionaryAddValue(properties, kIOSurfacePlaneInfo, planes); | |
138 } else { | |
139 AddIntegerValue(properties, kIOSurfaceBytesPerElement, | |
140 BytesPerElement(format, 0)); | |
141 } | |
142 | |
143 return IOSurfaceCreate(properties); | |
144 } | |
145 | |
146 } // namespace content | |
OLD | NEW |