Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3918)

Unified Diff: ash/surfaces/surface_controller.cc

Issue 1394573003: chromeos: Add SurfaceServiceProvider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ash/surfaces/surface_controller.cc
diff --git a/ash/surfaces/surface_controller.cc b/ash/surfaces/surface_controller.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3b687dc09b13e912c323534fee99ffc88461b564
--- /dev/null
+++ b/ash/surfaces/surface_controller.cc
@@ -0,0 +1,159 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/surfaces/surface_controller.h"
+
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+#include <GLES2/gl2extchromium.h>
+
+#include "base/logging.h"
+#include "cc/output/context_provider.h"
+#include "gpu/command_buffer/client/gles2_interface.h"
+#include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
+#include "gpu/command_buffer/common/mailbox.h"
+#include "ui/aura/env.h"
+#include "ui/compositor/compositor.h"
+#include "ui/compositor/layer.h"
+
+namespace ash {
+namespace {
+
+GLenum GLInternalFormat(gfx::BufferFormat format) {
+ const GLenum kGLInternalFormats[] = {
+ GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, // ATC
+ GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // ATCIA
+ GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // DXT1
+ GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, // DXT5
+ GL_ETC1_RGB8_OES, // ETC1
+ GL_R8_EXT, // R_8
+ GL_RGBA, // RGBA_4444
+ GL_RGBA, // RGBA_8888
+ GL_RGB, // BGRX_8888
+ GL_BGRA_EXT, // BGRA_8888
+ GL_RGB_YUV_420_CHROMIUM, // YUV_420
+ GL_INVALID_ENUM, // YUV_420_BIPLANAR
+ GL_RGB_YCBCR_422_CHROMIUM, // UYVY_422
+ };
+ static_assert(arraysize(kGLInternalFormats) ==
+ (static_cast<int>(gfx::BufferFormat::LAST) + 1),
+ "BufferFormat::LAST must be last value of kGLInternalFormats");
+
+ DCHECK(format <= gfx::BufferFormat::LAST);
+ return kGLInternalFormats[static_cast<int>(format)];
+}
+
+} // namespace
+
+SurfaceController::SurfaceController()
+ : test_shell_surface_layer_(new ui::Layer(ui::LAYER_SOLID_COLOR)),
+ weak_ptr_factory_(this) {}
+
+SurfaceController::~SurfaceController() {}
+
+void SurfaceController::CreateGraphicsBufferFromGpuMemoryBufferHandle(
+ uint32 id,
+ const gfx::GpuMemoryBufferHandle& handle,
+ const gfx::Size& size,
+ gfx::BufferFormat format,
+ const GraphicsBufferCreatedCallback& callback) {
+ if (id < 0x10000) {
+ LOG(ERROR) << "Failed to create graphics buffer. ID is less than 0x10000";
+ callback.Run(false);
+ return;
+ }
+
+ // TODO(reveman): Expose GpuMemoryBufferManager's asynchronous API.
+ scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer =
+ aura::Env::GetInstance()
+ ->context_factory()
+ ->GetGpuMemoryBufferManager()
+ ->CreateGpuMemoryBufferFromHandle(handle, size, format);
+ if (!gpu_memory_buffer) {
+ LOG(ERROR) << "Failed to create graphics buffer from handle";
+ callback.Run(false);
+ return;
+ }
+
+ if (!gpu_memory_buffers_.add(id, gpu_memory_buffer.Pass()).second) {
+ LOG(ERROR) << "Failed to create graphics buffer. ID is already present "
+ << id;
+ callback.Run(false);
+ return;
+ }
+
+ callback.Run(true);
+}
+
+void SurfaceController::DestroyGraphicsBuffer(uint32 id) {
+ if (!gpu_memory_buffers_.erase(id))
+ LOG(ERROR) << "Failed to destroy graphics buffer. ID is not present " << id;
+}
+
+void SurfaceController::AttachGraphicsBufferToTestShellSurface(
+ uint32 buffer_id,
+ const base::Closure& released_callback) {
+ gfx::GpuMemoryBuffer* gpu_memory_buffer = gpu_memory_buffers_.get(buffer_id);
+ if (!gpu_memory_buffer) {
+ LOG_IF(ERROR, buffer_id) << "Failed to attach graphics buffer to test shell"
+ << " surface. ID is not present " << buffer_id;
+ test_shell_surface_layer_->SetShowSolidColorContent();
+ test_shell_surface_layer_->SetVisible(false);
+ return;
+ }
+
+ ui::ContextFactory* context_factory =
+ aura::Env::GetInstance()->context_factory();
+ scoped_refptr<cc::ContextProvider> context_provider =
+ context_factory->SharedMainThreadContextProvider();
+ gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL();
+ gfx::BufferFormat format = gpu_memory_buffer->GetFormat();
+ gfx::Size size = gpu_memory_buffer->GetSize();
+ GLenum texture_target =
+ context_factory->GetImageTextureTarget(format, gfx::BufferUsage::SCANOUT);
+ unsigned texture_id = 0;
+ gles2->GenTextures(1, &texture_id);
+ gles2->ActiveTexture(GL_TEXTURE0);
+ gles2->BindTexture(texture_target, texture_id);
+ gles2->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ gles2->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ unsigned image_id = gles2->CreateImageCHROMIUM(
+ gpu_memory_buffer->AsClientBuffer(), size.width(), size.height(),
+ GLInternalFormat(format));
+ gles2->BindTexImage2DCHROMIUM(texture_target, image_id);
+ uint32 sync_point = gles2->InsertSyncPointCHROMIUM();
+ gpu::Mailbox mailbox;
+ gles2->GenMailboxCHROMIUM(mailbox.name);
+ gles2->ProduceTextureCHROMIUM(texture_target, mailbox.name);
+ cc::TextureMailbox texture_mailbox(mailbox, texture_target, sync_point);
+ test_shell_surface_layer_->SetTextureMailbox(
+ texture_mailbox,
+ cc::SingleReleaseCallback::Create(base::Bind(
+ &SurfaceController::TextureReleased, weak_ptr_factory_.GetWeakPtr(),
+ context_provider, texture_id, image_id, released_callback)),
+ size);
+ test_shell_surface_layer_->SetTextureFlipped(false);
+ test_shell_surface_layer_->SetBounds(gfx::Rect(size));
+ test_shell_surface_layer_->SetFillsBoundsOpaquely(true);
+ test_shell_surface_layer_->SetVisible(true);
+ test_shell_surface_layer_->SchedulePaint(gfx::Rect(size));
+}
+
+void SurfaceController::TextureReleased(
+ scoped_refptr<cc::ContextProvider> context_provider,
+ unsigned texture_id,
+ unsigned image_id,
+ const base::Closure& released_callback,
+ uint32 sync_point,
+ bool is_lost) {
+ gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL();
+ gles2->WaitSyncPointCHROMIUM(sync_point);
+ gles2->DeleteTextures(1, &texture_id);
+ gles2->DestroyImageCHROMIUM(image_id);
+ released_callback.Run();
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698