| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "services/ui/demo/bitmap_uploader.h" | 5 #include "services/ui/demo/bitmap_uploader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "cc/ipc/compositor_frame.mojom.h" | 13 #include "cc/ipc/compositor_frame.mojom.h" |
| 14 #include "cc/quads/render_pass.h" | 14 #include "cc/quads/render_pass.h" |
| 15 #include "cc/quads/solid_color_draw_quad.h" | 15 #include "cc/quads/solid_color_draw_quad.h" |
| 16 #include "cc/quads/texture_draw_quad.h" | 16 #include "cc/quads/texture_draw_quad.h" |
| 17 #include "services/ui/public/cpp/context_provider.h" |
| 17 #include "services/ui/public/cpp/gles2_context.h" | 18 #include "services/ui/public/cpp/gles2_context.h" |
| 18 #include "services/ui/public/cpp/gpu_service.h" | 19 #include "services/ui/public/cpp/gpu_service.h" |
| 19 #include "services/ui/public/cpp/window.h" | 20 #include "services/ui/public/cpp/window.h" |
| 20 #include "services/ui/public/cpp/window_surface.h" | 21 #include "services/ui/public/cpp/window_surface.h" |
| 21 | 22 |
| 22 namespace ui { | 23 namespace ui { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const uint32_t g_transparent_color = 0x00000000; | 26 const uint32_t g_transparent_color = 0x00000000; |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 const char kBitmapUploaderForAcceleratedWidget[] = | 30 const char kBitmapUploaderForAcceleratedWidget[] = |
| 30 "__BITMAP_UPLOADER_ACCELERATED_WIDGET__"; | 31 "__BITMAP_UPLOADER_ACCELERATED_WIDGET__"; |
| 31 | 32 |
| 32 BitmapUploader::BitmapUploader(Window* window) | 33 BitmapUploader::BitmapUploader(Window* window) |
| 33 : window_(window), | 34 : window_(window), |
| 34 color_(g_transparent_color), | 35 color_(g_transparent_color), |
| 35 width_(0), | 36 width_(0), |
| 36 height_(0), | 37 height_(0), |
| 37 format_(BGRA), | 38 format_(BGRA), |
| 38 next_resource_id_(1u) { | 39 next_resource_id_(1u) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 void BitmapUploader::Init(ui::GpuService* gpu_service) { | 42 void BitmapUploader::Init(ui::GpuService* gpu_service) { |
| 42 surface_ = window_->RequestSurface(mojom::SurfaceType::DEFAULT); | 43 compositor_frame_sink_ = window_->RequestCompositorFrameSink( |
| 43 surface_->BindToThread(); | 44 mojom::SurfaceType::DEFAULT, |
| 44 surface_->set_client(this); | 45 new ContextProvider(gpu_service->EstablishGpuChannelSync())); |
| 45 | 46 compositor_frame_sink_->BindToClient(this); |
| 46 gles2_context_ = GLES2Context::CreateOffscreenContext( | |
| 47 gpu_service->EstablishGpuChannelSync(), | |
| 48 base::ThreadTaskRunnerHandle::Get()); | |
| 49 } | 47 } |
| 50 | 48 |
| 51 BitmapUploader::~BitmapUploader() {} | 49 BitmapUploader::~BitmapUploader() { |
| 50 compositor_frame_sink_->DetachFromClient(); |
| 51 } |
| 52 | 52 |
| 53 // Sets the color which is RGBA. | 53 // Sets the color which is RGBA. |
| 54 void BitmapUploader::SetColor(uint32_t color) { | 54 void BitmapUploader::SetColor(uint32_t color) { |
| 55 if (color_ == color) | 55 if (color_ == color) |
| 56 return; | 56 return; |
| 57 color_ = color; | 57 color_ = color; |
| 58 if (surface_) | 58 if (compositor_frame_sink_) |
| 59 Upload(); | 59 Upload(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Sets a bitmap. | 62 // Sets a bitmap. |
| 63 void BitmapUploader::SetBitmap(int width, | 63 void BitmapUploader::SetBitmap(int width, |
| 64 int height, | 64 int height, |
| 65 std::unique_ptr<std::vector<unsigned char>> data, | 65 std::unique_ptr<std::vector<unsigned char>> data, |
| 66 Format format) { | 66 Format format) { |
| 67 width_ = width; | 67 width_ = width; |
| 68 height_ = height; | 68 height_ = height; |
| 69 bitmap_ = std::move(data); | 69 bitmap_ = std::move(data); |
| 70 format_ = format; | 70 format_ = format; |
| 71 if (surface_) | 71 if (compositor_frame_sink_) |
| 72 Upload(); | 72 Upload(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void BitmapUploader::Upload() { | 75 void BitmapUploader::Upload() { |
| 76 if (!gles2_context_) | 76 if (!compositor_frame_sink_ || !compositor_frame_sink_->context_provider()) |
| 77 return; | 77 return; |
| 78 | 78 |
| 79 const gfx::Rect bounds(window_->bounds().size()); | 79 const gfx::Rect bounds(window_->bounds().size()); |
| 80 | 80 |
| 81 cc::CompositorFrame frame; | 81 cc::CompositorFrame frame; |
| 82 // TODO(rjkroege): Support device scale factors other than 1. | 82 // TODO(rjkroege): Support device scale factors other than 1. |
| 83 frame.metadata.device_scale_factor = 1.0f; | 83 frame.metadata.device_scale_factor = 1.0f; |
| 84 frame.delegated_frame_data.reset(new cc::DelegatedFrameData()); | 84 frame.delegated_frame_data.reset(new cc::DelegatedFrameData()); |
| 85 frame.delegated_frame_data->resource_list.resize(0u); | 85 frame.delegated_frame_data->resource_list.resize(0u); |
| 86 | 86 |
| 87 const cc::RenderPassId render_pass_id(1, 1); | 87 const cc::RenderPassId render_pass_id(1, 1); |
| 88 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | 88 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
| 89 pass->SetAll(render_pass_id, bounds, bounds, gfx::Transform(), | 89 pass->SetAll(render_pass_id, bounds, bounds, gfx::Transform(), |
| 90 true /* has_transparent_background */); | 90 true /* has_transparent_background */); |
| 91 | 91 |
| 92 // The SharedQuadState is owned by the SharedQuadStateList | 92 // The SharedQuadState is owned by the SharedQuadStateList |
| 93 // shared_quad_state_list. | 93 // shared_quad_state_list. |
| 94 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | 94 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
| 95 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, | 95 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, |
| 96 false /* is_clipped */, 1.f /* opacity */, SkXfermode::kSrc_Mode, | 96 false /* is_clipped */, 1.f /* opacity */, SkXfermode::kSrc_Mode, |
| 97 0 /* sorting_context_id */); | 97 0 /* sorting_context_id */); |
| 98 | 98 |
| 99 if (bitmap_.get()) { | 99 if (bitmap_.get()) { |
| 100 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); | 100 gpu::gles2::GLES2Interface* gl = |
| 101 compositor_frame_sink_->context_provider()->ContextGL(); |
| 101 gfx::Size bitmap_size(width_, height_); | 102 gfx::Size bitmap_size(width_, height_); |
| 102 GLuint texture_id = BindTextureForSize(bitmap_size); | 103 GLuint texture_id = BindTextureForSize(bitmap_size); |
| 103 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap_size.width(), | 104 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap_size.width(), |
| 104 bitmap_size.height(), TextureFormat(), GL_UNSIGNED_BYTE, | 105 bitmap_size.height(), TextureFormat(), GL_UNSIGNED_BYTE, |
| 105 &((*bitmap_)[0])); | 106 &((*bitmap_)[0])); |
| 106 | 107 |
| 107 gpu::Mailbox mailbox; | 108 gpu::Mailbox mailbox; |
| 108 gl->GenMailboxCHROMIUM(mailbox.name); | 109 gl->GenMailboxCHROMIUM(mailbox.name); |
| 109 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | 110 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| 110 | 111 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 const bool force_antialiasing_off = false; | 165 const bool force_antialiasing_off = false; |
| 165 const gfx::Rect opaque_rect(0, 0, 0, 0); | 166 const gfx::Rect opaque_rect(0, 0, 0, 0); |
| 166 const bool needs_blending = true; | 167 const bool needs_blending = true; |
| 167 quad->SetAll(sqs, bounds, opaque_rect, bounds, needs_blending, color_, | 168 quad->SetAll(sqs, bounds, opaque_rect, bounds, needs_blending, color_, |
| 168 force_antialiasing_off); | 169 force_antialiasing_off); |
| 169 } | 170 } |
| 170 | 171 |
| 171 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); | 172 frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); |
| 172 | 173 |
| 173 // TODO(rjkroege, fsamuel): We should throttle frames. | 174 // TODO(rjkroege, fsamuel): We should throttle frames. |
| 174 surface_->SubmitCompositorFrame(std::move(frame), base::Closure()); | 175 compositor_frame_sink_->SubmitCompositorFrame(std::move(frame)); |
| 175 } | 176 } |
| 176 | 177 |
| 177 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size& size) { | 178 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size& size) { |
| 178 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); | 179 gpu::gles2::GLES2Interface* gl = |
| 180 compositor_frame_sink_->context_provider()->ContextGL(); |
| 179 // TODO(jamesr): Recycle textures. | 181 // TODO(jamesr): Recycle textures. |
| 180 GLuint texture = 0u; | 182 GLuint texture = 0u; |
| 181 gl->GenTextures(1, &texture); | 183 gl->GenTextures(1, &texture); |
| 182 gl->BindTexture(GL_TEXTURE_2D, texture); | 184 gl->BindTexture(GL_TEXTURE_2D, texture); |
| 183 gl->TexImage2D(GL_TEXTURE_2D, 0, TextureFormat(), size.width(), size.height(), | 185 gl->TexImage2D(GL_TEXTURE_2D, 0, TextureFormat(), size.width(), size.height(), |
| 184 0, TextureFormat(), GL_UNSIGNED_BYTE, 0); | 186 0, TextureFormat(), GL_UNSIGNED_BYTE, 0); |
| 185 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 187 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 186 return texture; | 188 return texture; |
| 187 } | 189 } |
| 188 | 190 |
| 189 void BitmapUploader::OnResourcesReturned( | 191 void BitmapUploader::SetBeginFrameSource(cc::BeginFrameSource* source) {} |
| 190 WindowSurface* surface, | 192 |
| 193 void BitmapUploader::ReclaimResources( |
| 191 const cc::ReturnedResourceArray& resources) { | 194 const cc::ReturnedResourceArray& resources) { |
| 192 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); | 195 gpu::gles2::GLES2Interface* gl = |
| 196 compositor_frame_sink_->context_provider()->ContextGL(); |
| 193 // TODO(jamesr): Recycle. | 197 // TODO(jamesr): Recycle. |
| 194 for (size_t i = 0; i < resources.size(); ++i) { | 198 for (size_t i = 0; i < resources.size(); ++i) { |
| 195 cc::ReturnedResource resource = std::move(resources[i]); | 199 cc::ReturnedResource resource = std::move(resources[i]); |
| 196 DCHECK_EQ(1, resource.count); | 200 DCHECK_EQ(1, resource.count); |
| 197 gl->WaitSyncTokenCHROMIUM(resource.sync_token.GetConstData()); | 201 gl->WaitSyncTokenCHROMIUM(resource.sync_token.GetConstData()); |
| 198 uint32_t texture_id = resource_to_texture_id_map_[resource.id]; | 202 uint32_t texture_id = resource_to_texture_id_map_[resource.id]; |
| 199 DCHECK_NE(0u, texture_id); | 203 DCHECK_NE(0u, texture_id); |
| 200 resource_to_texture_id_map_.erase(resource.id); | 204 resource_to_texture_id_map_.erase(resource.id); |
| 201 gl->DeleteTextures(1, &texture_id); | 205 gl->DeleteTextures(1, &texture_id); |
| 202 } | 206 } |
| 203 } | 207 } |
| 204 | 208 |
| 209 void BitmapUploader::SetTreeActivationCallback(const base::Closure& callback) { |
| 210 // TODO(fsamuel): Implement this. |
| 211 } |
| 212 |
| 213 void BitmapUploader::DidReceiveCompositorFrameAck() { |
| 214 // TODO(fsamuel): Implement this. |
| 215 } |
| 216 |
| 217 void BitmapUploader::DidLoseCompositorFrameSink() { |
| 218 // TODO(fsamuel): Implement this. |
| 219 } |
| 220 |
| 221 void BitmapUploader::OnDraw(const gfx::Transform& transform, |
| 222 const gfx::Rect& viewport, |
| 223 bool resourceless_software_draw) { |
| 224 // TODO(fsamuel): Implement this. |
| 225 } |
| 226 |
| 227 void BitmapUploader::SetMemoryPolicy(const cc::ManagedMemoryPolicy& policy) { |
| 228 // TODO(fsamuel): Implement this. |
| 229 } |
| 230 |
| 231 void BitmapUploader::SetExternalTilePriorityConstraints( |
| 232 const gfx::Rect& viewport_rect, |
| 233 const gfx::Transform& transform) { |
| 234 // TODO(fsamuel): Implement this. |
| 235 } |
| 236 |
| 205 } // namespace ui | 237 } // namespace ui |
| OLD | NEW |