OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/surfaces/test_surface_overlay_view.h" |
| 6 |
| 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> |
| 9 #include <GLES2/gl2extchromium.h> |
| 10 |
| 11 #include "ash/shell.h" |
| 12 #include "ash/shell_window_ids.h" |
| 13 #include "cc/output/context_provider.h" |
| 14 #include "gpu/command_buffer/client/gles2_interface.h" |
| 15 #include "gpu/command_buffer/common/mailbox.h" |
| 16 #include "ui/aura/env.h" |
| 17 #include "ui/compositor/layer.h" |
| 18 #include "ui/events/event.h" |
| 19 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/gpu_memory_buffer.h" |
| 21 |
| 22 namespace ash { |
| 23 namespace { |
| 24 |
| 25 GLenum GLInternalFormat(gfx::BufferFormat format) { |
| 26 const GLenum kGLInternalFormats[] = { |
| 27 GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, // ATC |
| 28 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // ATCIA |
| 29 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // DXT1 |
| 30 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, // DXT5 |
| 31 GL_ETC1_RGB8_OES, // ETC1 |
| 32 GL_R8_EXT, // R_8 |
| 33 GL_RGBA, // RGBA_4444 |
| 34 GL_RGBA, // RGBA_8888 |
| 35 GL_RGB, // RGBX_8888 |
| 36 GL_RGBA, // RGBA_8888 |
| 37 GL_BGRA_EXT, // BGRA_8888 |
| 38 GL_RGB_YUV_420_CHROMIUM, // YUV_420 |
| 39 GL_RGB_YCBCR_422_CHROMIUM, // YUV_420_BIPLANAR |
| 40 }; |
| 41 static_assert(arraysize(kGLInternalFormats) == |
| 42 (static_cast<int>(gfx::BufferFormat::LAST) + 1), |
| 43 "BufferFormat::LAST must be last value of kGLInternalFormats"); |
| 44 |
| 45 DCHECK(format <= gfx::BufferFormat::LAST); |
| 46 return kGLInternalFormats[static_cast<int>(format)]; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 TestSurfaceOverlayView::TestSurfaceOverlayView() |
| 52 : test_surface_layer_(new ui::Layer(ui::LAYER_TEXTURED)), |
| 53 weak_ptr_factory_(this) { |
| 54 Shell::GetInstance()->surface_controller()->AddObserver(this); |
| 55 } |
| 56 |
| 57 TestSurfaceOverlayView::~TestSurfaceOverlayView() { |
| 58 Shell::GetInstance()->surface_controller()->RemoveObserver(this); |
| 59 } |
| 60 |
| 61 void TestSurfaceOverlayView::OnKeyEvent(ui::KeyEvent* event) { |
| 62 } |
| 63 |
| 64 void TestSurfaceOverlayView::OnMouseEvent(ui::MouseEvent* event) { |
| 65 } |
| 66 |
| 67 void TestSurfaceOverlayView::OnScrollEvent(ui::ScrollEvent* event) { |
| 68 } |
| 69 |
| 70 void TestSurfaceOverlayView::OnGestureEvent(ui::GestureEvent* event) { |
| 71 } |
| 72 |
| 73 void TestSurfaceOverlayView::OnPaint(gfx::Canvas* canvas) { |
| 74 canvas->FillRect(GetLocalBounds(), SK_ColorGRAY); |
| 75 } |
| 76 |
| 77 void TestSurfaceOverlayView::WindowClosing() { |
| 78 Cancel(); |
| 79 } |
| 80 |
| 81 void TestSurfaceOverlayView::Cancel() { |
| 82 Shell::GetInstance()->overlay_filter()->Deactivate(this); |
| 83 views::Widget* widget = GetWidget(); |
| 84 if (widget) |
| 85 widget->Close(); |
| 86 } |
| 87 |
| 88 bool TestSurfaceOverlayView::IsCancelingKeyEvent(ui::KeyEvent* event) { |
| 89 if (event->type() != ui::ET_KEY_PRESSED) |
| 90 return false; |
| 91 |
| 92 // Ignore the caps lock state. |
| 93 const int flags = (event->flags() & ~ui::EF_CAPS_LOCK_DOWN); |
| 94 |
| 95 // Keys to invoke hide (Ctrl+Alt+A). |
| 96 return event->key_code() == ui::VKEY_A && |
| 97 flags == (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN); |
| 98 } |
| 99 |
| 100 aura::Window* TestSurfaceOverlayView::GetWindow() { |
| 101 return GetWidget()->GetNativeWindow(); |
| 102 } |
| 103 |
| 104 void TestSurfaceOverlayView::OnTestSurfaceGpuMemoryBufferChanged() { |
| 105 gfx::GpuMemoryBuffer* gpu_memory_buffer = |
| 106 Shell::GetInstance() |
| 107 ->surface_controller() |
| 108 ->GetTestSurfaceGpuMemoryBuffer(); |
| 109 if (!gpu_memory_buffer) { |
| 110 test_surface_layer_->SetVisible(false); |
| 111 return; |
| 112 } |
| 113 |
| 114 ui::ContextFactory* context_factory = |
| 115 aura::Env::GetInstance()->context_factory(); |
| 116 scoped_refptr<cc::ContextProvider> context_provider = |
| 117 context_factory->SharedMainThreadContextProvider(); |
| 118 gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL(); |
| 119 |
| 120 gfx::BufferFormat format = gpu_memory_buffer->GetFormat(); |
| 121 gfx::Size size = gpu_memory_buffer->GetSize(); |
| 122 |
| 123 GLenum texture_target = |
| 124 context_factory->GetImageTextureTarget(format, gfx::BufferUsage::SCANOUT); |
| 125 |
| 126 unsigned texture_id = 0; |
| 127 gles2->GenTextures(1, &texture_id); |
| 128 gles2->ActiveTexture(GL_TEXTURE0); |
| 129 gles2->BindTexture(texture_target, texture_id); |
| 130 gles2->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 131 gles2->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 132 gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 133 gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 134 unsigned image_id = gles2->CreateImageCHROMIUM( |
| 135 gpu_memory_buffer->AsClientBuffer(), size.width(), size.height(), |
| 136 GLInternalFormat(format)); |
| 137 gles2->BindTexImage2DCHROMIUM(texture_target, image_id); |
| 138 uint32 sync_point = gles2->InsertSyncPointCHROMIUM(); |
| 139 |
| 140 gpu::Mailbox mailbox; |
| 141 gles2->GenMailboxCHROMIUM(mailbox.name); |
| 142 gles2->ProduceTextureCHROMIUM(texture_target, mailbox.name); |
| 143 cc::TextureMailbox texture_mailbox(mailbox, texture_target, sync_point); |
| 144 test_surface_layer_->SetTextureMailbox( |
| 145 texture_mailbox, cc::SingleReleaseCallback::Create( |
| 146 base::Bind(&TestSurfaceOverlayView::TextureReleased, |
| 147 weak_ptr_factory_.GetWeakPtr(), |
| 148 context_provider, texture_id, image_id)), |
| 149 size); |
| 150 test_surface_layer_->SetVisible(true); |
| 151 test_surface_layer_->SetBounds(gfx::Rect(size)); |
| 152 test_surface_layer_->SchedulePaint(gfx::Rect(size)); |
| 153 } |
| 154 |
| 155 void TestSurfaceOverlayView::TextureReleased( |
| 156 scoped_refptr<cc::ContextProvider> context_provider, |
| 157 unsigned texture_id, |
| 158 unsigned image_id, |
| 159 uint32 sync_point, |
| 160 bool is_lost) { |
| 161 gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL(); |
| 162 gles2->WaitSyncPointCHROMIUM(sync_point); |
| 163 gles2->DeleteTextures(1, &texture_id); |
| 164 gles2->DestroyImageCHROMIUM(image_id); |
| 165 } |
| 166 |
| 167 // static |
| 168 void TestSurfaceOverlayView::Show() { |
| 169 if (Shell::GetInstance()->overlay_filter()->IsActive()) |
| 170 return; |
| 171 |
| 172 TestSurfaceOverlayView* view = new TestSurfaceOverlayView; |
| 173 views::Widget::InitParams params( |
| 174 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 175 params.bounds = Shell::GetScreen()->GetPrimaryDisplay().bounds(); |
| 176 params.show_state = ui::SHOW_STATE_FULLSCREEN; |
| 177 params.parent = Shell::GetContainer(Shell::GetPrimaryRootWindow(), |
| 178 kShellWindowId_OverlayContainer); |
| 179 views::Widget* widget = new views::Widget; |
| 180 widget->Init(params); |
| 181 widget->SetContentsView(view); |
| 182 widget->Show(); |
| 183 widget->GetNativeView()->SetName("TestSurfaceOverlayView"); |
| 184 widget->GetNativeView()->Focus(); |
| 185 |
| 186 Shell::GetInstance()->overlay_filter()->Activate(view); |
| 187 } |
| 188 |
| 189 } // namespace ash |
OLD | NEW |