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

Side by Side 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 unified diff | Download patch
OLDNEW
(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/surface_controller.h"
6
7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2ext.h>
9 #include <GLES2/gl2extchromium.h>
10
11 #include "base/logging.h"
12 #include "cc/output/context_provider.h"
13 #include "gpu/command_buffer/client/gles2_interface.h"
14 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
15 #include "gpu/command_buffer/common/mailbox.h"
16 #include "ui/aura/env.h"
17 #include "ui/compositor/compositor.h"
18 #include "ui/compositor/layer.h"
19
20 namespace ash {
21 namespace {
22
23 GLenum GLInternalFormat(gfx::BufferFormat format) {
24 const GLenum kGLInternalFormats[] = {
25 GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, // ATC
26 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // ATCIA
27 GL_COMPRESSED_RGB_S3TC_DXT1_EXT, // DXT1
28 GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, // DXT5
29 GL_ETC1_RGB8_OES, // ETC1
30 GL_R8_EXT, // R_8
31 GL_RGBA, // RGBA_4444
32 GL_RGBA, // RGBA_8888
33 GL_RGB, // BGRX_8888
34 GL_BGRA_EXT, // BGRA_8888
35 GL_RGB_YUV_420_CHROMIUM, // YUV_420
36 GL_INVALID_ENUM, // YUV_420_BIPLANAR
37 GL_RGB_YCBCR_422_CHROMIUM, // UYVY_422
38 };
39 static_assert(arraysize(kGLInternalFormats) ==
40 (static_cast<int>(gfx::BufferFormat::LAST) + 1),
41 "BufferFormat::LAST must be last value of kGLInternalFormats");
42
43 DCHECK(format <= gfx::BufferFormat::LAST);
44 return kGLInternalFormats[static_cast<int>(format)];
45 }
46
47 } // namespace
48
49 SurfaceController::SurfaceController()
50 : test_shell_surface_layer_(new ui::Layer(ui::LAYER_SOLID_COLOR)),
51 weak_ptr_factory_(this) {}
52
53 SurfaceController::~SurfaceController() {}
54
55 void SurfaceController::CreateGraphicsBufferFromGpuMemoryBufferHandle(
56 uint32 id,
57 const gfx::GpuMemoryBufferHandle& handle,
58 const gfx::Size& size,
59 gfx::BufferFormat format,
60 const GraphicsBufferCreatedCallback& callback) {
61 if (id < 0x10000) {
62 LOG(ERROR) << "Failed to create graphics buffer. ID is less than 0x10000";
63 callback.Run(false);
64 return;
65 }
66
67 // TODO(reveman): Expose GpuMemoryBufferManager's asynchronous API.
68 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer =
69 aura::Env::GetInstance()
70 ->context_factory()
71 ->GetGpuMemoryBufferManager()
72 ->CreateGpuMemoryBufferFromHandle(handle, size, format);
73 if (!gpu_memory_buffer) {
74 LOG(ERROR) << "Failed to create graphics buffer from handle";
75 callback.Run(false);
76 return;
77 }
78
79 if (!gpu_memory_buffers_.add(id, gpu_memory_buffer.Pass()).second) {
80 LOG(ERROR) << "Failed to create graphics buffer. ID is already present "
81 << id;
82 callback.Run(false);
83 return;
84 }
85
86 callback.Run(true);
87 }
88
89 void SurfaceController::DestroyGraphicsBuffer(uint32 id) {
90 if (!gpu_memory_buffers_.erase(id))
91 LOG(ERROR) << "Failed to destroy graphics buffer. ID is not present " << id;
92 }
93
94 void SurfaceController::AttachGraphicsBufferToTestShellSurface(
95 uint32 buffer_id,
96 const base::Closure& released_callback) {
97 gfx::GpuMemoryBuffer* gpu_memory_buffer = gpu_memory_buffers_.get(buffer_id);
98 if (!gpu_memory_buffer) {
99 LOG_IF(ERROR, buffer_id) << "Failed to attach graphics buffer to test shell"
100 << " surface. ID is not present " << buffer_id;
101 test_shell_surface_layer_->SetShowSolidColorContent();
102 test_shell_surface_layer_->SetVisible(false);
103 return;
104 }
105
106 ui::ContextFactory* context_factory =
107 aura::Env::GetInstance()->context_factory();
108 scoped_refptr<cc::ContextProvider> context_provider =
109 context_factory->SharedMainThreadContextProvider();
110 gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL();
111 gfx::BufferFormat format = gpu_memory_buffer->GetFormat();
112 gfx::Size size = gpu_memory_buffer->GetSize();
113 GLenum texture_target =
114 context_factory->GetImageTextureTarget(format, gfx::BufferUsage::SCANOUT);
115 unsigned texture_id = 0;
116 gles2->GenTextures(1, &texture_id);
117 gles2->ActiveTexture(GL_TEXTURE0);
118 gles2->BindTexture(texture_target, texture_id);
119 gles2->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
120 gles2->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
121 gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
122 gles2->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
123 unsigned image_id = gles2->CreateImageCHROMIUM(
124 gpu_memory_buffer->AsClientBuffer(), size.width(), size.height(),
125 GLInternalFormat(format));
126 gles2->BindTexImage2DCHROMIUM(texture_target, image_id);
127 uint32 sync_point = gles2->InsertSyncPointCHROMIUM();
128 gpu::Mailbox mailbox;
129 gles2->GenMailboxCHROMIUM(mailbox.name);
130 gles2->ProduceTextureCHROMIUM(texture_target, mailbox.name);
131 cc::TextureMailbox texture_mailbox(mailbox, texture_target, sync_point);
132 test_shell_surface_layer_->SetTextureMailbox(
133 texture_mailbox,
134 cc::SingleReleaseCallback::Create(base::Bind(
135 &SurfaceController::TextureReleased, weak_ptr_factory_.GetWeakPtr(),
136 context_provider, texture_id, image_id, released_callback)),
137 size);
138 test_shell_surface_layer_->SetTextureFlipped(false);
139 test_shell_surface_layer_->SetBounds(gfx::Rect(size));
140 test_shell_surface_layer_->SetFillsBoundsOpaquely(true);
141 test_shell_surface_layer_->SetVisible(true);
142 test_shell_surface_layer_->SchedulePaint(gfx::Rect(size));
143 }
144
145 void SurfaceController::TextureReleased(
146 scoped_refptr<cc::ContextProvider> context_provider,
147 unsigned texture_id,
148 unsigned image_id,
149 const base::Closure& released_callback,
150 uint32 sync_point,
151 bool is_lost) {
152 gpu::gles2::GLES2Interface* gles2 = context_provider->ContextGL();
153 gles2->WaitSyncPointCHROMIUM(sync_point);
154 gles2->DeleteTextures(1, &texture_id);
155 gles2->DestroyImageCHROMIUM(image_id);
156 released_callback.Run();
157 }
158
159 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698