Chromium Code Reviews| 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 "ui/gfx/surface/accelerated_surface_win.h" | 5 #include "ui/gfx/surface/accelerated_surface_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <algorithm> | |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 13 #include "base/debug/trace_event.h" | 14 #include "base/debug/trace_event.h" |
| 14 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/scoped_native_library.h" | 18 #include "base/scoped_native_library.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 device_.Receive()); | 139 device_.Receive()); |
| 139 if (FAILED(hr)) | 140 if (FAILED(hr)) |
| 140 return; | 141 return; |
| 141 | 142 |
| 142 hr = device_->CreateQuery(D3DQUERYTYPE_EVENT, query_.Receive()); | 143 hr = device_->CreateQuery(D3DQUERYTYPE_EVENT, query_.Receive()); |
| 143 if (FAILED(hr)) { | 144 if (FAILED(hr)) { |
| 144 device_ = NULL; | 145 device_ = NULL; |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 149 bool AcceleratedPresenter::CopyTo(const gfx::Size& size, | |
| 150 std::vector<unsigned char>* buf) { | |
| 151 base::AutoLock locked(lock_); | |
| 152 | |
| 153 base::win::ScopedComPtr<IDirect3DSurface9> temp_buffer[2]; | |
| 154 HRESULT hr = swap_chain_->GetBackBuffer(0, | |
|
apatrick_chromium
2012/03/08 20:15:47
swap_chain_ will be null if either no surface has
mazda
2012/03/10 07:51:37
Done.
| |
| 155 D3DBACKBUFFER_TYPE_MONO, | |
| 156 temp_buffer[0].Receive()); | |
| 157 if (FAILED(hr)) | |
| 158 return false; | |
| 159 | |
| 160 D3DSURFACE_DESC desc; | |
| 161 hr = temp_buffer[0]->GetDesc(&desc); | |
| 162 if (FAILED(hr) || (desc.Width == 0) || (desc.Height == 0)) | |
| 163 return false; | |
| 164 | |
| 165 // Repeat downsampling the surface until its size becomes identical to | |
| 166 // |size|. We keep the factor of each downsampling no more than two because | |
| 167 // using a factor more than two can introduce aliasing. | |
| 168 int buffer_width = desc.Width; | |
| 169 int buffer_height = desc.Height; | |
| 170 int read_buffer = 0; | |
| 171 int write_buffer = 1; | |
| 172 do { | |
|
apatrick_chromium
2012/03/08 20:15:47
I believe it is possible to call GetRenderTargetDa
mazda
2012/03/10 07:51:37
Done.
| |
| 173 buffer_width = std::max(buffer_width / 2, size.width()); | |
| 174 buffer_height = std::max(buffer_height / 2, size.height()); | |
| 175 temp_buffer[write_buffer].Release(); | |
| 176 hr = present_thread_->device()->CreateRenderTarget( | |
|
vangelis
2012/03/08 17:34:35
Can we avoid creating render targets for all inter
mazda
2012/03/10 07:51:37
Done.
| |
| 177 buffer_width, | |
| 178 buffer_height, | |
| 179 D3DFMT_A8R8G8B8, | |
| 180 D3DMULTISAMPLE_NONE, | |
| 181 0, | |
| 182 TRUE, | |
| 183 temp_buffer[write_buffer].Receive(), | |
| 184 NULL); | |
| 185 if (FAILED(hr)) | |
| 186 return false; | |
| 187 | |
| 188 hr = present_thread_->device()->StretchRect(temp_buffer[read_buffer], | |
| 189 NULL, | |
| 190 temp_buffer[write_buffer], | |
| 191 NULL, | |
| 192 D3DTEXF_LINEAR); | |
| 193 if (FAILED(hr)) | |
| 194 return false; | |
| 195 | |
| 196 std::swap(read_buffer, write_buffer); | |
| 197 } while (size != gfx::Size(buffer_width, buffer_height)); | |
| 198 | |
| 199 const size_t buf_size = static_cast<size_t>(4 * size.GetArea()); | |
| 200 if (buf->size() < buf_size) { | |
| 201 buf->resize(buf_size); | |
| 202 } | |
| 203 | |
| 204 base::win::ScopedComPtr<IDirect3DSurface9> temp_surface; | |
| 205 HANDLE handle = reinterpret_cast<HANDLE>(&(*buf)[0]); | |
| 206 hr = present_thread_->device()->CreateOffscreenPlainSurface( | |
| 207 size.width(), | |
| 208 size.height(), | |
| 209 D3DFMT_A8R8G8B8, | |
| 210 D3DPOOL_SYSTEMMEM, | |
| 211 temp_surface.Receive(), | |
| 212 &handle); | |
| 213 if (FAILED(hr)) | |
| 214 return false; | |
| 215 | |
| 216 // Copy the data in the temporary buffer to the surface backed by |buf|. | |
| 217 hr = present_thread_->device()->GetRenderTargetData( | |
| 218 temp_buffer[read_buffer], temp_surface); | |
| 219 if (FAILED(hr)) | |
| 220 return false; | |
| 221 | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 148 void PresentThread::Init() { | 225 void PresentThread::Init() { |
| 149 TRACE_EVENT0("surface", "PresentThread::Init"); | 226 TRACE_EVENT0("surface", "PresentThread::Init"); |
| 150 d3d_module_.Reset(base::LoadNativeLibrary(FilePath(kD3D9ModuleName), NULL)); | 227 d3d_module_.Reset(base::LoadNativeLibrary(FilePath(kD3D9ModuleName), NULL)); |
| 151 ResetDevice(); | 228 ResetDevice(); |
| 152 } | 229 } |
| 153 | 230 |
| 154 void PresentThread::CleanUp() { | 231 void PresentThread::CleanUp() { |
| 155 // The D3D device and query are leaked because destroying the associated D3D | 232 // The D3D device and query are leaked because destroying the associated D3D |
| 156 // query crashes some Intel drivers. | 233 // query crashes some Intel drivers. |
| 157 device_.Detach(); | 234 device_.Detach(); |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 424 completion_task); | 501 completion_task); |
| 425 } | 502 } |
| 426 | 503 |
| 427 bool AcceleratedSurface::Present(HWND window) { | 504 bool AcceleratedSurface::Present(HWND window) { |
| 428 if (presenter_) | 505 if (presenter_) |
| 429 return presenter_->Present(window); | 506 return presenter_->Present(window); |
| 430 else | 507 else |
| 431 return false; | 508 return false; |
| 432 } | 509 } |
| 433 | 510 |
| 511 bool AcceleratedSurface::CopyTo(const gfx::Size& size, | |
| 512 std::vector<unsigned char>* buf) { | |
| 513 return presenter_->CopyTo(size, buf); | |
| 514 } | |
| 515 | |
| 434 void AcceleratedSurface::Suspend() { | 516 void AcceleratedSurface::Suspend() { |
| 435 if (presenter_) | 517 if (presenter_) |
| 436 presenter_->Suspend(); | 518 presenter_->Suspend(); |
| 437 } | 519 } |
| 438 | |
| OLD | NEW |