| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/compositor/compositor.h" | 5 #include "ui/gfx/compositor/compositor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <d3dx10.h> | 8 #include <d3dx10.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 public: | 49 public: |
| 50 ViewTexture(CompositorWin* compositor, | 50 ViewTexture(CompositorWin* compositor, |
| 51 ID3D10Device* device, | 51 ID3D10Device* device, |
| 52 ID3D10Effect* effect); | 52 ID3D10Effect* effect); |
| 53 | 53 |
| 54 // Texture: | 54 // Texture: |
| 55 virtual void SetCanvas(const SkCanvas& canvas, | 55 virtual void SetCanvas(const SkCanvas& canvas, |
| 56 const gfx::Point& origin, | 56 const gfx::Point& origin, |
| 57 const gfx::Size& overall_size) OVERRIDE; | 57 const gfx::Size& overall_size) OVERRIDE; |
| 58 virtual void Draw(const ui::TextureDrawParams& params) OVERRIDE; | 58 virtual void Draw(const ui::TextureDrawParams& params) OVERRIDE; |
| 59 | |
| 60 virtual void Draw(const ui::TextureDrawParams& params, | 59 virtual void Draw(const ui::TextureDrawParams& params, |
| 61 const gfx::Rect& clip_bounds_in_texture) OVERRIDE; | 60 const gfx::Rect& clip_bounds_in_texture) OVERRIDE; |
| 62 | 61 |
| 63 private: | 62 private: |
| 64 ~ViewTexture(); | 63 ~ViewTexture(); |
| 65 | 64 |
| 66 void Errored(HRESULT result); | 65 void Errored(HRESULT result); |
| 67 | 66 |
| 68 void ConvertBitmapToD3DData(const SkBitmap& bitmap, | 67 void ConvertBitmapToD3DData(const SkBitmap& bitmap, |
| 69 scoped_array<uint32>* converted_data); | 68 scoped_array<uint32>* converted_data); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 90 CompositorWin(gfx::AcceleratedWidget widget, | 89 CompositorWin(gfx::AcceleratedWidget widget, |
| 91 const gfx::Size& size); | 90 const gfx::Size& size); |
| 92 | 91 |
| 93 void Init(); | 92 void Init(); |
| 94 | 93 |
| 95 // Invoked to update the perspective needed by this texture. |transform| is | 94 // Invoked to update the perspective needed by this texture. |transform| is |
| 96 // the transform for the texture, and |size| the size of the texture. | 95 // the transform for the texture, and |size| the size of the texture. |
| 97 void UpdatePerspective(const ui::Transform& transform, | 96 void UpdatePerspective(const ui::Transform& transform, |
| 98 const gfx::Size& view_size); | 97 const gfx::Size& view_size); |
| 99 | 98 |
| 100 // Returns the overall size of the compositor. | |
| 101 const gfx::Size& GetHostSize(); | |
| 102 | |
| 103 // Returns the index buffer used for drawing a texture. | 99 // Returns the index buffer used for drawing a texture. |
| 104 ID3D10Buffer* GetTextureIndexBuffer(); | 100 ID3D10Buffer* GetTextureIndexBuffer(); |
| 105 | 101 |
| 106 // Compositor: | 102 // Compositor: |
| 107 virtual Texture* CreateTexture() OVERRIDE; | 103 virtual Texture* CreateTexture() OVERRIDE; |
| 108 virtual void NotifyStart() OVERRIDE; | 104 virtual void NotifyStart() OVERRIDE; |
| 109 virtual void NotifyEnd() OVERRIDE; | 105 virtual void NotifyEnd() OVERRIDE; |
| 110 virtual void Blur(const gfx::Rect& bounds) OVERRIDE; | 106 virtual void Blur(const gfx::Rect& bounds) OVERRIDE; |
| 111 virtual void SchedulePaint() OVERRIDE; | 107 virtual void SchedulePaint() OVERRIDE; |
| 112 virtual void OnWidgetSizeChanged(const gfx::Size& size) OVERRIDE; | 108 |
| 109 protected: |
| 110 virtual void OnWidgetSizeChanged() OVERRIDE; |
| 113 | 111 |
| 114 private: | 112 private: |
| 115 enum Direction { | 113 enum Direction { |
| 116 HORIZONTAL, | 114 HORIZONTAL, |
| 117 VERTICAL | 115 VERTICAL |
| 118 }; | 116 }; |
| 119 | 117 |
| 120 ~CompositorWin(); | 118 ~CompositorWin(); |
| 121 | 119 |
| 122 void Errored(HRESULT error_code); | 120 void Errored(HRESULT error_code); |
| 123 | 121 |
| 124 void CreateDevice(); | 122 void CreateDevice(); |
| 125 | 123 |
| 126 void LoadEffects(); | 124 void LoadEffects(); |
| 127 | 125 |
| 128 void InitVertexLayout(); | 126 void InitVertexLayout(); |
| 129 | 127 |
| 130 void Resize(const gfx::Size& size); | |
| 131 | |
| 132 // Updates the kernel used for blurring. Size is the size of the texture | 128 // Updates the kernel used for blurring. Size is the size of the texture |
| 133 // being drawn to along the appropriate axis. | 129 // being drawn to along the appropriate axis. |
| 134 void UpdateBlurKernel(Direction direction, int size); | 130 void UpdateBlurKernel(Direction direction, int size); |
| 135 | 131 |
| 136 // Creates a texture, render target view and shader. | 132 // Creates a texture, render target view and shader. |
| 137 void CreateTexture(const gfx::Size& size, | 133 void CreateTexture(const gfx::Size& size, |
| 138 ID3D10Texture2D** texture, | 134 ID3D10Texture2D** texture, |
| 139 ID3D10RenderTargetView** render_target_view, | 135 ID3D10RenderTargetView** render_target_view, |
| 140 ID3D10ShaderResourceView** shader_resource_view); | 136 ID3D10ShaderResourceView** shader_resource_view); |
| 141 | 137 |
| 142 // Creates |vertex_buffer_|. | 138 // Creates |vertex_buffer_|. |
| 143 void CreateVertexBuffer(); | 139 void CreateVertexBuffer(); |
| 144 | 140 |
| 145 // Creates |index_buffer_|. | 141 // Creates |index_buffer_|. |
| 146 void CreateIndexBuffer(); | 142 void CreateIndexBuffer(); |
| 147 | 143 |
| 148 // Creates a vertex buffer for the specified region. The caller owns the | 144 // Creates a vertex buffer for the specified region. The caller owns the |
| 149 // return value. | 145 // return value. |
| 150 ID3D10Buffer* CreateVertexBufferForRegion(const gfx::Rect& bounds); | 146 ID3D10Buffer* CreateVertexBufferForRegion(const gfx::Rect& bounds); |
| 151 | 147 |
| 152 gfx::AcceleratedWidget host_; | 148 gfx::AcceleratedWidget host_; |
| 153 | 149 |
| 154 // Size the device was last created at. | |
| 155 gfx::Size last_size_; | |
| 156 | |
| 157 ScopedComPtr<ID3D10Device> device_; | 150 ScopedComPtr<ID3D10Device> device_; |
| 158 ScopedComPtr<IDXGISwapChain> swap_chain_; | 151 ScopedComPtr<IDXGISwapChain> swap_chain_; |
| 159 ScopedComPtr<ID3D10RenderTargetView> dest_render_target_view_; | 152 ScopedComPtr<ID3D10RenderTargetView> dest_render_target_view_; |
| 160 ScopedComPtr<ID3D10Texture2D> depth_stencil_buffer_; | 153 ScopedComPtr<ID3D10Texture2D> depth_stencil_buffer_; |
| 161 ScopedComPtr<ID3D10DepthStencilView> depth_stencil_view_; | 154 ScopedComPtr<ID3D10DepthStencilView> depth_stencil_view_; |
| 162 ScopedComPtr<ID3D10Effect, NULL> fx_; | 155 ScopedComPtr<ID3D10Effect, NULL> fx_; |
| 163 ID3D10EffectTechnique* technique_; | 156 ID3D10EffectTechnique* technique_; |
| 164 | 157 |
| 165 // Layout for Vertex. | 158 // Layout for Vertex. |
| 166 ScopedComPtr<ID3D10InputLayout> vertex_layout_; | 159 ScopedComPtr<ID3D10InputLayout> vertex_layout_; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 (SkColorGetA(color) << 24) | | 291 (SkColorGetA(color) << 24) | |
| 299 (SkColorGetB(color) << 16) | | 292 (SkColorGetB(color) << 16) | |
| 300 (SkColorGetG(color) << 8) | | 293 (SkColorGetG(color) << 8) | |
| 301 (SkColorGetR(color)); | 294 (SkColorGetR(color)); |
| 302 } | 295 } |
| 303 } | 296 } |
| 304 } | 297 } |
| 305 | 298 |
| 306 void ViewTexture::CreateVertexBuffer(const gfx::Size& size) { | 299 void ViewTexture::CreateVertexBuffer(const gfx::Size& size) { |
| 307 vertex_buffer_.Release(); | 300 vertex_buffer_.Release(); |
| 308 const gfx::Size& host_size = compositor_->GetHostSize(); | 301 const gfx::Size& host_size = compositor_->size(); |
| 309 float x = static_cast<float>(host_size.width()) / 2.0f; | 302 float x = static_cast<float>(host_size.width()) / 2.0f; |
| 310 float y = static_cast<float>(host_size.height()) / 2.0f; | 303 float y = static_cast<float>(host_size.height()) / 2.0f; |
| 311 float w = static_cast<float>(size.width()); | 304 float w = static_cast<float>(size.width()); |
| 312 float h = static_cast<float>(size.height()); | 305 float h = static_cast<float>(size.height()); |
| 313 Vertex vertices[] = { | 306 Vertex vertices[] = { |
| 314 { D3DXVECTOR3(0.0f, -h, 0.0f), D3DXVECTOR2(0.0f, 1.0f) }, | 307 { D3DXVECTOR3(0.0f, -h, 0.0f), D3DXVECTOR2(0.0f, 1.0f) }, |
| 315 { D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR2(0.0f, 0.0f) }, | 308 { D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR2(0.0f, 0.0f) }, |
| 316 { D3DXVECTOR3( w, 0.0f, 0.0f), D3DXVECTOR2(1.0f, 0.0f) }, | 309 { D3DXVECTOR3( w, 0.0f, 0.0f), D3DXVECTOR2(1.0f, 0.0f) }, |
| 317 { D3DXVECTOR3( w, -h, 0.0f), D3DXVECTOR2(1.0f, 1.0f) }, | 310 { D3DXVECTOR3( w, -h, 0.0f), D3DXVECTOR2(1.0f, 1.0f) }, |
| 318 }; | 311 }; |
| 319 | 312 |
| 320 // Create the vertex buffer containing the points. | 313 // Create the vertex buffer containing the points. |
| 321 D3D10_BUFFER_DESC buffer_desc; | 314 D3D10_BUFFER_DESC buffer_desc; |
| 322 buffer_desc.Usage = D3D10_USAGE_IMMUTABLE; | 315 buffer_desc.Usage = D3D10_USAGE_IMMUTABLE; |
| 323 buffer_desc.ByteWidth = sizeof(Vertex) * ARRAYSIZE_UNSAFE(vertices); | 316 buffer_desc.ByteWidth = sizeof(Vertex) * ARRAYSIZE_UNSAFE(vertices); |
| 324 buffer_desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; | 317 buffer_desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; |
| 325 buffer_desc.CPUAccessFlags = 0; | 318 buffer_desc.CPUAccessFlags = 0; |
| 326 buffer_desc.MiscFlags = 0; | 319 buffer_desc.MiscFlags = 0; |
| 327 D3D10_SUBRESOURCE_DATA init_data; | 320 D3D10_SUBRESOURCE_DATA init_data; |
| 328 init_data.pSysMem = vertices; | 321 init_data.pSysMem = vertices; |
| 329 RETURN_IF_FAILED(device_->CreateBuffer(&buffer_desc, &init_data, | 322 RETURN_IF_FAILED(device_->CreateBuffer(&buffer_desc, &init_data, |
| 330 vertex_buffer_.Receive())); | 323 vertex_buffer_.Receive())); |
| 331 } | 324 } |
| 332 | 325 |
| 333 CompositorWin::CompositorWin(gfx::AcceleratedWidget widget, | 326 CompositorWin::CompositorWin(gfx::AcceleratedWidget widget, |
| 334 const gfx::Size& size) | 327 const gfx::Size& size) |
| 335 : host_(widget), | 328 : Compositor(size), |
| 336 technique_(NULL), | 329 host_(widget), |
| 337 last_size_(size) { | 330 technique_(NULL) { |
| 338 } | 331 } |
| 339 | 332 |
| 340 void CompositorWin::Init() { | 333 void CompositorWin::Init() { |
| 341 CreateDevice(); | 334 CreateDevice(); |
| 342 LoadEffects(); | 335 LoadEffects(); |
| 343 Resize(last_size_); | 336 OnWidgetSizeChanged(); |
| 344 InitVertexLayout(); | 337 InitVertexLayout(); |
| 345 CreateVertexBuffer(); | 338 CreateVertexBuffer(); |
| 346 CreateIndexBuffer(); | 339 CreateIndexBuffer(); |
| 347 } | 340 } |
| 348 | 341 |
| 349 void CompositorWin::UpdatePerspective(const ui::Transform& transform, | 342 void CompositorWin::UpdatePerspective(const ui::Transform& transform, |
| 350 const gfx::Size& view_size) { | 343 const gfx::Size& view_size) { |
| 351 float transform_data_buffer[16]; | 344 float transform_data_buffer[16]; |
| 352 transform.matrix().asColMajorf(transform_data_buffer); | 345 transform.matrix().asColMajorf(transform_data_buffer); |
| 353 D3DXMATRIX transform_matrix(&transform_data_buffer[0]); | 346 D3DXMATRIX transform_matrix(&transform_data_buffer[0]); |
| 354 std::swap(transform_matrix._12, transform_matrix._21); | 347 std::swap(transform_matrix._12, transform_matrix._21); |
| 355 std::swap(transform_matrix._13, transform_matrix._31); | 348 std::swap(transform_matrix._13, transform_matrix._31); |
| 356 std::swap(transform_matrix._23, transform_matrix._32); | 349 std::swap(transform_matrix._23, transform_matrix._32); |
| 357 | 350 |
| 358 // Different coordinate system; flip the y. | 351 // Different coordinate system; flip the y. |
| 359 transform_matrix._42 *= -1; | 352 transform_matrix._42 *= -1; |
| 360 | 353 |
| 361 // Scale so x and y are from 0-2. | 354 // Scale so x and y are from 0-2. |
| 362 D3DXMATRIX scale_matrix; | 355 D3DXMATRIX scale_matrix; |
| 363 D3DXMatrixScaling( | 356 D3DXMatrixScaling( |
| 364 &scale_matrix, | 357 &scale_matrix, |
| 365 2.0f / static_cast<float>(last_size_.width()), | 358 2.0f / static_cast<float>(size().width()), |
| 366 2.0f / static_cast<float>(last_size_.height()), | 359 2.0f / static_cast<float>(size().height()), |
| 367 1.0f); | 360 1.0f); |
| 368 | 361 |
| 369 // Translate so x and y are from -1,-1 to 1,1. | 362 // Translate so x and y are from -1,-1 to 1,1. |
| 370 D3DXMATRIX translate_matrix; | 363 D3DXMATRIX translate_matrix; |
| 371 D3DXMatrixTranslation(&translate_matrix, -1.0f, 1.0f, 0.0f); | 364 D3DXMatrixTranslation(&translate_matrix, -1.0f, 1.0f, 0.0f); |
| 372 | 365 |
| 373 D3DXMATRIX projection_matrix; | 366 D3DXMATRIX projection_matrix; |
| 374 D3DXMatrixIdentity(&projection_matrix); | 367 D3DXMatrixIdentity(&projection_matrix); |
| 375 D3DXMatrixPerspectiveFovLH(&projection_matrix, | 368 D3DXMatrixPerspectiveFovLH(&projection_matrix, |
| 376 atanf(.5f) * 2.0f, 1.0f, 1.0f, 1000.0f); | 369 atanf(.5f) * 2.0f, 1.0f, 1.0f, 1000.0f); |
| 377 D3DXVECTOR3 pos(0.0f, 0.0f, -2.0f); | 370 D3DXVECTOR3 pos(0.0f, 0.0f, -2.0f); |
| 378 D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); | 371 D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); |
| 379 D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); | 372 D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); |
| 380 D3DXMATRIX view; | 373 D3DXMATRIX view; |
| 381 D3DXMatrixIdentity(&view); | 374 D3DXMatrixIdentity(&view); |
| 382 D3DXMatrixLookAtLH(&view, &pos, &target, &up); | 375 D3DXMatrixLookAtLH(&view, &pos, &target, &up); |
| 383 | 376 |
| 384 D3DXMATRIX wvp = transform_matrix * scale_matrix * translate_matrix * view * | 377 D3DXMATRIX wvp = transform_matrix * scale_matrix * translate_matrix * view * |
| 385 projection_matrix; | 378 projection_matrix; |
| 386 | 379 |
| 387 fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix(wvp); | 380 fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix(wvp); |
| 388 } | 381 } |
| 389 | 382 |
| 390 const gfx::Size& CompositorWin::GetHostSize() { | |
| 391 return last_size_; | |
| 392 } | |
| 393 | |
| 394 ID3D10Buffer* CompositorWin::GetTextureIndexBuffer() { | 383 ID3D10Buffer* CompositorWin::GetTextureIndexBuffer() { |
| 395 return index_buffer_.get(); | 384 return index_buffer_.get(); |
| 396 } | 385 } |
| 397 | 386 |
| 398 Texture* CompositorWin::CreateTexture() { | 387 Texture* CompositorWin::CreateTexture() { |
| 399 return new ViewTexture(this, device_.get(), fx_.get()); | 388 return new ViewTexture(this, device_.get(), fx_.get()); |
| 400 } | 389 } |
| 401 | 390 |
| 402 void CompositorWin::NotifyStart() { | 391 void CompositorWin::NotifyStart() { |
| 403 ID3D10RenderTargetView* target_view = main_render_target_view_.get(); | 392 ID3D10RenderTargetView* target_view = main_render_target_view_.get(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 ID3D10Buffer* vertex_buffer = scoped_vertex_buffer.get(); | 451 ID3D10Buffer* vertex_buffer = scoped_vertex_buffer.get(); |
| 463 UINT stride = sizeof(Vertex); | 452 UINT stride = sizeof(Vertex); |
| 464 UINT offset = 0; | 453 UINT offset = 0; |
| 465 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); | 454 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); |
| 466 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); | 455 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); |
| 467 device_->DrawIndexed(6, 0, 0); | 456 device_->DrawIndexed(6, 0, 0); |
| 468 D3DXMATRIX identity_matrix; | 457 D3DXMATRIX identity_matrix; |
| 469 D3DXMatrixIdentity(&identity_matrix); | 458 D3DXMatrixIdentity(&identity_matrix); |
| 470 | 459 |
| 471 // Horizontal blur from the main texture to blur texture. | 460 // Horizontal blur from the main texture to blur texture. |
| 472 UpdateBlurKernel(HORIZONTAL, last_size_.width()); | 461 UpdateBlurKernel(HORIZONTAL, size().width()); |
| 473 ID3D10RenderTargetView* target_view = blur_render_target_view_.get(); | 462 ID3D10RenderTargetView* target_view = blur_render_target_view_.get(); |
| 474 device_->OMSetRenderTargets(1, &target_view, NULL); | 463 device_->OMSetRenderTargets(1, &target_view, NULL); |
| 475 RETURN_IF_FAILED( | 464 RETURN_IF_FAILED( |
| 476 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> | 465 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> |
| 477 SetResource(main_texture_shader_view_.get())); | 466 SetResource(main_texture_shader_view_.get())); |
| 478 blur_fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix( | 467 blur_fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix( |
| 479 identity_matrix); | 468 identity_matrix); |
| 480 ID3D10EffectTechnique* technique = blur_fx_->GetTechniqueByName("ViewTech"); | 469 ID3D10EffectTechnique* technique = blur_fx_->GetTechniqueByName("ViewTech"); |
| 481 DCHECK(technique); | 470 DCHECK(technique); |
| 482 D3D10_TECHNIQUE_DESC tech_desc; | 471 D3D10_TECHNIQUE_DESC tech_desc; |
| 483 technique->GetDesc(&tech_desc); | 472 technique->GetDesc(&tech_desc); |
| 484 for(UINT p = 0; p < tech_desc.Passes; ++p) | 473 for(UINT p = 0; p < tech_desc.Passes; ++p) |
| 485 technique->GetPassByIndex(p)->Apply(0); | 474 technique->GetPassByIndex(p)->Apply(0); |
| 486 device_->DrawIndexed(6, 0, 0); | 475 device_->DrawIndexed(6, 0, 0); |
| 487 | 476 |
| 488 { | 477 { |
| 489 // We do this to avoid a warning. | 478 // We do this to avoid a warning. |
| 490 ID3D10ShaderResourceView* tmp = NULL; | 479 ID3D10ShaderResourceView* tmp = NULL; |
| 491 device_->PSSetShaderResources(0, 1, &tmp); | 480 device_->PSSetShaderResources(0, 1, &tmp); |
| 492 } | 481 } |
| 493 | 482 |
| 494 // Vertical blur from the blur texture back to main buffer. | 483 // Vertical blur from the blur texture back to main buffer. |
| 495 RETURN_IF_FAILED( | 484 RETURN_IF_FAILED( |
| 496 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> | 485 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> |
| 497 SetResource(blur_texture_shader_view_.get())); | 486 SetResource(blur_texture_shader_view_.get())); |
| 498 UpdateBlurKernel(VERTICAL, last_size_.height()); | 487 UpdateBlurKernel(VERTICAL, size().height()); |
| 499 target_view = main_render_target_view_.get(); | 488 target_view = main_render_target_view_.get(); |
| 500 device_->OMSetRenderTargets(1, &target_view, NULL); | 489 device_->OMSetRenderTargets(1, &target_view, NULL); |
| 501 for(UINT p = 0; p < tech_desc.Passes; ++p) | 490 for(UINT p = 0; p < tech_desc.Passes; ++p) |
| 502 technique->GetPassByIndex(p)->Apply(0); | 491 technique->GetPassByIndex(p)->Apply(0); |
| 503 device_->DrawIndexed(6, 0, 0); | 492 device_->DrawIndexed(6, 0, 0); |
| 504 | 493 |
| 505 #if !defined(NDEBUG) | 494 #if !defined(NDEBUG) |
| 506 // A warning is generated if a bound vertexbuffer is deleted. To avoid that | 495 // A warning is generated if a bound vertexbuffer is deleted. To avoid that |
| 507 // warning we reset the buffer here. We only do it for debug builds as it's | 496 // warning we reset the buffer here. We only do it for debug builds as it's |
| 508 // ok to effectively unbind the vertex buffer. | 497 // ok to effectively unbind the vertex buffer. |
| 509 vertex_buffer = vertex_buffer_.get(); | 498 vertex_buffer = vertex_buffer_.get(); |
| 510 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); | 499 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); |
| 511 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); | 500 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); |
| 512 #endif | 501 #endif |
| 513 } | 502 } |
| 514 | 503 |
| 515 void CompositorWin::SchedulePaint() { | 504 void CompositorWin::SchedulePaint() { |
| 516 RECT bounds; | 505 RECT bounds; |
| 517 GetClientRect(host_, &bounds); | 506 GetClientRect(host_, &bounds); |
| 518 InvalidateRect(host_, &bounds, FALSE); | 507 InvalidateRect(host_, &bounds, FALSE); |
| 519 } | 508 } |
| 520 | 509 |
| 521 void CompositorWin::OnWidgetSizeChanged(const gfx::Size& size) { | 510 void CompositorWin::OnWidgetSizeChanged() { |
| 522 Resize(size); | 511 dest_render_target_view_ = NULL; |
| 512 depth_stencil_buffer_ = NULL; |
| 513 depth_stencil_view_ = NULL; |
| 514 |
| 515 main_render_target_view_ = NULL; |
| 516 main_texture_ = NULL; |
| 517 main_texture_shader_view_ = NULL; |
| 518 |
| 519 blur_render_target_view_ = NULL; |
| 520 blur_texture_ = NULL; |
| 521 blur_texture_shader_view_ = NULL; |
| 522 |
| 523 CreateTexture(size(), main_texture_.Receive(), |
| 524 main_render_target_view_.Receive(), |
| 525 main_texture_shader_view_.Receive()); |
| 526 |
| 527 CreateTexture(size(), blur_texture_.Receive(), |
| 528 blur_render_target_view_.Receive(), |
| 529 blur_texture_shader_view_.Receive()); |
| 530 |
| 531 // Resize the swap chain and recreate the render target view. |
| 532 RETURN_IF_FAILED(swap_chain_->ResizeBuffers( |
| 533 1, size().width(), size().height(), DXGI_FORMAT_R8G8B8A8_UNORM, 0)); |
| 534 ScopedComPtr<ID3D10Texture2D> back_buffer; |
| 535 RETURN_IF_FAILED(swap_chain_->GetBuffer( |
| 536 0, __uuidof(ID3D10Texture2D), |
| 537 reinterpret_cast<void**>(back_buffer.Receive()))); |
| 538 RETURN_IF_FAILED(device_->CreateRenderTargetView( |
| 539 back_buffer.get(), 0, |
| 540 dest_render_target_view_.Receive())); |
| 541 |
| 542 // Create the depth/stencil buffer and view. |
| 543 D3D10_TEXTURE2D_DESC depth_stencil_desc; |
| 544 depth_stencil_desc.Width = size().width(); |
| 545 depth_stencil_desc.Height = size().height(); |
| 546 depth_stencil_desc.MipLevels = 1; |
| 547 depth_stencil_desc.ArraySize = 1; |
| 548 depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; |
| 549 depth_stencil_desc.SampleDesc.Count = 1; // multisampling must match |
| 550 depth_stencil_desc.SampleDesc.Quality = 0; // swap chain values. |
| 551 depth_stencil_desc.Usage = D3D10_USAGE_DEFAULT; |
| 552 depth_stencil_desc.BindFlags = D3D10_BIND_DEPTH_STENCIL; |
| 553 depth_stencil_desc.CPUAccessFlags = 0; |
| 554 depth_stencil_desc.MiscFlags = 0; |
| 555 |
| 556 RETURN_IF_FAILED(device_->CreateTexture2D(&depth_stencil_desc, 0, |
| 557 depth_stencil_buffer_.Receive())); |
| 558 RETURN_IF_FAILED(device_->CreateDepthStencilView( |
| 559 depth_stencil_buffer_.get(), 0, |
| 560 depth_stencil_view_.Receive())); |
| 561 |
| 562 |
| 563 // Set the viewport transform. |
| 564 D3D10_VIEWPORT vp; |
| 565 vp.TopLeftX = 0; |
| 566 vp.TopLeftY = 0; |
| 567 vp.Width = size().width(); |
| 568 vp.Height = size().height(); |
| 569 vp.MinDepth = 0.0f; |
| 570 vp.MaxDepth = 1.0f; |
| 571 |
| 572 device_->RSSetViewports(1, &vp); |
| 523 } | 573 } |
| 524 | 574 |
| 525 CompositorWin::~CompositorWin() { | 575 CompositorWin::~CompositorWin() { |
| 526 } | 576 } |
| 527 | 577 |
| 528 void CompositorWin::Errored(HRESULT error_code) { | 578 void CompositorWin::Errored(HRESULT error_code) { |
| 529 // TODO: figure out error handling. | 579 // TODO: figure out error handling. |
| 530 DCHECK(false); | 580 DCHECK(false); |
| 531 } | 581 } |
| 532 | 582 |
| 533 void CompositorWin::CreateDevice() { | 583 void CompositorWin::CreateDevice() { |
| 534 DXGI_SWAP_CHAIN_DESC sd; | 584 DXGI_SWAP_CHAIN_DESC sd; |
| 535 sd.BufferDesc.Width = last_size_.width(); | 585 sd.BufferDesc.Width = size().width(); |
| 536 sd.BufferDesc.Height = last_size_.height(); | 586 sd.BufferDesc.Height = size().height(); |
| 537 sd.BufferDesc.RefreshRate.Numerator = 60; | 587 sd.BufferDesc.RefreshRate.Numerator = 60; |
| 538 sd.BufferDesc.RefreshRate.Denominator = 1; | 588 sd.BufferDesc.RefreshRate.Denominator = 1; |
| 539 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | 589 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 540 sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; | 590 sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; |
| 541 sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; | 591 sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; |
| 542 | 592 |
| 543 // No multisampling. | 593 // No multisampling. |
| 544 sd.SampleDesc.Count = 1; | 594 sd.SampleDesc.Count = 1; |
| 545 sd.SampleDesc.Quality = 0; | 595 sd.SampleDesc.Quality = 0; |
| 546 | 596 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 // Create the input layout | 659 // Create the input layout |
| 610 D3D10_PASS_DESC pass_desc; | 660 D3D10_PASS_DESC pass_desc; |
| 611 RETURN_IF_FAILED(technique_->GetPassByIndex(0)->GetDesc(&pass_desc)); | 661 RETURN_IF_FAILED(technique_->GetPassByIndex(0)->GetDesc(&pass_desc)); |
| 612 RETURN_IF_FAILED( | 662 RETURN_IF_FAILED( |
| 613 device_->CreateInputLayout(vertex_desc, ARRAYSIZE_UNSAFE(vertex_desc), | 663 device_->CreateInputLayout(vertex_desc, ARRAYSIZE_UNSAFE(vertex_desc), |
| 614 pass_desc.pIAInputSignature, | 664 pass_desc.pIAInputSignature, |
| 615 pass_desc.IAInputSignatureSize, | 665 pass_desc.IAInputSignatureSize, |
| 616 vertex_layout_.Receive())); | 666 vertex_layout_.Receive())); |
| 617 } | 667 } |
| 618 | 668 |
| 619 void CompositorWin::Resize(const gfx::Size& size) { | |
| 620 last_size_ = size; | |
| 621 | |
| 622 dest_render_target_view_ = NULL; | |
| 623 depth_stencil_buffer_ = NULL; | |
| 624 depth_stencil_view_ = NULL; | |
| 625 | |
| 626 main_render_target_view_ = NULL; | |
| 627 main_texture_ = NULL; | |
| 628 main_texture_shader_view_ = NULL; | |
| 629 | |
| 630 blur_render_target_view_ = NULL; | |
| 631 blur_texture_ = NULL; | |
| 632 blur_texture_shader_view_ = NULL; | |
| 633 | |
| 634 CreateTexture(size, main_texture_.Receive(), | |
| 635 main_render_target_view_.Receive(), | |
| 636 main_texture_shader_view_.Receive()); | |
| 637 | |
| 638 CreateTexture(size, blur_texture_.Receive(), | |
| 639 blur_render_target_view_.Receive(), | |
| 640 blur_texture_shader_view_.Receive()); | |
| 641 | |
| 642 // Resize the swap chain and recreate the render target view. | |
| 643 RETURN_IF_FAILED(swap_chain_->ResizeBuffers( | |
| 644 1, size.width(), size.height(), DXGI_FORMAT_R8G8B8A8_UNORM, 0)); | |
| 645 ScopedComPtr<ID3D10Texture2D> back_buffer; | |
| 646 RETURN_IF_FAILED(swap_chain_->GetBuffer( | |
| 647 0, __uuidof(ID3D10Texture2D), | |
| 648 reinterpret_cast<void**>(back_buffer.Receive()))); | |
| 649 RETURN_IF_FAILED(device_->CreateRenderTargetView( | |
| 650 back_buffer.get(), 0, | |
| 651 dest_render_target_view_.Receive())); | |
| 652 | |
| 653 // Create the depth/stencil buffer and view. | |
| 654 D3D10_TEXTURE2D_DESC depth_stencil_desc; | |
| 655 depth_stencil_desc.Width = size.width(); | |
| 656 depth_stencil_desc.Height = size.height(); | |
| 657 depth_stencil_desc.MipLevels = 1; | |
| 658 depth_stencil_desc.ArraySize = 1; | |
| 659 depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; | |
| 660 depth_stencil_desc.SampleDesc.Count = 1; // multisampling must match | |
| 661 depth_stencil_desc.SampleDesc.Quality = 0; // swap chain values. | |
| 662 depth_stencil_desc.Usage = D3D10_USAGE_DEFAULT; | |
| 663 depth_stencil_desc.BindFlags = D3D10_BIND_DEPTH_STENCIL; | |
| 664 depth_stencil_desc.CPUAccessFlags = 0; | |
| 665 depth_stencil_desc.MiscFlags = 0; | |
| 666 | |
| 667 RETURN_IF_FAILED(device_->CreateTexture2D(&depth_stencil_desc, 0, | |
| 668 depth_stencil_buffer_.Receive())); | |
| 669 RETURN_IF_FAILED(device_->CreateDepthStencilView( | |
| 670 depth_stencil_buffer_.get(), 0, | |
| 671 depth_stencil_view_.Receive())); | |
| 672 | |
| 673 | |
| 674 // Set the viewport transform. | |
| 675 D3D10_VIEWPORT vp; | |
| 676 vp.TopLeftX = 0; | |
| 677 vp.TopLeftY = 0; | |
| 678 vp.Width = size.width(); | |
| 679 vp.Height = size.height(); | |
| 680 vp.MinDepth = 0.0f; | |
| 681 vp.MaxDepth = 1.0f; | |
| 682 | |
| 683 device_->RSSetViewports(1, &vp); | |
| 684 } | |
| 685 | |
| 686 void CompositorWin::UpdateBlurKernel(Direction direction, int size) { | 669 void CompositorWin::UpdateBlurKernel(Direction direction, int size) { |
| 687 // Update the blur data. | 670 // Update the blur data. |
| 688 const int kSize = 13; | 671 const int kSize = 13; |
| 689 float pixel_data[4 * kSize]; | 672 float pixel_data[4 * kSize]; |
| 690 memset(pixel_data, 0, sizeof(float) * 4 * kSize); | 673 memset(pixel_data, 0, sizeof(float) * 4 * kSize); |
| 691 for (int i = 0; i < kSize; ++i) { | 674 for (int i = 0; i < kSize; ++i) { |
| 692 pixel_data[i * 4 + ((direction == HORIZONTAL) ? 0 : 1)] = | 675 pixel_data[i * 4 + ((direction == HORIZONTAL) ? 0 : 1)] = |
| 693 static_cast<float>(i - (kSize / 2)) / static_cast<float>(size); | 676 static_cast<float>(i - (kSize / 2)) / static_cast<float>(size); |
| 694 } | 677 } |
| 695 RETURN_IF_FAILED(blur_fx_->GetVariableByName("TexelKernel")->AsVector()-> | 678 RETURN_IF_FAILED(blur_fx_->GetVariableByName("TexelKernel")->AsVector()-> |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 index_buffer.MiscFlags = 0; | 749 index_buffer.MiscFlags = 0; |
| 767 D3D10_SUBRESOURCE_DATA init_data; | 750 D3D10_SUBRESOURCE_DATA init_data; |
| 768 init_data.pSysMem = indices; | 751 init_data.pSysMem = indices; |
| 769 RETURN_IF_FAILED(device_->CreateBuffer(&index_buffer, &init_data, | 752 RETURN_IF_FAILED(device_->CreateBuffer(&index_buffer, &init_data, |
| 770 index_buffer_.Receive())); | 753 index_buffer_.Receive())); |
| 771 } | 754 } |
| 772 | 755 |
| 773 ID3D10Buffer* CompositorWin::CreateVertexBufferForRegion( | 756 ID3D10Buffer* CompositorWin::CreateVertexBufferForRegion( |
| 774 const gfx::Rect& bounds) { | 757 const gfx::Rect& bounds) { |
| 775 float x = static_cast<float>(bounds.x()) / | 758 float x = static_cast<float>(bounds.x()) / |
| 776 static_cast<float>(last_size_.width()) * 2.0f - 1.0f; | 759 static_cast<float>(size().width()) * 2.0f - 1.0f; |
| 777 float max_x = | 760 float max_x = |
| 778 x + bounds.width() / static_cast<float>(last_size_.width()) * 2.0f; | 761 x + bounds.width() / static_cast<float>(size().width()) * 2.0f; |
| 779 float y = | 762 float y = |
| 780 static_cast<float>(last_size_.height() - bounds.y() - bounds.height()) / | 763 static_cast<float>(size().height() - bounds.y() - bounds.height()) / |
| 781 static_cast<float>(last_size_.height()) * 2.0f - 1.0f; | 764 static_cast<float>(size().height()) * 2.0f - 1.0f; |
| 782 float max_y = | 765 float max_y = |
| 783 y + bounds.height() / static_cast<float>(last_size_.height()) * 2.0f; | 766 y + bounds.height() / static_cast<float>(size().height()) * 2.0f; |
| 784 float tex_x = x / 2.0f + .5f; | 767 float tex_x = x / 2.0f + .5f; |
| 785 float max_tex_x = max_x / 2.0f + .5f; | 768 float max_tex_x = max_x / 2.0f + .5f; |
| 786 float tex_y = 1.0f - (max_y + 1.0f) / 2.0f; | 769 float tex_y = 1.0f - (max_y + 1.0f) / 2.0f; |
| 787 float max_tex_y = tex_y + (max_y - y) / 2.0f; | 770 float max_tex_y = tex_y + (max_y - y) / 2.0f; |
| 788 Vertex vertices[] = { | 771 Vertex vertices[] = { |
| 789 { D3DXVECTOR3( x, y, 0.0f), D3DXVECTOR2( tex_x, max_tex_y) }, | 772 { D3DXVECTOR3( x, y, 0.0f), D3DXVECTOR2( tex_x, max_tex_y) }, |
| 790 { D3DXVECTOR3( x, max_y, 0.0f), D3DXVECTOR2( tex_x, tex_y) }, | 773 { D3DXVECTOR3( x, max_y, 0.0f), D3DXVECTOR2( tex_x, tex_y) }, |
| 791 { D3DXVECTOR3(max_x, max_y, 0.0f), D3DXVECTOR2(max_tex_x, tex_y) }, | 774 { D3DXVECTOR3(max_x, max_y, 0.0f), D3DXVECTOR2(max_tex_x, tex_y) }, |
| 792 { D3DXVECTOR3(max_x, y, 0.0f), D3DXVECTOR2(max_tex_x, max_tex_y) }, | 775 { D3DXVECTOR3(max_x, y, 0.0f), D3DXVECTOR2(max_tex_x, max_tex_y) }, |
| 793 }; | 776 }; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 816 | 799 |
| 817 // static | 800 // static |
| 818 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, | 801 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, |
| 819 const gfx::Size& size) { | 802 const gfx::Size& size) { |
| 820 CompositorWin* compositor = new CompositorWin(widget, size); | 803 CompositorWin* compositor = new CompositorWin(widget, size); |
| 821 compositor->Init(); | 804 compositor->Init(); |
| 822 return compositor; | 805 return compositor; |
| 823 } | 806 } |
| 824 | 807 |
| 825 } // namespace ui | 808 } // namespace ui |
| OLD | NEW |