| 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/gpu_image_decode_controller.h" | 5 #include "cc/tiles/gpu_image_decode_controller.h" |
| 6 | 6 |
| 7 #include "base/memory/discardable_memory_allocator.h" | 7 #include "base/memory/discardable_memory_allocator.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 params.fMatrix = draw_image.matrix(); | 50 params.fMatrix = draw_image.matrix(); |
| 51 params.fQuality = draw_image.filter_quality(); | 51 params.fQuality = draw_image.filter_quality(); |
| 52 | 52 |
| 53 return params; | 53 return params; |
| 54 } | 54 } |
| 55 | 55 |
| 56 } // namespace | 56 } // namespace |
| 57 | 57 |
| 58 // Task which decodes an image and stores the result in discardable memory. | 58 // Task which decodes an image and stores the result in discardable memory. |
| 59 // This task does not use GPU resources and can be run on any thread. | 59 // This task does not use GPU resources and can be run on any thread. |
| 60 class ImageDecodeTaskImpl : public ImageDecodeTask { | 60 class ImageDecodeTaskImpl : public TileTask { |
| 61 public: | 61 public: |
| 62 ImageDecodeTaskImpl(GpuImageDecodeController* controller, | 62 ImageDecodeTaskImpl(GpuImageDecodeController* controller, |
| 63 const DrawImage& draw_image, | 63 const DrawImage& draw_image, |
| 64 uint64_t source_prepare_tiles_id) | 64 uint64_t source_prepare_tiles_id) |
| 65 : controller_(controller), | 65 : TileTask(true), |
| 66 controller_(controller), |
| 66 image_(draw_image), | 67 image_(draw_image), |
| 67 image_ref_(skia::SharePtr(draw_image.image())), | 68 image_ref_(skia::SharePtr(draw_image.image())), |
| 68 source_prepare_tiles_id_(source_prepare_tiles_id) { | 69 source_prepare_tiles_id_(source_prepare_tiles_id) { |
| 69 DCHECK(!SkipImage(draw_image)); | 70 DCHECK(!SkipImage(draw_image)); |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Overridden from Task: | 73 // Overridden from Task: |
| 73 void RunOnWorkerThread() override { | 74 void RunOnWorkerThread() override { |
| 74 TRACE_EVENT2("cc", "ImageDecodeTaskImpl::RunOnWorkerThread", "mode", "gpu", | 75 TRACE_EVENT2("cc", "ImageDecodeTaskImpl::RunOnWorkerThread", "mode", "gpu", |
| 75 "source_prepare_tiles_id", source_prepare_tiles_id_); | 76 "source_prepare_tiles_id", source_prepare_tiles_id_); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 90 DrawImage image_; | 91 DrawImage image_; |
| 91 skia::RefPtr<const SkImage> image_ref_; | 92 skia::RefPtr<const SkImage> image_ref_; |
| 92 const uint64_t source_prepare_tiles_id_; | 93 const uint64_t source_prepare_tiles_id_; |
| 93 | 94 |
| 94 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); | 95 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); |
| 95 }; | 96 }; |
| 96 | 97 |
| 97 // Task which creates an image from decoded data. Typically this involves | 98 // Task which creates an image from decoded data. Typically this involves |
| 98 // uploading data to the GPU, which requires this task be run on the non- | 99 // uploading data to the GPU, which requires this task be run on the non- |
| 99 // concurrent thread. | 100 // concurrent thread. |
| 100 class ImageUploadTaskImpl : public ImageDecodeTask { | 101 class ImageUploadTaskImpl : public TileTask { |
| 101 public: | 102 public: |
| 102 ImageUploadTaskImpl(GpuImageDecodeController* controller, | 103 ImageUploadTaskImpl(GpuImageDecodeController* controller, |
| 103 const DrawImage& draw_image, | 104 const DrawImage& draw_image, |
| 104 scoped_refptr<ImageDecodeTask> decode_dependency, | 105 scoped_refptr<TileTask> decode_dependency, |
| 105 uint64_t source_prepare_tiles_id) | 106 uint64_t source_prepare_tiles_id) |
| 106 : ImageDecodeTask(std::move(decode_dependency)), | 107 : TileTask(false), |
| 107 controller_(controller), | 108 controller_(controller), |
| 108 image_(draw_image), | 109 image_(draw_image), |
| 109 image_ref_(skia::SharePtr(draw_image.image())), | 110 image_ref_(skia::SharePtr(draw_image.image())), |
| 110 source_prepare_tiles_id_(source_prepare_tiles_id) { | 111 source_prepare_tiles_id_(source_prepare_tiles_id) { |
| 111 DCHECK(!SkipImage(draw_image)); | 112 DCHECK(!SkipImage(draw_image)); |
| 113 dependencies_.push_back(std::move(decode_dependency)); |
| 112 } | 114 } |
| 113 | 115 |
| 114 // Override from Task: | 116 // Override from Task: |
| 115 void RunOnWorkerThread() override { | 117 void RunOnWorkerThread() override { |
| 116 TRACE_EVENT2("cc", "ImageUploadTaskImpl::RunOnWorkerThread", "mode", "gpu", | 118 TRACE_EVENT2("cc", "ImageUploadTaskImpl::RunOnWorkerThread", "mode", "gpu", |
| 117 "source_prepare_tiles_id", source_prepare_tiles_id_); | 119 "source_prepare_tiles_id", source_prepare_tiles_id_); |
| 118 controller_->UploadImage(image_); | 120 controller_->UploadImage(image_); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void ScheduleOnOriginThread(RasterBufferProvider* provider) override {} | 123 void ScheduleOnOriginThread(RasterBufferProvider* provider) override {} |
| 122 void CompleteOnOriginThread(RasterBufferProvider* provider) override { | 124 void CompleteOnOriginThread(RasterBufferProvider* provider) override { |
| 123 controller_->UploadTaskCompleted(image_); | 125 controller_->UploadTaskCompleted(image_); |
| 124 } | 126 } |
| 125 | 127 |
| 126 // Override from ImageDecodeTask: | |
| 127 bool SupportsConcurrentExecution() const override { return false; } | |
| 128 | |
| 129 protected: | 128 protected: |
| 130 ~ImageUploadTaskImpl() override {} | 129 ~ImageUploadTaskImpl() override {} |
| 131 | 130 |
| 132 private: | 131 private: |
| 133 GpuImageDecodeController* controller_; | 132 GpuImageDecodeController* controller_; |
| 134 DrawImage image_; | 133 DrawImage image_; |
| 135 skia::RefPtr<const SkImage> image_ref_; | 134 skia::RefPtr<const SkImage> image_ref_; |
| 136 uint64_t source_prepare_tiles_id_; | 135 uint64_t source_prepare_tiles_id_; |
| 137 | 136 |
| 138 DISALLOW_COPY_AND_ASSIGN(ImageUploadTaskImpl); | 137 DISALLOW_COPY_AND_ASSIGN(ImageUploadTaskImpl); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 167 | 166 |
| 168 GpuImageDecodeController::~GpuImageDecodeController() { | 167 GpuImageDecodeController::~GpuImageDecodeController() { |
| 169 // SetShouldAggressivelyFreeResources will zero our limits and free all | 168 // SetShouldAggressivelyFreeResources will zero our limits and free all |
| 170 // outstanding image memory. | 169 // outstanding image memory. |
| 171 SetShouldAggressivelyFreeResources(true); | 170 SetShouldAggressivelyFreeResources(true); |
| 172 } | 171 } |
| 173 | 172 |
| 174 bool GpuImageDecodeController::GetTaskForImageAndRef( | 173 bool GpuImageDecodeController::GetTaskForImageAndRef( |
| 175 const DrawImage& draw_image, | 174 const DrawImage& draw_image, |
| 176 uint64_t prepare_tiles_id, | 175 uint64_t prepare_tiles_id, |
| 177 scoped_refptr<ImageDecodeTask>* task) { | 176 scoped_refptr<TileTask>* task) { |
| 178 if (SkipImage(draw_image)) { | 177 if (SkipImage(draw_image)) { |
| 179 *task = nullptr; | 178 *task = nullptr; |
| 180 return false; | 179 return false; |
| 181 } | 180 } |
| 182 | 181 |
| 183 base::AutoLock lock(lock_); | 182 base::AutoLock lock(lock_); |
| 184 const auto image_id = draw_image.image()->uniqueID(); | 183 const auto image_id = draw_image.image()->uniqueID(); |
| 185 | 184 |
| 186 auto found = image_data_.Get(image_id); | 185 auto found = image_data_.Get(image_id); |
| 187 if (found != image_data_.end()) { | 186 if (found != image_data_.end()) { |
| 188 ImageData* image_data = found->second.get(); | 187 ImageData* image_data = found->second.get(); |
| 189 if (image_data->is_at_raster) { | 188 if (image_data->is_at_raster) { |
| 190 // Image is at-raster, just return, this usage will be at-raster as well. | 189 // Image is at-raster, just return, this usage will be at-raster as well. |
| 191 *task = nullptr; | 190 *task = nullptr; |
| 192 return false; | 191 return false; |
| 193 } | 192 } |
| 194 | 193 |
| 195 if (image_data->upload.image) { | 194 if (image_data->upload.image) { |
| 196 // The image is already uploaded, ref and return. | 195 // The image is already uploaded, ref and return. |
| 197 RefImage(draw_image); | 196 RefImage(draw_image); |
| 198 *task = nullptr; | 197 *task = nullptr; |
| 199 return true; | 198 return true; |
| 200 } | 199 } |
| 201 } | 200 } |
| 202 | 201 |
| 203 // We didn't have a pre-uploaded image, so we need an upload task. Try to find | 202 // We didn't have a pre-uploaded image, so we need an upload task. Try to find |
| 204 // an existing one. | 203 // an existing one. |
| 205 scoped_refptr<ImageDecodeTask>& existing_task = | 204 scoped_refptr<TileTask>& existing_task = |
| 206 pending_image_upload_tasks_[image_id]; | 205 pending_image_upload_tasks_[image_id]; |
| 207 if (existing_task) { | 206 if (existing_task) { |
| 208 // We had an existing upload task, ref the image and return the task. | 207 // We had an existing upload task, ref the image and return the task. |
| 209 RefImage(draw_image); | 208 RefImage(draw_image); |
| 210 *task = existing_task; | 209 *task = existing_task; |
| 211 return true; | 210 return true; |
| 212 } | 211 } |
| 213 | 212 |
| 214 // We will be creating a new upload task. If necessary, create a placeholder | 213 // We will be creating a new upload task. If necessary, create a placeholder |
| 215 // ImageData to hold the result. | 214 // ImageData to hold the result. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 | 379 |
| 381 // While the upload task is active, we keep a ref on both the image it will be | 380 // While the upload task is active, we keep a ref on both the image it will be |
| 382 // populating, as well as the decode it needs to populate it. Release these | 381 // populating, as well as the decode it needs to populate it. Release these |
| 383 // refs now. | 382 // refs now. |
| 384 UnrefImageDecode(draw_image); | 383 UnrefImageDecode(draw_image); |
| 385 UnrefImageInternal(draw_image); | 384 UnrefImageInternal(draw_image); |
| 386 } | 385 } |
| 387 | 386 |
| 388 // Checks if an existing image decode exists. If not, returns a task to produce | 387 // Checks if an existing image decode exists. If not, returns a task to produce |
| 389 // the requested decode. | 388 // the requested decode. |
| 390 scoped_refptr<ImageDecodeTask> | 389 scoped_refptr<TileTask> GpuImageDecodeController::GetImageDecodeTaskAndRef( |
| 391 GpuImageDecodeController::GetImageDecodeTaskAndRef(const DrawImage& draw_image, | 390 const DrawImage& draw_image, |
| 392 uint64_t prepare_tiles_id) { | 391 uint64_t prepare_tiles_id) { |
| 393 lock_.AssertAcquired(); | 392 lock_.AssertAcquired(); |
| 394 | 393 |
| 395 const uint32_t image_id = draw_image.image()->uniqueID(); | 394 const uint32_t image_id = draw_image.image()->uniqueID(); |
| 396 | 395 |
| 397 // This ref is kept alive while an upload task may need this decode. We | 396 // This ref is kept alive while an upload task may need this decode. We |
| 398 // release this ref in UploadTaskCompleted. | 397 // release this ref in UploadTaskCompleted. |
| 399 RefImageDecode(draw_image); | 398 RefImageDecode(draw_image); |
| 400 | 399 |
| 401 auto found = image_data_.Peek(image_id); | 400 auto found = image_data_.Peek(image_id); |
| 402 if (found != image_data_.end() && found->second->decode.is_locked) { | 401 if (found != image_data_.end() && found->second->decode.is_locked) { |
| 403 // We should never be creating a decode task for an at raster image. | 402 // We should never be creating a decode task for an at raster image. |
| 404 DCHECK(!found->second->is_at_raster); | 403 DCHECK(!found->second->is_at_raster); |
| 405 // We should never be creating a decode for an already-uploaded image. | 404 // We should never be creating a decode for an already-uploaded image. |
| 406 DCHECK(!found->second->upload.image); | 405 DCHECK(!found->second->upload.image); |
| 407 return nullptr; | 406 return nullptr; |
| 408 } | 407 } |
| 409 | 408 |
| 410 // We didn't have an existing locked image, create a task to lock or decode. | 409 // We didn't have an existing locked image, create a task to lock or decode. |
| 411 scoped_refptr<ImageDecodeTask>& existing_task = | 410 scoped_refptr<TileTask>& existing_task = |
| 412 pending_image_decode_tasks_[image_id]; | 411 pending_image_decode_tasks_[image_id]; |
| 413 if (!existing_task) { | 412 if (!existing_task) { |
| 414 // Ref image decode and create a decode task. This ref will be released in | 413 // Ref image decode and create a decode task. This ref will be released in |
| 415 // DecodeTaskCompleted. | 414 // DecodeTaskCompleted. |
| 416 RefImageDecode(draw_image); | 415 RefImageDecode(draw_image); |
| 417 existing_task = make_scoped_refptr( | 416 existing_task = make_scoped_refptr( |
| 418 new ImageDecodeTaskImpl(this, draw_image, prepare_tiles_id)); | 417 new ImageDecodeTaskImpl(this, draw_image, prepare_tiles_id)); |
| 419 } | 418 } |
| 420 return existing_task; | 419 return existing_task; |
| 421 } | 420 } |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 } | 733 } |
| 735 | 734 |
| 736 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( | 735 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( |
| 737 const DrawImage& draw_image) const { | 736 const DrawImage& draw_image) const { |
| 738 return SkImageInfo::Make( | 737 return SkImageInfo::Make( |
| 739 draw_image.image()->width(), draw_image.image()->height(), | 738 draw_image.image()->width(), draw_image.image()->height(), |
| 740 ResourceFormatToClosestSkColorType(format_), kPremul_SkAlphaType); | 739 ResourceFormatToClosestSkColorType(format_), kPremul_SkAlphaType); |
| 741 } | 740 } |
| 742 | 741 |
| 743 } // namespace cc | 742 } // namespace cc |
| OLD | NEW |