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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 class ImageDecodeTaskImpl : public ImageDecodeTask { | 60 class ImageDecodeTaskImpl : public ImageDecodeTask { |
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 : controller_(controller), |
66 image_(draw_image), | 66 image_(draw_image), |
67 image_ref_(skia::SharePtr(draw_image.image())), | 67 image_ref_(skia::SharePtr(draw_image.image())), |
68 source_prepare_tiles_id_(source_prepare_tiles_id) { | 68 source_prepare_tiles_id_(source_prepare_tiles_id) { |
69 DCHECK(!SkipImage(draw_image)); | 69 DCHECK(!SkipImage(draw_image)); |
| 70 SetTaskTypeId(TASK_TYPE_IMAGE_DECODE); |
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_); |
76 controller_->DecodeImage(image_); | 77 controller_->DecodeImage(image_); |
77 } | 78 } |
78 | 79 |
79 // Overridden from TileTask: | |
80 void ScheduleOnOriginThread(TileTaskClient* client) override {} | |
81 void CompleteOnOriginThread(TileTaskClient* client) override { | |
82 controller_->DecodeTaskCompleted(image_); | |
83 } | |
84 | |
85 protected: | 80 protected: |
86 ~ImageDecodeTaskImpl() override {} | 81 ~ImageDecodeTaskImpl() override {} |
87 | 82 |
88 private: | 83 private: |
| 84 friend class GpuImageDecodeController; |
| 85 |
89 GpuImageDecodeController* controller_; | 86 GpuImageDecodeController* controller_; |
90 DrawImage image_; | 87 DrawImage image_; |
91 skia::RefPtr<const SkImage> image_ref_; | 88 skia::RefPtr<const SkImage> image_ref_; |
92 const uint64_t source_prepare_tiles_id_; | 89 const uint64_t source_prepare_tiles_id_; |
93 | 90 |
94 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); | 91 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); |
95 }; | 92 }; |
96 | 93 |
97 // Task which creates an image from decoded data. Typically this involves | 94 // 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- | 95 // uploading data to the GPU, which requires this task be run on the non- |
99 // concurrent thread. | 96 // concurrent thread. |
100 class ImageUploadTaskImpl : public ImageDecodeTask { | 97 class ImageUploadTaskImpl : public ImageDecodeTask { |
101 public: | 98 public: |
102 ImageUploadTaskImpl(GpuImageDecodeController* controller, | 99 ImageUploadTaskImpl(GpuImageDecodeController* controller, |
103 const DrawImage& draw_image, | 100 const DrawImage& draw_image, |
104 scoped_refptr<ImageDecodeTask> decode_dependency, | 101 scoped_refptr<ImageDecodeTask> decode_dependency, |
105 uint64_t source_prepare_tiles_id) | 102 uint64_t source_prepare_tiles_id) |
106 : ImageDecodeTask(std::move(decode_dependency)), | 103 : ImageDecodeTask(std::move(decode_dependency)), |
107 controller_(controller), | 104 controller_(controller), |
108 image_(draw_image), | 105 image_(draw_image), |
109 image_ref_(skia::SharePtr(draw_image.image())), | 106 image_ref_(skia::SharePtr(draw_image.image())), |
110 source_prepare_tiles_id_(source_prepare_tiles_id) { | 107 source_prepare_tiles_id_(source_prepare_tiles_id) { |
| 108 SetTaskTypeId(TASK_TYPE_IMAGE_UPLOAD); |
111 DCHECK(!SkipImage(draw_image)); | 109 DCHECK(!SkipImage(draw_image)); |
112 } | 110 } |
113 | 111 |
114 // Override from Task: | 112 // Override from Task: |
115 void RunOnWorkerThread() override { | 113 void RunOnWorkerThread() override { |
116 TRACE_EVENT2("cc", "ImageUploadTaskImpl::RunOnWorkerThread", "mode", "gpu", | 114 TRACE_EVENT2("cc", "ImageUploadTaskImpl::RunOnWorkerThread", "mode", "gpu", |
117 "source_prepare_tiles_id", source_prepare_tiles_id_); | 115 "source_prepare_tiles_id", source_prepare_tiles_id_); |
118 controller_->UploadImage(image_); | 116 controller_->UploadImage(image_); |
119 } | 117 } |
120 | 118 |
121 void ScheduleOnOriginThread(TileTaskClient* client) override {} | |
122 void CompleteOnOriginThread(TileTaskClient* client) override { | |
123 controller_->UploadTaskCompleted(image_); | |
124 } | |
125 | |
126 // Override from ImageDecodeTask: | 119 // Override from ImageDecodeTask: |
127 bool SupportsConcurrentExecution() const override { return false; } | 120 bool SupportsConcurrentExecution() const override { return false; } |
128 | 121 |
129 protected: | 122 protected: |
130 ~ImageUploadTaskImpl() override {} | 123 ~ImageUploadTaskImpl() override {} |
131 | 124 |
132 private: | 125 private: |
| 126 friend class GpuImageDecodeController; |
| 127 |
133 GpuImageDecodeController* controller_; | 128 GpuImageDecodeController* controller_; |
134 DrawImage image_; | 129 DrawImage image_; |
135 skia::RefPtr<const SkImage> image_ref_; | 130 skia::RefPtr<const SkImage> image_ref_; |
136 uint64_t source_prepare_tiles_id_; | 131 uint64_t source_prepare_tiles_id_; |
137 | 132 |
138 DISALLOW_COPY_AND_ASSIGN(ImageUploadTaskImpl); | 133 DISALLOW_COPY_AND_ASSIGN(ImageUploadTaskImpl); |
139 }; | 134 }; |
140 | 135 |
141 GpuImageDecodeController::DecodedImageData::DecodedImageData() | 136 GpuImageDecodeController::DecodedImageData::DecodedImageData() |
142 : ref_count(0), is_locked(false), decode_failure(false) {} | 137 : ref_count(0), is_locked(false), decode_failure(false) {} |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 332 |
338 // We are holding the context lock, so finish cleaning up deleted images | 333 // We are holding the context lock, so finish cleaning up deleted images |
339 // now. | 334 // now. |
340 DeletePendingImages(); | 335 DeletePendingImages(); |
341 } else { | 336 } else { |
342 base::AutoLock lock(lock_); | 337 base::AutoLock lock(lock_); |
343 cached_bytes_limit_ = kMaxGpuImageBytes; | 338 cached_bytes_limit_ = kMaxGpuImageBytes; |
344 } | 339 } |
345 } | 340 } |
346 | 341 |
| 342 void GpuImageDecodeController::ImageDecodeTaskCompleted(Task* task) { |
| 343 base::AutoLock lock(lock_); |
| 344 ImageDecodeTaskImpl* decode_task = static_cast<ImageDecodeTaskImpl*>(task); |
| 345 DCHECK(decode_task); |
| 346 // Decode task is complete, remove it from our list of pending tasks. |
| 347 pending_image_decode_tasks_.erase(decode_task->image_.image()->uniqueID()); |
| 348 |
| 349 // While the decode task is active, we keep a ref on the decoded data. |
| 350 // Release that ref now. |
| 351 UnrefImageDecode(decode_task->image_); |
| 352 } |
| 353 |
| 354 void GpuImageDecodeController::ImageUploadTaskCompleted(Task* task) { |
| 355 base::AutoLock lock(lock_); |
| 356 ImageUploadTaskImpl* upload_task = static_cast<ImageUploadTaskImpl*>(task); |
| 357 DCHECK(upload_task); |
| 358 // Upload task is complete, remove it from our list of pending tasks. |
| 359 pending_image_upload_tasks_.erase(upload_task->image_.image()->uniqueID()); |
| 360 |
| 361 // While the upload task is active, we keep a ref on both the image it will be |
| 362 // populating, as well as the decode it needs to populate it. Release these |
| 363 // refs now. |
| 364 UnrefImageDecode(upload_task->image_); |
| 365 UnrefImageInternal(upload_task->image_); |
| 366 } |
| 367 |
347 void GpuImageDecodeController::DecodeImage(const DrawImage& draw_image) { | 368 void GpuImageDecodeController::DecodeImage(const DrawImage& draw_image) { |
348 base::AutoLock lock(lock_); | 369 base::AutoLock lock(lock_); |
349 auto found = image_data_.Peek(draw_image.image()->uniqueID()); | 370 auto found = image_data_.Peek(draw_image.image()->uniqueID()); |
350 DCHECK(found != image_data_.end()); | 371 DCHECK(found != image_data_.end()); |
351 DCHECK(!found->second->is_at_raster); | 372 DCHECK(!found->second->is_at_raster); |
352 DecodeImageIfNecessary(draw_image, found->second.get()); | 373 DecodeImageIfNecessary(draw_image, found->second.get()); |
353 } | 374 } |
354 | 375 |
355 void GpuImageDecodeController::UploadImage(const DrawImage& draw_image) { | 376 void GpuImageDecodeController::UploadImage(const DrawImage& draw_image) { |
356 ContextProvider::ScopedContextLock context_lock(context_); | 377 ContextProvider::ScopedContextLock context_lock(context_); |
357 base::AutoLock lock(lock_); | 378 base::AutoLock lock(lock_); |
358 auto found = image_data_.Peek(draw_image.image()->uniqueID()); | 379 auto found = image_data_.Peek(draw_image.image()->uniqueID()); |
359 DCHECK(found != image_data_.end()); | 380 DCHECK(found != image_data_.end()); |
360 DCHECK(!found->second->is_at_raster); | 381 DCHECK(!found->second->is_at_raster); |
361 UploadImageIfNecessary(draw_image, found->second.get()); | 382 UploadImageIfNecessary(draw_image, found->second.get()); |
362 } | 383 } |
363 | 384 |
364 void GpuImageDecodeController::DecodeTaskCompleted( | |
365 const DrawImage& draw_image) { | |
366 base::AutoLock lock(lock_); | |
367 // Decode task is complete, remove it from our list of pending tasks. | |
368 pending_image_decode_tasks_.erase(draw_image.image()->uniqueID()); | |
369 | |
370 // While the decode task is active, we keep a ref on the decoded data. | |
371 // Release that ref now. | |
372 UnrefImageDecode(draw_image); | |
373 } | |
374 | |
375 void GpuImageDecodeController::UploadTaskCompleted( | |
376 const DrawImage& draw_image) { | |
377 base::AutoLock lock(lock_); | |
378 // Upload task is complete, remove it from our list of pending tasks. | |
379 pending_image_upload_tasks_.erase(draw_image.image()->uniqueID()); | |
380 | |
381 // 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 | |
383 // refs now. | |
384 UnrefImageDecode(draw_image); | |
385 UnrefImageInternal(draw_image); | |
386 } | |
387 | |
388 // Checks if an existing image decode exists. If not, returns a task to produce | 385 // Checks if an existing image decode exists. If not, returns a task to produce |
389 // the requested decode. | 386 // the requested decode. |
390 scoped_refptr<ImageDecodeTask> | 387 scoped_refptr<ImageDecodeTask> |
391 GpuImageDecodeController::GetImageDecodeTaskAndRef(const DrawImage& draw_image, | 388 GpuImageDecodeController::GetImageDecodeTaskAndRef(const DrawImage& draw_image, |
392 uint64_t prepare_tiles_id) { | 389 uint64_t prepare_tiles_id) { |
393 lock_.AssertAcquired(); | 390 lock_.AssertAcquired(); |
394 | 391 |
395 const uint32_t image_id = draw_image.image()->uniqueID(); | 392 const uint32_t image_id = draw_image.image()->uniqueID(); |
396 | 393 |
397 // This ref is kept alive while an upload task may need this decode. We | 394 // This ref is kept alive while an upload task may need this decode. We |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 } | 731 } |
735 | 732 |
736 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( | 733 SkImageInfo GpuImageDecodeController::CreateImageInfoForDrawImage( |
737 const DrawImage& draw_image) const { | 734 const DrawImage& draw_image) const { |
738 return SkImageInfo::Make( | 735 return SkImageInfo::Make( |
739 draw_image.image()->width(), draw_image.image()->height(), | 736 draw_image.image()->width(), draw_image.image()->height(), |
740 ResourceFormatToClosestSkColorType(format_), kPremul_SkAlphaType); | 737 ResourceFormatToClosestSkColorType(format_), kPremul_SkAlphaType); |
741 } | 738 } |
742 | 739 |
743 } // namespace cc | 740 } // namespace cc |
OLD | NEW |