OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "gpu/command_buffer/service/texture_manager.h" | 5 #include "gpu/command_buffer/service/texture_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bits.h" | 11 #include "base/bits.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "base/trace_event/memory_dump_manager.h" | 15 #include "base/trace_event/memory_dump_manager.h" |
16 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | 16 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
17 #include "gpu/command_buffer/service/context_state.h" | 17 #include "gpu/command_buffer/service/context_state.h" |
18 #include "gpu/command_buffer/service/error_state.h" | 18 #include "gpu/command_buffer/service/error_state.h" |
19 #include "gpu/command_buffer/service/feature_info.h" | 19 #include "gpu/command_buffer/service/feature_info.h" |
20 #include "gpu/command_buffer/service/framebuffer_manager.h" | 20 #include "gpu/command_buffer/service/framebuffer_manager.h" |
21 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 21 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
22 #include "gpu/command_buffer/service/mailbox_manager.h" | 22 #include "gpu/command_buffer/service/mailbox_manager.h" |
23 #include "gpu/command_buffer/service/memory_tracking.h" | 23 #include "gpu/command_buffer/service/memory_tracking.h" |
24 #include "ui/gl/gl_implementation.h" | 24 #include "ui/gl/gl_implementation.h" |
| 25 #include "ui/gl/gl_version_info.h" |
25 #include "ui/gl/trace_util.h" | 26 #include "ui/gl/trace_util.h" |
26 | 27 |
27 namespace gpu { | 28 namespace gpu { |
28 namespace gles2 { | 29 namespace gles2 { |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 // This should contain everything to uniquely identify a Texture. | 33 // This should contain everything to uniquely identify a Texture. |
33 const char TextureTag[] = "|Texture|"; | 34 const char TextureTag[] = "|Texture|"; |
34 struct TextureSignature { | 35 struct TextureSignature { |
(...skipping 1963 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1998 DoTexImage(texture_state, state->GetErrorState(), framebuffer_state, | 1999 DoTexImage(texture_state, state->GetErrorState(), framebuffer_state, |
1999 function_name, texture_ref, new_args); | 2000 function_name, texture_ref, new_args); |
2000 } | 2001 } |
2001 return; | 2002 return; |
2002 } | 2003 } |
2003 | 2004 |
2004 DoTexImage(texture_state, state->GetErrorState(), framebuffer_state, | 2005 DoTexImage(texture_state, state->GetErrorState(), framebuffer_state, |
2005 function_name, texture_ref, args); | 2006 function_name, texture_ref, args); |
2006 } | 2007 } |
2007 | 2008 |
| 2009 bool TextureManager::ValidateTexSubImage(ContextState* state, |
| 2010 const char* function_name, |
| 2011 const DoTexSubImageArguments& args, |
| 2012 TextureRef** texture_ref) { |
| 2013 ErrorState* error_state = state->GetErrorState(); |
| 2014 const Validators* validators = feature_info_->validators(); |
| 2015 |
| 2016 if (!validators->texture_target.IsValid(args.target)) { |
| 2017 ERRORSTATE_SET_GL_ERROR_INVALID_ENUM(error_state, function_name, |
| 2018 args.target, "target"); |
| 2019 return false; |
| 2020 } |
| 2021 if (args.width < 0) { |
| 2022 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_VALUE, function_name, |
| 2023 "width < 0"); |
| 2024 return false; |
| 2025 } |
| 2026 if (args.height < 0) { |
| 2027 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_VALUE, function_name, |
| 2028 "height < 0"); |
| 2029 return false; |
| 2030 } |
| 2031 TextureRef* local_texture_ref = GetTextureInfoForTarget(state, args.target); |
| 2032 if (!local_texture_ref) { |
| 2033 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_OPERATION, function_name, |
| 2034 "unknown texture for target"); |
| 2035 return false; |
| 2036 } |
| 2037 Texture* texture = local_texture_ref->texture(); |
| 2038 GLenum current_type = 0; |
| 2039 GLenum internal_format = 0; |
| 2040 if (!texture->GetLevelType(args.target, args.level, ¤t_type, |
| 2041 &internal_format)) { |
| 2042 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_OPERATION, function_name, |
| 2043 "level does not exist."); |
| 2044 return false; |
| 2045 } |
| 2046 if (!ValidateTextureParameters(error_state, function_name, args.format, |
| 2047 args.type, internal_format, args.level)) { |
| 2048 return false; |
| 2049 } |
| 2050 if (args.type != current_type && !feature_info_->IsES3Enabled()) { |
| 2051 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_OPERATION, function_name, |
| 2052 "type does not match type of texture."); |
| 2053 return false; |
| 2054 } |
| 2055 if (!texture->ValidForTexture(args.target, args.level, args.xoffset, |
| 2056 args.yoffset, 0, args.width, args.height, 1)) { |
| 2057 ERRORSTATE_SET_GL_ERROR(error_state, GL_INVALID_VALUE, function_name, |
| 2058 "bad dimensions."); |
| 2059 return false; |
| 2060 } |
| 2061 if ((GLES2Util::GetChannelsForFormat(args.format) & |
| 2062 (GLES2Util::kDepth | GLES2Util::kStencil)) != 0 && |
| 2063 !feature_info_->IsES3Enabled()) { |
| 2064 ERRORSTATE_SET_GL_ERROR( |
| 2065 error_state, GL_INVALID_OPERATION, function_name, |
| 2066 "can not supply data for depth or stencil textures"); |
| 2067 return false; |
| 2068 } |
| 2069 DCHECK(args.pixels); |
| 2070 *texture_ref = local_texture_ref; |
| 2071 return true; |
| 2072 } |
| 2073 |
| 2074 void TextureManager::ValidateAndDoTexSubImage( |
| 2075 GLES2Decoder* decoder, |
| 2076 DecoderTextureState* texture_state, |
| 2077 ContextState* state, |
| 2078 DecoderFramebufferState* framebuffer_state, |
| 2079 const char* function_name, |
| 2080 const DoTexSubImageArguments& args) { |
| 2081 ErrorState* error_state = state->GetErrorState(); |
| 2082 TextureRef* texture_ref; |
| 2083 if (!ValidateTexSubImage(state, function_name, args, &texture_ref)) { |
| 2084 return; |
| 2085 } |
| 2086 |
| 2087 Texture* texture = texture_ref->texture(); |
| 2088 GLsizei tex_width = 0; |
| 2089 GLsizei tex_height = 0; |
| 2090 bool ok = texture->GetLevelSize(args.target, args.level, &tex_width, |
| 2091 &tex_height, nullptr); |
| 2092 DCHECK(ok); |
| 2093 if (args.xoffset != 0 || args.yoffset != 0 || args.width != tex_width || |
| 2094 args.height != tex_height) { |
| 2095 gfx::Rect cleared_rect; |
| 2096 if (CombineAdjacentRects( |
| 2097 texture->GetLevelClearedRect(args.target, args.level), |
| 2098 gfx::Rect(args.xoffset, args.yoffset, args.width, args.height), |
| 2099 &cleared_rect)) { |
| 2100 DCHECK_GE(cleared_rect.size().GetArea(), |
| 2101 texture->GetLevelClearedRect(args.target, args.level) |
| 2102 .size() |
| 2103 .GetArea()); |
| 2104 SetLevelClearedRect(texture_ref, args.target, args.level, cleared_rect); |
| 2105 } else { |
| 2106 // Otherwise clear part of texture level that is not already cleared. |
| 2107 if (!ClearTextureLevel(decoder, texture_ref, args.target, args.level)) { |
| 2108 ERRORSTATE_SET_GL_ERROR(error_state, GL_OUT_OF_MEMORY, |
| 2109 "glTexSubImage2D", "dimensions too big"); |
| 2110 return; |
| 2111 } |
| 2112 } |
| 2113 ScopedTextureUploadTimer timer(texture_state); |
| 2114 glTexSubImage2D(args.target, args.level, args.xoffset, args.yoffset, |
| 2115 args.width, args.height, AdjustTexFormat(args.format), |
| 2116 args.type, args.pixels); |
| 2117 return; |
| 2118 } |
| 2119 |
| 2120 if (!texture_state->texsubimage_faster_than_teximage && |
| 2121 !texture->IsImmutable() && !texture->HasImages()) { |
| 2122 ScopedTextureUploadTimer timer(texture_state); |
| 2123 GLenum internal_format; |
| 2124 GLenum tex_type; |
| 2125 texture->GetLevelType(args.target, args.level, &tex_type, &internal_format); |
| 2126 // NOTE: In OpenGL ES 2.0 border is always zero. If that changes we'll need |
| 2127 // to look it up. |
| 2128 glTexImage2D(args.target, args.level, internal_format, args.width, |
| 2129 args.height, 0, AdjustTexFormat(args.format), args.type, |
| 2130 args.pixels); |
| 2131 } else { |
| 2132 ScopedTextureUploadTimer timer(texture_state); |
| 2133 glTexSubImage2D(args.target, args.level, args.xoffset, args.yoffset, |
| 2134 args.width, args.height, AdjustTexFormat(args.format), |
| 2135 args.type, args.pixels); |
| 2136 } |
| 2137 SetLevelCleared(texture_ref, args.target, args.level, true); |
| 2138 return; |
| 2139 } |
| 2140 |
2008 GLenum TextureManager::AdjustTexFormat(GLenum format) const { | 2141 GLenum TextureManager::AdjustTexFormat(GLenum format) const { |
2009 // TODO(bajones): GLES 3 allows for internal format and format to differ. | 2142 // TODO(bajones): GLES 3 allows for internal format and format to differ. |
2010 // This logic may need to change as a result. | 2143 // This logic may need to change as a result. |
2011 if (gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGL) { | 2144 if (gfx::GetGLImplementation() == gfx::kGLImplementationDesktopGL) { |
2012 if (format == GL_SRGB_EXT) | 2145 if (format == GL_SRGB_EXT) |
2013 return GL_RGB; | 2146 return GL_RGB; |
2014 if (format == GL_SRGB_ALPHA_EXT) | 2147 if (format == GL_SRGB_ALPHA_EXT) |
2015 return GL_RGBA; | 2148 return GL_RGBA; |
2016 } | 2149 } |
2017 return format; | 2150 return format; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2085 GLenum error = ERRORSTATE_PEEK_GL_ERROR(error_state, function_name); | 2218 GLenum error = ERRORSTATE_PEEK_GL_ERROR(error_state, function_name); |
2086 if (error == GL_NO_ERROR) { | 2219 if (error == GL_NO_ERROR) { |
2087 SetLevelInfo( | 2220 SetLevelInfo( |
2088 texture_ref, args.target, args.level, args.internal_format, args.width, | 2221 texture_ref, args.target, args.level, args.internal_format, args.width, |
2089 args.height, args.depth, args.border, args.format, args.type, | 2222 args.height, args.depth, args.border, args.format, args.type, |
2090 args.pixels != NULL ? gfx::Rect(args.width, args.height) : gfx::Rect()); | 2223 args.pixels != NULL ? gfx::Rect(args.width, args.height) : gfx::Rect()); |
2091 texture_state->tex_image_failed = false; | 2224 texture_state->tex_image_failed = false; |
2092 } | 2225 } |
2093 } | 2226 } |
2094 | 2227 |
| 2228 bool TextureManager::CombineAdjacentRects(const gfx::Rect& rect1, |
| 2229 const gfx::Rect& rect2, |
| 2230 gfx::Rect* result) { |
| 2231 // Return |rect2| if |rect1| is empty or |rect2| contains |rect1|. |
| 2232 if (rect1.IsEmpty() || rect2.Contains(rect1)) { |
| 2233 *result = rect2; |
| 2234 return true; |
| 2235 } |
| 2236 |
| 2237 // Return |rect1| if |rect2| is empty or |rect1| contains |rect2|. |
| 2238 if (rect2.IsEmpty() || rect1.Contains(rect2)) { |
| 2239 *result = rect1; |
| 2240 return true; |
| 2241 } |
| 2242 |
| 2243 // Return the union of |rect1| and |rect2| if they share an edge. |
| 2244 if (rect1.SharesEdgeWith(rect2)) { |
| 2245 *result = gfx::UnionRects(rect1, rect2); |
| 2246 return true; |
| 2247 } |
| 2248 |
| 2249 // Return false if it's not possible to combine |rect1| and |rect2|. |
| 2250 return false; |
| 2251 } |
| 2252 |
2095 ScopedTextureUploadTimer::ScopedTextureUploadTimer( | 2253 ScopedTextureUploadTimer::ScopedTextureUploadTimer( |
2096 DecoderTextureState* texture_state) | 2254 DecoderTextureState* texture_state) |
2097 : texture_state_(texture_state), | 2255 : texture_state_(texture_state), |
2098 begin_time_(base::TimeTicks::Now()) { | 2256 begin_time_(base::TimeTicks::Now()) { |
2099 } | 2257 } |
2100 | 2258 |
2101 ScopedTextureUploadTimer::~ScopedTextureUploadTimer() { | 2259 ScopedTextureUploadTimer::~ScopedTextureUploadTimer() { |
2102 texture_state_->texture_upload_count++; | 2260 texture_state_->texture_upload_count++; |
2103 texture_state_->total_texture_upload_time += | 2261 texture_state_->total_texture_upload_time += |
2104 base::TimeTicks::Now() - begin_time_; | 2262 base::TimeTicks::Now() - begin_time_; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2163 pmd->AddOwnershipEdge(client_guid, service_guid, importance); | 2321 pmd->AddOwnershipEdge(client_guid, service_guid, importance); |
2164 | 2322 |
2165 // Dump all sub-levels held by the texture. They will appear below the main | 2323 // Dump all sub-levels held by the texture. They will appear below the main |
2166 // gl/textures/client_X/texture_Y dump. | 2324 // gl/textures/client_X/texture_Y dump. |
2167 ref->texture()->DumpLevelMemory(pmd, memory_tracker_->ClientTracingId(), | 2325 ref->texture()->DumpLevelMemory(pmd, memory_tracker_->ClientTracingId(), |
2168 dump_name); | 2326 dump_name); |
2169 } | 2327 } |
2170 | 2328 |
2171 } // namespace gles2 | 2329 } // namespace gles2 |
2172 } // namespace gpu | 2330 } // namespace gpu |
OLD | NEW |