OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_image_io_surface.h" | 5 #include "ui/gl/gl_image_io_surface.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/trace_event/memory_allocator_dump.h" |
| 12 #include "base/trace_event/memory_dump_manager.h" |
| 13 #include "base/trace_event/process_memory_dump.h" |
11 #include "ui/gl/gl_bindings.h" | 14 #include "ui/gl/gl_bindings.h" |
12 #include "ui/gl/gl_context.h" | 15 #include "ui/gl/gl_context.h" |
13 | 16 |
14 // Note that this must be included after gl_bindings.h to avoid conflicts. | 17 // Note that this must be included after gl_bindings.h to avoid conflicts. |
15 #include <OpenGL/CGLIOSurface.h> | 18 #include <OpenGL/CGLIOSurface.h> |
16 #include <Quartz/Quartz.h> | 19 #include <Quartz/Quartz.h> |
17 | 20 |
18 namespace gfx { | 21 namespace gfx { |
19 namespace { | 22 namespace { |
20 | 23 |
21 typedef std::map<gfx::AcceleratedWidget,CALayer*> WidgetToLayerMap; | 24 using WidgetToLayerMap = std::map<AcceleratedWidget, CALayer*>; |
22 base::LazyInstance<WidgetToLayerMap> g_widget_to_layer_map; | 25 base::LazyInstance<WidgetToLayerMap> g_widget_to_layer_map; |
23 | 26 |
24 bool ValidInternalFormat(unsigned internalformat) { | 27 bool ValidInternalFormat(unsigned internalformat) { |
25 switch (internalformat) { | 28 switch (internalformat) { |
26 case GL_R8: | 29 case GL_R8: |
27 case GL_BGRA_EXT: | 30 case GL_BGRA_EXT: |
28 case GL_RGB: | 31 case GL_RGB: |
29 case GL_RGB_YCBCR_422_CHROMIUM: | 32 case GL_RGB_YCBCR_422_CHROMIUM: |
30 return true; | 33 return true; |
31 default: | 34 default: |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 NOTREACHED(); | 134 NOTREACHED(); |
132 return 0; | 135 return 0; |
133 } | 136 } |
134 | 137 |
135 NOTREACHED(); | 138 NOTREACHED(); |
136 return 0; | 139 return 0; |
137 } | 140 } |
138 | 141 |
139 } // namespace | 142 } // namespace |
140 | 143 |
141 GLImageIOSurface::GLImageIOSurface(const gfx::Size& size, | 144 GLImageIOSurface::GLImageIOSurface(const Size& size, unsigned internalformat) |
142 unsigned internalformat) | |
143 : size_(size), | 145 : size_(size), |
144 internalformat_(internalformat), | 146 internalformat_(internalformat), |
145 format_(BufferFormat::RGBA_8888) {} | 147 format_(BufferFormat::RGBA_8888) {} |
146 | 148 |
147 GLImageIOSurface::~GLImageIOSurface() { | 149 GLImageIOSurface::~GLImageIOSurface() { |
148 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
149 DCHECK(!io_surface_); | 151 DCHECK(!io_surface_); |
150 } | 152 } |
151 | 153 |
152 bool GLImageIOSurface::Initialize(IOSurfaceRef io_surface, | 154 bool GLImageIOSurface::Initialize(IOSurfaceRef io_surface, |
153 gfx::GenericSharedMemoryId io_surface_id, | 155 GenericSharedMemoryId io_surface_id, |
154 BufferFormat format) { | 156 BufferFormat format) { |
155 DCHECK(thread_checker_.CalledOnValidThread()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
156 DCHECK(!io_surface_); | 158 DCHECK(!io_surface_); |
157 | 159 |
158 if (!ValidInternalFormat(internalformat_)) { | 160 if (!ValidInternalFormat(internalformat_)) { |
159 LOG(ERROR) << "Invalid internalformat: " << internalformat_; | 161 LOG(ERROR) << "Invalid internalformat: " << internalformat_; |
160 return false; | 162 return false; |
161 } | 163 } |
162 | 164 |
163 if (!ValidFormat(format)) { | 165 if (!ValidFormat(format)) { |
164 LOG(ERROR) << "Invalid format: " << static_cast<int>(format); | 166 LOG(ERROR) << "Invalid format: " << static_cast<int>(format); |
165 return false; | 167 return false; |
166 } | 168 } |
167 | 169 |
168 format_ = format; | 170 format_ = format; |
169 io_surface_.reset(io_surface, base::scoped_policy::RETAIN); | 171 io_surface_.reset(io_surface, base::scoped_policy::RETAIN); |
170 io_surface_id_ = io_surface_id; | 172 io_surface_id_ = io_surface_id; |
171 return true; | 173 return true; |
172 } | 174 } |
173 | 175 |
174 void GLImageIOSurface::Destroy(bool have_context) { | 176 void GLImageIOSurface::Destroy(bool have_context) { |
175 DCHECK(thread_checker_.CalledOnValidThread()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
176 io_surface_.reset(); | 178 io_surface_.reset(); |
177 } | 179 } |
178 | 180 |
179 gfx::Size GLImageIOSurface::GetSize() { return size_; } | 181 Size GLImageIOSurface::GetSize() { |
| 182 return size_; |
| 183 } |
180 | 184 |
181 unsigned GLImageIOSurface::GetInternalFormat() { return internalformat_; } | 185 unsigned GLImageIOSurface::GetInternalFormat() { return internalformat_; } |
182 | 186 |
183 bool GLImageIOSurface::BindTexImage(unsigned target) { | 187 bool GLImageIOSurface::BindTexImage(unsigned target) { |
184 DCHECK(thread_checker_.CalledOnValidThread()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
185 if (target != GL_TEXTURE_RECTANGLE_ARB) { | 189 if (target != GL_TEXTURE_RECTANGLE_ARB) { |
186 // This might be supported in the future. For now, perform strict | 190 // This might be supported in the future. For now, perform strict |
187 // validation so we know what's going on. | 191 // validation so we know what's going on. |
188 LOG(ERROR) << "IOSurface requires TEXTURE_RECTANGLE_ARB target"; | 192 LOG(ERROR) << "IOSurface requires TEXTURE_RECTANGLE_ARB target"; |
189 return false; | 193 return false; |
190 } | 194 } |
191 | 195 |
192 CGLContextObj cgl_context = | 196 CGLContextObj cgl_context = |
193 static_cast<CGLContextObj>(GLContext::GetCurrent()->GetHandle()); | 197 static_cast<CGLContextObj>(GLContext::GetCurrent()->GetHandle()); |
194 | 198 |
195 DCHECK(io_surface_); | 199 DCHECK(io_surface_); |
196 CGLError cgl_error = | 200 CGLError cgl_error = |
197 CGLTexImageIOSurface2D(cgl_context, target, TextureFormat(format_), | 201 CGLTexImageIOSurface2D(cgl_context, target, TextureFormat(format_), |
198 size_.width(), size_.height(), DataFormat(format_), | 202 size_.width(), size_.height(), DataFormat(format_), |
199 DataType(format_), io_surface_.get(), 0); | 203 DataType(format_), io_surface_.get(), 0); |
200 if (cgl_error != kCGLNoError) { | 204 if (cgl_error != kCGLNoError) { |
201 LOG(ERROR) << "Error in CGLTexImageIOSurface2D"; | 205 LOG(ERROR) << "Error in CGLTexImageIOSurface2D"; |
202 return false; | 206 return false; |
203 } | 207 } |
204 | 208 |
205 return true; | 209 return true; |
206 } | 210 } |
207 | 211 |
| 212 bool GLImageIOSurface::CopyTexImage(unsigned target) { |
| 213 return false; |
| 214 } |
| 215 |
208 bool GLImageIOSurface::CopyTexSubImage(unsigned target, | 216 bool GLImageIOSurface::CopyTexSubImage(unsigned target, |
209 const Point& offset, | 217 const Point& offset, |
210 const Rect& rect) { | 218 const Rect& rect) { |
211 return false; | 219 return false; |
212 } | 220 } |
213 | 221 |
214 bool GLImageIOSurface::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 222 bool GLImageIOSurface::ScheduleOverlayPlane(AcceleratedWidget widget, |
215 int z_order, | 223 int z_order, |
216 OverlayTransform transform, | 224 OverlayTransform transform, |
217 const Rect& bounds_rect, | 225 const Rect& bounds_rect, |
218 const RectF& crop_rect) { | 226 const RectF& crop_rect) { |
219 NOTREACHED(); | 227 NOTREACHED(); |
220 return false; | 228 return false; |
221 } | 229 } |
222 | 230 |
223 void GLImageIOSurface::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 231 void GLImageIOSurface::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
224 uint64_t process_tracing_id, | 232 uint64_t process_tracing_id, |
225 const std::string& dump_name) { | 233 const std::string& dump_name) { |
226 // IOSurfaceGetAllocSize will return 0 if io_surface_ is invalid. In this case | 234 // IOSurfaceGetAllocSize will return 0 if io_surface_ is invalid. In this case |
227 // we log 0 for consistency with other GLImage memory dump functions. | 235 // we log 0 for consistency with other GLImage memory dump functions. |
228 size_t size_bytes = IOSurfaceGetAllocSize(io_surface_); | 236 size_t size_bytes = IOSurfaceGetAllocSize(io_surface_); |
229 | 237 |
230 base::trace_event::MemoryAllocatorDump* dump = | 238 base::trace_event::MemoryAllocatorDump* dump = |
231 pmd->CreateAllocatorDump(dump_name); | 239 pmd->CreateAllocatorDump(dump_name); |
232 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 240 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
233 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 241 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
234 static_cast<uint64_t>(size_bytes)); | 242 static_cast<uint64_t>(size_bytes)); |
235 | 243 |
236 auto guid = gfx::GetGenericSharedMemoryGUIDForTracing(process_tracing_id, | 244 auto guid = |
237 io_surface_id_); | 245 GetGenericSharedMemoryGUIDForTracing(process_tracing_id, io_surface_id_); |
238 pmd->CreateSharedGlobalAllocatorDump(guid); | 246 pmd->CreateSharedGlobalAllocatorDump(guid); |
239 pmd->AddOwnershipEdge(dump->guid(), guid); | 247 pmd->AddOwnershipEdge(dump->guid(), guid); |
240 } | 248 } |
241 | 249 |
242 base::ScopedCFTypeRef<IOSurfaceRef> GLImageIOSurface::io_surface() { | 250 base::ScopedCFTypeRef<IOSurfaceRef> GLImageIOSurface::io_surface() { |
243 return io_surface_; | 251 return io_surface_; |
244 } | 252 } |
245 | 253 |
246 // static | 254 // static |
247 void GLImageIOSurface::SetLayerForWidget( | 255 void GLImageIOSurface::SetLayerForWidget(AcceleratedWidget widget, |
248 gfx::AcceleratedWidget widget, CALayer* layer) { | 256 CALayer* layer) { |
249 if (layer) | 257 if (layer) |
250 g_widget_to_layer_map.Pointer()->insert(std::make_pair(widget, layer)); | 258 g_widget_to_layer_map.Pointer()->insert(std::make_pair(widget, layer)); |
251 else | 259 else |
252 g_widget_to_layer_map.Pointer()->erase(widget); | 260 g_widget_to_layer_map.Pointer()->erase(widget); |
253 } | 261 } |
254 | 262 |
255 } // namespace gfx | 263 } // namespace gfx |
OLD | NEW |