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