| 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 "services/ui/demo/bitmap_uploader.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "cc/ipc/compositor_frame.mojom.h" | |
| 14 #include "cc/quads/render_pass.h" | |
| 15 #include "cc/quads/solid_color_draw_quad.h" | |
| 16 #include "cc/quads/texture_draw_quad.h" | |
| 17 #include "services/ui/public/cpp/context_provider.h" | |
| 18 #include "services/ui/public/cpp/gles2_context.h" | |
| 19 #include "services/ui/public/cpp/gpu/gpu_service.h" | |
| 20 #include "services/ui/public/cpp/window.h" | |
| 21 | |
| 22 namespace ui { | |
| 23 namespace { | |
| 24 | |
| 25 const uint32_t g_transparent_color = 0x00000000; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 const char kBitmapUploaderForAcceleratedWidget[] = | |
| 30 "__BITMAP_UPLOADER_ACCELERATED_WIDGET__"; | |
| 31 | |
| 32 BitmapUploader::BitmapUploader(Window* window) | |
| 33 : window_(window), | |
| 34 color_(g_transparent_color), | |
| 35 width_(0), | |
| 36 height_(0), | |
| 37 format_(BGRA), | |
| 38 next_resource_id_(1u), | |
| 39 weak_factory_(this) {} | |
| 40 | |
| 41 void BitmapUploader::Init(ui::GpuService* gpu_service) { | |
| 42 gpu_service->EstablishGpuChannel(base::Bind( | |
| 43 &BitmapUploader::OnGpuChannelEstablished, weak_factory_.GetWeakPtr(), | |
| 44 gpu_service->gpu_memory_buffer_manager())); | |
| 45 } | |
| 46 | |
| 47 BitmapUploader::~BitmapUploader() { | |
| 48 compositor_frame_sink_->DetachFromClient(); | |
| 49 } | |
| 50 | |
| 51 // Sets the color which is RGBA. | |
| 52 void BitmapUploader::SetColor(uint32_t color) { | |
| 53 if (color_ == color) | |
| 54 return; | |
| 55 color_ = color; | |
| 56 if (compositor_frame_sink_) | |
| 57 Upload(); | |
| 58 } | |
| 59 | |
| 60 // Sets a bitmap. | |
| 61 void BitmapUploader::SetBitmap(int width, | |
| 62 int height, | |
| 63 std::unique_ptr<std::vector<unsigned char>> data, | |
| 64 Format format) { | |
| 65 width_ = width; | |
| 66 height_ = height; | |
| 67 bitmap_ = std::move(data); | |
| 68 format_ = format; | |
| 69 if (compositor_frame_sink_) | |
| 70 Upload(); | |
| 71 } | |
| 72 | |
| 73 void BitmapUploader::Upload() { | |
| 74 if (!compositor_frame_sink_ || !compositor_frame_sink_->context_provider()) | |
| 75 return; | |
| 76 | |
| 77 const gfx::Rect bounds(window_->bounds().size()); | |
| 78 | |
| 79 cc::CompositorFrame frame; | |
| 80 // TODO(rjkroege): Support device scale factors other than 1. | |
| 81 frame.metadata.device_scale_factor = 1.0f; | |
| 82 frame.resource_list.resize(0u); | |
| 83 | |
| 84 const cc::RenderPassId render_pass_id(1, 1); | |
| 85 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | |
| 86 pass->SetAll(render_pass_id, bounds, bounds, gfx::Transform(), | |
| 87 true /* has_transparent_background */); | |
| 88 | |
| 89 // The SharedQuadState is owned by the SharedQuadStateList | |
| 90 // shared_quad_state_list. | |
| 91 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | |
| 92 sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, | |
| 93 false /* is_clipped */, 1.f /* opacity */, SkBlendMode::kSrc, | |
| 94 0 /* sorting_context_id */); | |
| 95 | |
| 96 if (bitmap_.get()) { | |
| 97 gpu::gles2::GLES2Interface* gl = | |
| 98 compositor_frame_sink_->context_provider()->ContextGL(); | |
| 99 gfx::Size bitmap_size(width_, height_); | |
| 100 GLuint texture_id = BindTextureForSize(bitmap_size); | |
| 101 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap_size.width(), | |
| 102 bitmap_size.height(), TextureFormat(), GL_UNSIGNED_BYTE, | |
| 103 &((*bitmap_)[0])); | |
| 104 | |
| 105 gpu::Mailbox mailbox; | |
| 106 gl->GenMailboxCHROMIUM(mailbox.name); | |
| 107 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | |
| 108 | |
| 109 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
| 110 gl->ShallowFlushCHROMIUM(); | |
| 111 | |
| 112 gpu::SyncToken sync_token; | |
| 113 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
| 114 | |
| 115 cc::TransferableResource resource; | |
| 116 resource.id = next_resource_id_++; | |
| 117 resource_to_texture_id_map_[resource.id] = texture_id; | |
| 118 resource.format = cc::ResourceFormat::RGBA_8888; | |
| 119 resource.filter = GL_LINEAR; | |
| 120 resource.size = bitmap_size; | |
| 121 resource.mailbox_holder = | |
| 122 gpu::MailboxHolder(mailbox, sync_token, GL_TEXTURE_2D); | |
| 123 resource.read_lock_fences_enabled = false; | |
| 124 resource.is_software = false; | |
| 125 resource.is_overlay_candidate = false; | |
| 126 frame.resource_list.push_back(std::move(resource)); | |
| 127 | |
| 128 cc::TextureDrawQuad* quad = | |
| 129 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); | |
| 130 | |
| 131 gfx::Size rect_size; | |
| 132 if (width_ <= bounds.width() && height_ <= bounds.height()) { | |
| 133 rect_size.SetSize(width_, height_); | |
| 134 } else { | |
| 135 // The source bitmap is larger than the viewport. Resize it while | |
| 136 // maintaining the aspect ratio. | |
| 137 float width_ratio = static_cast<float>(width_) / bounds.width(); | |
| 138 float height_ratio = static_cast<float>(height_) / bounds.height(); | |
| 139 if (width_ratio > height_ratio) { | |
| 140 rect_size.SetSize(bounds.width(), height_ / width_ratio); | |
| 141 } else { | |
| 142 rect_size.SetSize(width_ / height_ratio, bounds.height()); | |
| 143 } | |
| 144 } | |
| 145 gfx::Rect rect(rect_size); | |
| 146 const bool needs_blending = true; | |
| 147 const bool premultiplied_alpha = true; | |
| 148 const gfx::PointF uv_top_left(0.f, 0.f); | |
| 149 const gfx::PointF uv_bottom_right(1.f, 1.f); | |
| 150 float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f}; | |
| 151 const bool y_flipped = false; | |
| 152 const bool nearest_neighbor = false; | |
| 153 quad->SetAll(sqs, rect, rect, rect, needs_blending, resource.id, | |
| 154 gfx::Size(), premultiplied_alpha, uv_top_left, uv_bottom_right, | |
| 155 g_transparent_color, vertex_opacity, y_flipped, | |
| 156 nearest_neighbor, false); | |
| 157 } | |
| 158 | |
| 159 if (color_ != g_transparent_color) { | |
| 160 cc::SolidColorDrawQuad* quad = | |
| 161 pass->CreateAndAppendDrawQuad<cc::SolidColorDrawQuad>(); | |
| 162 const bool force_antialiasing_off = false; | |
| 163 const gfx::Rect opaque_rect(0, 0, 0, 0); | |
| 164 const bool needs_blending = true; | |
| 165 quad->SetAll(sqs, bounds, opaque_rect, bounds, needs_blending, color_, | |
| 166 force_antialiasing_off); | |
| 167 } | |
| 168 | |
| 169 frame.render_pass_list.push_back(std::move(pass)); | |
| 170 | |
| 171 // TODO(rjkroege, fsamuel): We should throttle frames. | |
| 172 compositor_frame_sink_->SubmitCompositorFrame(std::move(frame)); | |
| 173 } | |
| 174 | |
| 175 void BitmapUploader::OnGpuChannelEstablished( | |
| 176 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 177 scoped_refptr<gpu::GpuChannelHost> gpu_channel) { | |
| 178 compositor_frame_sink_ = window_->RequestCompositorFrameSink( | |
| 179 mojom::CompositorFrameSinkType::DEFAULT, | |
| 180 new ContextProvider(std::move(gpu_channel)), gpu_memory_buffer_manager); | |
| 181 compositor_frame_sink_->BindToClient(this); | |
| 182 } | |
| 183 | |
| 184 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size& size) { | |
| 185 gpu::gles2::GLES2Interface* gl = | |
| 186 compositor_frame_sink_->context_provider()->ContextGL(); | |
| 187 // TODO(jamesr): Recycle textures. | |
| 188 GLuint texture = 0u; | |
| 189 gl->GenTextures(1, &texture); | |
| 190 gl->BindTexture(GL_TEXTURE_2D, texture); | |
| 191 gl->TexImage2D(GL_TEXTURE_2D, 0, TextureFormat(), size.width(), size.height(), | |
| 192 0, TextureFormat(), GL_UNSIGNED_BYTE, 0); | |
| 193 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 194 return texture; | |
| 195 } | |
| 196 | |
| 197 void BitmapUploader::SetBeginFrameSource(cc::BeginFrameSource* source) {} | |
| 198 | |
| 199 void BitmapUploader::ReclaimResources( | |
| 200 const cc::ReturnedResourceArray& resources) { | |
| 201 gpu::gles2::GLES2Interface* gl = | |
| 202 compositor_frame_sink_->context_provider()->ContextGL(); | |
| 203 // TODO(jamesr): Recycle. | |
| 204 for (size_t i = 0; i < resources.size(); ++i) { | |
| 205 cc::ReturnedResource resource = std::move(resources[i]); | |
| 206 DCHECK_EQ(1, resource.count); | |
| 207 gl->WaitSyncTokenCHROMIUM(resource.sync_token.GetConstData()); | |
| 208 uint32_t texture_id = resource_to_texture_id_map_[resource.id]; | |
| 209 DCHECK_NE(0u, texture_id); | |
| 210 resource_to_texture_id_map_.erase(resource.id); | |
| 211 gl->DeleteTextures(1, &texture_id); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void BitmapUploader::SetTreeActivationCallback(const base::Closure& callback) { | |
| 216 // TODO(fsamuel): Implement this. | |
| 217 } | |
| 218 | |
| 219 void BitmapUploader::DidReceiveCompositorFrameAck() { | |
| 220 // TODO(fsamuel): Implement this. | |
| 221 } | |
| 222 | |
| 223 void BitmapUploader::DidLoseCompositorFrameSink() { | |
| 224 // TODO(fsamuel): Implement this. | |
| 225 } | |
| 226 | |
| 227 void BitmapUploader::OnDraw(const gfx::Transform& transform, | |
| 228 const gfx::Rect& viewport, | |
| 229 bool resourceless_software_draw) { | |
| 230 // TODO(fsamuel): Implement this. | |
| 231 } | |
| 232 | |
| 233 void BitmapUploader::SetMemoryPolicy(const cc::ManagedMemoryPolicy& policy) { | |
| 234 // TODO(fsamuel): Implement this. | |
| 235 } | |
| 236 | |
| 237 void BitmapUploader::SetExternalTilePriorityConstraints( | |
| 238 const gfx::Rect& viewport_rect, | |
| 239 const gfx::Transform& transform) { | |
| 240 // TODO(fsamuel): Implement this. | |
| 241 } | |
| 242 | |
| 243 } // namespace ui | |
| OLD | NEW |