Chromium Code Reviews| 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 "content/common/gpu/client/gpu_memory_buffer_impl.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/numerics/safe_math.h" | 8 #include "base/numerics/safe_math.h" |
| 9 #include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h" | 9 #include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h" |
| 10 #include "ui/gl/gl_bindings.h" | 10 #include "ui/gl/gl_bindings.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 // static | 73 // static |
| 74 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer( | 74 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer( |
| 75 ClientBuffer buffer) { | 75 ClientBuffer buffer) { |
| 76 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer); | 76 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // static | 79 // static |
| 80 bool GpuMemoryBufferImpl::StrideInBytes(size_t width, | |
| 81 Format format, | |
| 82 size_t* stride_in_bytes) { | |
| 83 base::CheckedNumeric<size_t> s = width; | |
| 84 switch (format) { | |
| 85 case ATCIA: | |
| 86 case DXT5: | |
| 87 *stride_in_bytes = width; | |
| 88 return true; | |
| 89 case ATC: | |
| 90 case DXT1: | |
| 91 case ETC1: | |
| 92 DCHECK_EQ(width % 2, 0U); | |
| 93 s /= 2; | |
| 94 if (!s.IsValid()) | |
| 95 return false; | |
| 96 | |
| 97 *stride_in_bytes = s.ValueOrDie(); | |
| 98 return true; | |
| 99 case RGBA_8888: | |
| 100 case RGBX_8888: | |
| 101 case BGRA_8888: | |
| 102 s *= 4; | |
| 103 if (!s.IsValid()) | |
| 104 return false; | |
| 105 | |
| 106 *stride_in_bytes = s.ValueOrDie(); | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 NOTREACHED(); | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 // static | |
| 115 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat( | 80 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat( |
| 116 gfx::GpuMemoryBuffer::Format format) { | 81 gfx::GpuMemoryBuffer::Format format) { |
|
reveman
2015/04/07 20:33:44
nit: remove gfx::GpuMemoryBuffer:: prefix if possi
emircan
2015/04/07 21:28:45
Done.
| |
| 117 switch (format) { | 82 switch (format) { |
| 118 case gfx::GpuMemoryBuffer::Format::ATC: | 83 case gfx::GpuMemoryBuffer::Format::ATC: |
|
reveman
2015/04/07 20:33:44
nit: remove gfx::GpuMemoryBuffer::Format:: prefix
emircan
2015/04/07 21:28:45
Done.
| |
| 119 case gfx::GpuMemoryBuffer::Format::ATCIA: | 84 case gfx::GpuMemoryBuffer::Format::ATCIA: |
| 120 case gfx::GpuMemoryBuffer::Format::DXT1: | 85 case gfx::GpuMemoryBuffer::Format::DXT1: |
| 121 case gfx::GpuMemoryBuffer::Format::DXT5: | 86 case gfx::GpuMemoryBuffer::Format::DXT5: |
| 122 case gfx::GpuMemoryBuffer::Format::ETC1: | 87 case gfx::GpuMemoryBuffer::Format::ETC1: |
| 123 case gfx::GpuMemoryBuffer::Format::RGBA_8888: | 88 case gfx::GpuMemoryBuffer::Format::RGBA_8888: |
| 124 case gfx::GpuMemoryBuffer::Format::RGBX_8888: | 89 case gfx::GpuMemoryBuffer::Format::RGBX_8888: |
| 125 case gfx::GpuMemoryBuffer::Format::BGRA_8888: | 90 case gfx::GpuMemoryBuffer::Format::BGRA_8888: |
| 126 return 1; | 91 return 1; |
| 127 default: | 92 case gfx::GpuMemoryBuffer::Format::YUV_420: |
| 128 NOTREACHED(); | 93 return 3; |
| 129 return 0; | |
| 130 } | 94 } |
| 95 NOTREACHED(); | |
| 96 return 0; | |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 bool GpuMemoryBufferImpl::StrideInBytes(size_t width, | |
| 101 Format format, | |
| 102 int plane, | |
| 103 size_t* stride_in_bytes) { | |
| 104 switch (format) { | |
| 105 case ATCIA: | |
| 106 case DXT5: | |
| 107 DCHECK_EQ(plane, 0); | |
| 108 *stride_in_bytes = width; | |
| 109 return true; | |
| 110 case ATC: | |
| 111 case DXT1: | |
| 112 case ETC1: | |
| 113 DCHECK_EQ(plane, 0); | |
| 114 DCHECK_EQ(width % 2, 0U); | |
| 115 *stride_in_bytes = width / 2; | |
| 116 return true; | |
| 117 case RGBA_8888: | |
| 118 case RGBX_8888: | |
| 119 case BGRA_8888: { | |
| 120 base::CheckedNumeric<size_t> s = width; | |
| 121 DCHECK_EQ(plane, 0); | |
| 122 s *= 4; | |
| 123 if (!s.IsValid()) | |
| 124 return false; | |
| 125 *stride_in_bytes = s.ValueOrDie(); | |
| 126 return true; | |
| 127 } | |
| 128 case YUV_420: { | |
| 129 DCHECK_EQ(width % 2, 0u); | |
| 130 *stride_in_bytes = width / SubsamplingFactor(format, plane); | |
| 131 return true; | |
| 132 } | |
| 133 } | |
| 134 NOTREACHED(); | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 size_t GpuMemoryBufferImpl::SubsamplingFactor( | |
| 140 gfx::GpuMemoryBuffer::Format format, | |
|
reveman
2015/04/07 20:33:44
nit: remove gfx::GpuMemoryBuffer:: prefix if possi
emircan
2015/04/07 21:28:45
Done.
| |
| 141 int plane) { | |
| 142 switch (format) { | |
| 143 case gfx::GpuMemoryBuffer::ATC: | |
|
reveman
2015/04/07 20:33:44
nit: remove gfx::GpuMemoryBuffer::Format:: prefix
emircan
2015/04/07 21:28:45
Done.
| |
| 144 case gfx::GpuMemoryBuffer::ATCIA: | |
| 145 case gfx::GpuMemoryBuffer::DXT1: | |
| 146 case gfx::GpuMemoryBuffer::DXT5: | |
| 147 case gfx::GpuMemoryBuffer::ETC1: | |
| 148 case gfx::GpuMemoryBuffer::RGBA_8888: | |
| 149 case gfx::GpuMemoryBuffer::RGBX_8888: | |
| 150 case gfx::GpuMemoryBuffer::BGRA_8888: | |
| 151 return 1; | |
| 152 case gfx::GpuMemoryBuffer::YUV_420: { | |
| 153 static size_t factor[] = {1, 2, 2}; | |
| 154 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); | |
| 155 return factor[plane]; | |
| 156 } | |
| 157 } | |
| 158 NOTREACHED(); | |
| 159 return 0; | |
| 160 } | |
| 161 | |
| 162 // static | |
| 163 bool GpuMemoryBufferImpl::BufferSizeInBytes( | |
| 164 const gfx::Size& size, | |
| 165 gfx::GpuMemoryBuffer::Format format, | |
|
reveman
2015/04/07 20:33:44
nit: remove gfx::GpuMemoryBuffer:: prefix if possi
emircan
2015/04/07 21:28:45
Done.
| |
| 166 size_t* total_size_in_bytes) { | |
|
reveman
2015/04/07 20:33:44
nit: s/total_size_in_bytes/size_in_bytes/
emircan
2015/04/07 21:28:45
Done.
| |
| 167 base::CheckedNumeric<size_t> size_in_bytes = 0u; | |
|
reveman
2015/04/07 20:33:44
nit: s/size_in_bytes/checked_size_in_bytes/
emircan
2015/04/07 21:28:45
Done.
| |
| 168 for (size_t i = 0; i < NumberOfPlanesForGpuMemoryBufferFormat(format); ++i) { | |
| 169 size_t stride_in_bytes = 0; | |
| 170 if (!StrideInBytes(size.width(), format, i, &stride_in_bytes)) | |
| 171 return false; | |
| 172 base::CheckedNumeric<size_t> plane_size = | |
|
reveman
2015/04/07 20:33:44
nit: s/plane_size/checked_plane_size_in_bytes/
emircan
2015/04/07 21:28:45
Done.
| |
| 173 stride_in_bytes * (size.height() / SubsamplingFactor(format, i)); | |
|
reveman
2015/04/07 20:33:44
This can still overflow. For CheckedNumeric to wor
emircan
2015/04/07 21:28:45
Done.
| |
| 174 if (!plane_size.IsValid()) | |
| 175 return false; | |
| 176 size_in_bytes += plane_size.ValueOrDie(); | |
| 177 if (!size_in_bytes.IsValid()) | |
| 178 return false; | |
| 179 } | |
| 180 *total_size_in_bytes = size_in_bytes.ValueOrDie(); | |
| 181 return true; | |
| 131 } | 182 } |
| 132 | 183 |
| 133 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const { | 184 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const { |
| 134 return format_; | 185 return format_; |
| 135 } | 186 } |
| 136 | 187 |
| 137 bool GpuMemoryBufferImpl::IsMapped() const { | 188 bool GpuMemoryBufferImpl::IsMapped() const { |
| 138 return mapped_; | 189 return mapped_; |
| 139 } | 190 } |
| 140 | 191 |
| 141 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() { | 192 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() { |
| 142 return reinterpret_cast<ClientBuffer>(this); | 193 return reinterpret_cast<ClientBuffer>(this); |
| 143 } | 194 } |
| 144 | 195 |
| 145 } // namespace content | 196 } // namespace content |
| OLD | NEW |