| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/tiles/image_controller.h" | 5 #include "cc/tiles/image_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/task_scheduler/post_task.h" | 8 #include "base/task_scheduler/post_task.h" |
| 9 #include "base/task_scheduler/task_traits.h" | 9 #include "base/task_scheduler/task_traits.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "cc/base/completion_event.h" | 12 #include "cc/base/completion_event.h" |
| 13 #include "cc/tiles/tile_task_manager.h" | 13 #include "cc/tiles/tile_task_manager.h" |
| 14 | 14 |
| 15 namespace cc { | 15 namespace cc { |
| 16 | 16 |
| 17 ImageController::ImageDecodeRequestId | 17 ImageController::ImageDecodeRequestId |
| 18 ImageController::s_next_image_decode_queue_id_ = 1; | 18 ImageController::s_next_image_decode_queue_id_ = 1; |
| 19 | 19 |
| 20 ImageController::ImageController( | 20 ImageController::ImageController( |
| 21 base::SequencedTaskRunner* origin_task_runner, | 21 base::SequencedTaskRunner* origin_task_runner, |
| 22 scoped_refptr<base::SequencedTaskRunner> worker_task_runner) | 22 scoped_refptr<base::SequencedTaskRunner> worker_task_runner) |
| 23 : origin_task_runner_(origin_task_runner), | 23 : worker_task_runner_(std::move(worker_task_runner)), |
| 24 worker_task_runner_(std::move(worker_task_runner)), | 24 origin_task_runner_(origin_task_runner), |
| 25 weak_ptr_factory_(this) {} | 25 weak_ptr_factory_(this) {} |
| 26 | 26 |
| 27 ImageController::~ImageController() { | 27 ImageController::~ImageController() { |
| 28 StopWorkerTasks(); | 28 StopWorkerTasks(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void ImageController::StopWorkerTasks() { | 31 void ImageController::StopWorkerTasks() { |
| 32 // We can't have worker threads without a cache_ or a worker_task_runner_, so | 32 // We can't have worker threads without a cache_ or a worker_task_runner_, so |
| 33 // terminate early. | 33 // terminate early. |
| 34 if (!cache_ || !worker_task_runner_) | 34 if (!cache_ || !worker_task_runner_) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 cache_->UnrefImage(image_pair.second); | 70 cache_->UnrefImage(image_pair.second); |
| 71 requested_locked_images_.clear(); | 71 requested_locked_images_.clear(); |
| 72 | 72 |
| 73 // Now, complete the tasks that already ran but haven't completed. These would | 73 // Now, complete the tasks that already ran but haven't completed. These would |
| 74 // be posted in the run loop, but since we invalidated the weak ptrs, we need | 74 // be posted in the run loop, but since we invalidated the weak ptrs, we need |
| 75 // to run everything manually. | 75 // to run everything manually. |
| 76 for (auto& request_to_complete : requests_needing_completion_) { | 76 for (auto& request_to_complete : requests_needing_completion_) { |
| 77 ImageDecodeRequestId id = request_to_complete.first; | 77 ImageDecodeRequestId id = request_to_complete.first; |
| 78 ImageDecodeRequest& request = request_to_complete.second; | 78 ImageDecodeRequest& request = request_to_complete.second; |
| 79 | 79 |
| 80 // The task (if one exists) would have run already, so we just need to | 80 // The task (if one exists) would have run already, we just need to make |
| 81 // complete it. | 81 // sure it was completed. Multiple requests for the same image use the same |
| 82 if (request.task) | 82 // task so it could have already been completed. |
| 83 if (request.task && !request.task->HasCompleted()) |
| 83 request.task->DidComplete(); | 84 request.task->DidComplete(); |
| 84 | 85 |
| 85 // Issue the callback, and unref the image immediately. This is so that any | 86 // Issue the callback, and unref the image immediately. This is so that any |
| 86 // code waiting on the callback can proceed, although we're breaking the | 87 // code waiting on the callback can proceed, although we're breaking the |
| 87 // promise of having this image decoded. This is unfortunate, but it seems | 88 // promise of having this image decoded. This is unfortunate, but it seems |
| 88 // like the least complexity to process an image decode controller becoming | 89 // like the least complexity to process an image decode controller becoming |
| 89 // nullptr. | 90 // nullptr. |
| 90 request.callback.Run(id); | 91 request.callback.Run(id); |
| 91 if (request.need_unref) | 92 if (request.need_unref) |
| 92 cache_->UnrefImage(request.draw_image); | 93 cache_->UnrefImage(request.draw_image); |
| 93 } | 94 } |
| 94 requests_needing_completion_.clear(); | 95 requests_needing_completion_.clear(); |
| 95 | 96 |
| 96 // Finally, complete all of the tasks that never started running. This is | 97 // Finally, complete all of the tasks that never started running. This is |
| 97 // similar to the |requests_needing_completion_|, but happens at a different | 98 // similar to the |requests_needing_completion_|, but happens at a different |
| 98 // stage in the pipeline. | 99 // stage in the pipeline. |
| 99 for (auto& request_pair : image_decode_queue_) { | 100 for (auto& request_pair : image_decode_queue_) { |
| 100 ImageDecodeRequestId id = request_pair.first; | 101 ImageDecodeRequestId id = request_pair.first; |
| 101 ImageDecodeRequest& request = request_pair.second; | 102 ImageDecodeRequest& request = request_pair.second; |
| 102 | 103 |
| 103 if (request.task) { | 104 if (request.task) { |
| 104 // This task may have run via a different request, so only cancel it if | 105 // This task may have run via a different request, so only cancel it if |
| 105 // it's "new". That is, the same task could have been referenced by | 106 // it's "new". That is, the same task could have been referenced by |
| 106 // several different image deque requests for the same image. | 107 // several different image deque requests for the same image. |
| 107 if (request.task->state().IsNew()) | 108 if (request.task->state().IsNew()) |
| 108 request.task->state().DidCancel(); | 109 request.task->state().DidCancel(); |
| 109 request.task->DidComplete(); | 110 |
| 111 if (!request.task->HasCompleted()) |
| 112 request.task->DidComplete(); |
| 110 } | 113 } |
| 111 // Run the callback and unref the image. | 114 // Run the callback and unref the image. |
| 112 request.callback.Run(id); | 115 request.callback.Run(id); |
| 113 cache_->UnrefImage(request.draw_image); | 116 cache_->UnrefImage(request.draw_image); |
| 114 } | 117 } |
| 115 image_decode_queue_.clear(); | 118 image_decode_queue_.clear(); |
| 116 } | 119 } |
| 117 | 120 |
| 118 void ImageController::SetImageDecodeCache(ImageDecodeCache* cache) { | 121 void ImageController::SetImageDecodeCache(ImageDecodeCache* cache) { |
| 119 if (!cache) { | 122 if (!cache) { |
| 120 SetPredecodeImages(std::vector<DrawImage>(), | 123 SetPredecodeImages(std::vector<DrawImage>(), |
| 121 ImageDecodeCache::TracingInfo()); | 124 ImageDecodeCache::TracingInfo()); |
| 122 StopWorkerTasks(); | 125 StopWorkerTasks(); |
| 126 } else if (cache_ && cache_ != cache) { |
| 127 StopWorkerTasks(); |
| 123 } | 128 } |
| 129 |
| 124 cache_ = cache; | 130 cache_ = cache; |
| 125 } | 131 } |
| 126 | 132 |
| 127 void ImageController::GetTasksForImagesAndRef( | 133 void ImageController::GetTasksForImagesAndRef( |
| 128 std::vector<DrawImage>* images, | 134 std::vector<DrawImage>* images, |
| 129 std::vector<scoped_refptr<TileTask>>* tasks, | 135 std::vector<scoped_refptr<TileTask>>* tasks, |
| 130 const ImageDecodeCache::TracingInfo& tracing_info) { | 136 const ImageDecodeCache::TracingInfo& tracing_info) { |
| 131 DCHECK(cache_); | 137 DCHECK(cache_); |
| 132 for (auto it = images->begin(); it != images->end();) { | 138 for (auto it = images->begin(); it != images->end();) { |
| 133 scoped_refptr<TileTask> task; | 139 scoped_refptr<TileTask> task; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 ImageController::ImageDecodeRequest::ImageDecodeRequest( | 314 ImageController::ImageDecodeRequest::ImageDecodeRequest( |
| 309 const ImageDecodeRequest& other) = default; | 315 const ImageDecodeRequest& other) = default; |
| 310 ImageController::ImageDecodeRequest::~ImageDecodeRequest() = default; | 316 ImageController::ImageDecodeRequest::~ImageDecodeRequest() = default; |
| 311 | 317 |
| 312 ImageController::ImageDecodeRequest& ImageController::ImageDecodeRequest:: | 318 ImageController::ImageDecodeRequest& ImageController::ImageDecodeRequest:: |
| 313 operator=(ImageDecodeRequest&& other) = default; | 319 operator=(ImageDecodeRequest&& other) = default; |
| 314 ImageController::ImageDecodeRequest& ImageController::ImageDecodeRequest:: | 320 ImageController::ImageDecodeRequest& ImageController::ImageDecodeRequest:: |
| 315 operator=(const ImageDecodeRequest& other) = default; | 321 operator=(const ImageDecodeRequest& other) = default; |
| 316 | 322 |
| 317 } // namespace cc | 323 } // namespace cc |
| OLD | NEW |