| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "cc/layers/heads_up_display_layer_impl.h" | 5 #include "cc/layers/heads_up_display_layer_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 paint_time_graph_(16.0, 48.0), | 74 paint_time_graph_(16.0, 48.0), |
| 75 fade_step_(0) {} | 75 fade_step_(0) {} |
| 76 | 76 |
| 77 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {} | 77 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {} |
| 78 | 78 |
| 79 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl( | 79 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl( |
| 80 LayerTreeImpl* tree_impl) { | 80 LayerTreeImpl* tree_impl) { |
| 81 return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); | 81 return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void HeadsUpDisplayLayerImpl::AcquireResource( |
| 85 ResourceProvider* resource_provider) { |
| 86 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin(); |
| 87 it != resources_.end(); |
| 88 ++it) { |
| 89 if (!resource_provider->InUseByConsumer((*it)->id())) { |
| 90 resources_.swap(it, resources_.end() - 1); |
| 91 return; |
| 92 } |
| 93 } |
| 94 |
| 95 scoped_ptr<ScopedResource> resource = |
| 96 ScopedResource::Create(resource_provider); |
| 97 resource->Allocate( |
| 98 content_bounds(), ResourceProvider::TextureHintImmutable, RGBA_8888); |
| 99 resources_.push_back(resource.Pass()); |
| 100 } |
| 101 |
| 102 class ResourceSizeIsEqualTo { |
| 103 public: |
| 104 explicit ResourceSizeIsEqualTo(const gfx::Size& size_) |
| 105 : compare_size_(size_) {} |
| 106 |
| 107 bool operator()(const ScopedResource* resource) { |
| 108 return resource->size() == compare_size_; |
| 109 } |
| 110 |
| 111 private: |
| 112 const gfx::Size compare_size_; |
| 113 }; |
| 114 |
| 115 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources( |
| 116 ResourceProvider* resource_provider) { |
| 117 ScopedPtrVector<ScopedResource>::iterator it_erase = |
| 118 resources_.partition(ResourceSizeIsEqualTo(content_bounds())); |
| 119 resources_.erase(it_erase, resources_.end()); |
| 120 } |
| 121 |
| 84 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode, | 122 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode, |
| 85 ResourceProvider* resource_provider) { | 123 ResourceProvider* resource_provider) { |
| 86 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) | 124 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE) |
| 87 return false; | 125 return false; |
| 88 | 126 |
| 89 if (!hud_resource_) | 127 ReleaseUnmatchedSizeResources(resource_provider); |
| 90 hud_resource_ = ScopedResource::Create(resource_provider); | 128 AcquireResource(resource_provider); |
| 91 | |
| 92 // TODO(danakj): The HUD could swap between two textures instead of creating a | |
| 93 // texture every frame in ubercompositor. | |
| 94 if (hud_resource_->size() != content_bounds() || | |
| 95 (hud_resource_->id() && | |
| 96 resource_provider->InUseByConsumer(hud_resource_->id()))) { | |
| 97 hud_resource_->Free(); | |
| 98 } | |
| 99 | |
| 100 if (!hud_resource_->id()) { | |
| 101 hud_resource_->Allocate( | |
| 102 content_bounds(), ResourceProvider::TextureHintImmutable, RGBA_8888); | |
| 103 } | |
| 104 | |
| 105 return LayerImpl::WillDraw(draw_mode, resource_provider); | 129 return LayerImpl::WillDraw(draw_mode, resource_provider); |
| 106 } | 130 } |
| 107 | 131 |
| 108 void HeadsUpDisplayLayerImpl::AppendQuads( | 132 void HeadsUpDisplayLayerImpl::AppendQuads( |
| 109 RenderPass* render_pass, | 133 RenderPass* render_pass, |
| 110 const OcclusionTracker<LayerImpl>& occlusion_tracker, | 134 const OcclusionTracker<LayerImpl>& occlusion_tracker, |
| 111 AppendQuadsData* append_quads_data) { | 135 AppendQuadsData* append_quads_data) { |
| 112 if (!hud_resource_->id()) | 136 if (!resources_.back()->id()) |
| 113 return; | 137 return; |
| 114 | 138 |
| 115 SharedQuadState* shared_quad_state = | 139 SharedQuadState* shared_quad_state = |
| 116 render_pass->CreateAndAppendSharedQuadState(); | 140 render_pass->CreateAndAppendSharedQuadState(); |
| 117 PopulateSharedQuadState(shared_quad_state); | 141 PopulateSharedQuadState(shared_quad_state); |
| 118 | 142 |
| 119 gfx::Rect quad_rect(content_bounds()); | 143 gfx::Rect quad_rect(content_bounds()); |
| 120 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect()); | 144 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect()); |
| 121 gfx::Rect visible_quad_rect(quad_rect); | 145 gfx::Rect visible_quad_rect(quad_rect); |
| 122 bool premultiplied_alpha = true; | 146 bool premultiplied_alpha = true; |
| 123 gfx::PointF uv_top_left(0.f, 0.f); | 147 gfx::PointF uv_top_left(0.f, 0.f); |
| 124 gfx::PointF uv_bottom_right(1.f, 1.f); | 148 gfx::PointF uv_bottom_right(1.f, 1.f); |
| 125 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f }; | 149 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f }; |
| 126 bool flipped = false; | 150 bool flipped = false; |
| 127 TextureDrawQuad* quad = | 151 TextureDrawQuad* quad = |
| 128 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>(); | 152 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>(); |
| 129 quad->SetNew(shared_quad_state, | 153 quad->SetNew(shared_quad_state, |
| 130 quad_rect, | 154 quad_rect, |
| 131 opaque_rect, | 155 opaque_rect, |
| 132 visible_quad_rect, | 156 visible_quad_rect, |
| 133 hud_resource_->id(), | 157 resources_.back()->id(), |
| 134 premultiplied_alpha, | 158 premultiplied_alpha, |
| 135 uv_top_left, | 159 uv_top_left, |
| 136 uv_bottom_right, | 160 uv_bottom_right, |
| 137 SK_ColorTRANSPARENT, | 161 SK_ColorTRANSPARENT, |
| 138 vertex_opacity, | 162 vertex_opacity, |
| 139 flipped); | 163 flipped); |
| 140 } | 164 } |
| 141 | 165 |
| 142 void HeadsUpDisplayLayerImpl::UpdateHudTexture( | 166 void HeadsUpDisplayLayerImpl::UpdateHudTexture( |
| 143 DrawMode draw_mode, | 167 DrawMode draw_mode, |
| 144 ResourceProvider* resource_provider) { | 168 ResourceProvider* resource_provider) { |
| 145 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !hud_resource_->id()) | 169 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id()) |
| 146 return; | 170 return; |
| 147 | 171 |
| 148 SkISize canvas_size; | 172 SkISize canvas_size; |
| 149 if (hud_canvas_) | 173 if (hud_canvas_) |
| 150 canvas_size = hud_canvas_->getDeviceSize(); | 174 canvas_size = hud_canvas_->getDeviceSize(); |
| 151 else | 175 else |
| 152 canvas_size.set(0, 0); | 176 canvas_size.set(0, 0); |
| 153 | 177 |
| 154 if (canvas_size.width() != content_bounds().width() || | 178 if (canvas_size.width() != content_bounds().width() || |
| 155 canvas_size.height() != content_bounds().height() || !hud_canvas_) { | 179 canvas_size.height() != content_bounds().height() || !hud_canvas_) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 172 hud_canvas_->restore(); | 196 hud_canvas_->restore(); |
| 173 } | 197 } |
| 174 | 198 |
| 175 TRACE_EVENT0("cc", "UploadHudTexture"); | 199 TRACE_EVENT0("cc", "UploadHudTexture"); |
| 176 SkImageInfo info; | 200 SkImageInfo info; |
| 177 size_t row_bytes = 0; | 201 size_t row_bytes = 0; |
| 178 const void* pixels = hud_canvas_->peekPixels(&info, &row_bytes); | 202 const void* pixels = hud_canvas_->peekPixels(&info, &row_bytes); |
| 179 DCHECK(pixels); | 203 DCHECK(pixels); |
| 180 gfx::Rect content_rect(content_bounds()); | 204 gfx::Rect content_rect(content_bounds()); |
| 181 DCHECK(info.colorType() == kN32_SkColorType); | 205 DCHECK(info.colorType() == kN32_SkColorType); |
| 182 resource_provider->SetPixels(hud_resource_->id(), | 206 resource_provider->SetPixels(resources_.back()->id(), |
| 183 static_cast<const uint8_t*>(pixels), | 207 static_cast<const uint8_t*>(pixels), |
| 184 content_rect, | 208 content_rect, |
| 185 content_rect, | 209 content_rect, |
| 186 gfx::Vector2d()); | 210 gfx::Vector2d()); |
| 187 } | 211 } |
| 188 | 212 |
| 189 void HeadsUpDisplayLayerImpl::ReleaseResources() { hud_resource_.reset(); } | 213 void HeadsUpDisplayLayerImpl::ReleaseResources() { |
| 214 resources_.clear(); |
| 215 } |
| 190 | 216 |
| 191 void HeadsUpDisplayLayerImpl::UpdateHudContents() { | 217 void HeadsUpDisplayLayerImpl::UpdateHudContents() { |
| 192 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state(); | 218 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state(); |
| 193 | 219 |
| 194 // Don't update numbers every frame so text is readable. | 220 // Don't update numbers every frame so text is readable. |
| 195 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time; | 221 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time; |
| 196 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) { | 222 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) { |
| 197 time_of_last_graph_update_ = now; | 223 time_of_last_graph_update_ = now; |
| 198 | 224 |
| 199 if (debug_state.show_fps_counter) { | 225 if (debug_state.show_fps_counter) { |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 return "cc::HeadsUpDisplayLayerImpl"; | 788 return "cc::HeadsUpDisplayLayerImpl"; |
| 763 } | 789 } |
| 764 | 790 |
| 765 void HeadsUpDisplayLayerImpl::AsValueInto( | 791 void HeadsUpDisplayLayerImpl::AsValueInto( |
| 766 base::debug::TracedValue* dict) const { | 792 base::debug::TracedValue* dict) const { |
| 767 LayerImpl::AsValueInto(dict); | 793 LayerImpl::AsValueInto(dict); |
| 768 dict->SetString("layer_name", "Heads Up Display Layer"); | 794 dict->SetString("layer_name", "Heads Up Display Layer"); |
| 769 } | 795 } |
| 770 | 796 |
| 771 } // namespace cc | 797 } // namespace cc |
| OLD | NEW |