image_decode_accelerator_stub.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2018 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. #ifndef GPU_IPC_SERVICE_IMAGE_DECODE_ACCELERATOR_STUB_H_
  5. #define GPU_IPC_SERVICE_IMAGE_DECODE_ACCELERATOR_STUB_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/containers/queue.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/thread_annotations.h"
  14. #include "gpu/command_buffer/service/sequence_id.h"
  15. #include "gpu/ipc/common/gpu_channel.mojom.h"
  16. #include "gpu/ipc/service/gpu_ipc_service_export.h"
  17. #include "gpu/ipc/service/image_decode_accelerator_worker.h"
  18. #include "ui/gfx/geometry/size.h"
  19. namespace base {
  20. class SingleThreadTaskRunner;
  21. } // namespace base
  22. namespace gpu {
  23. class GpuChannel;
  24. class ImageFactory;
  25. class SyncPointClientState;
  26. // Processes incoming image decode requests from renderers: it schedules the
  27. // decode with the appropriate hardware decode accelerator and releases sync
  28. // tokens as decodes complete. These sync tokens must be generated on the client
  29. // side (in ImageDecodeAcceleratorProxy) using the following information:
  30. //
  31. // - The command buffer namespace is GPU_IO.
  32. // - The command buffer ID is created using the
  33. // CommandBufferIdFromChannelAndRoute() function using
  34. // GpuChannelReservedRoutes::kImageDecodeAccelerator as the route ID.
  35. // - The release count should be incremented for each decode request.
  36. //
  37. // An object of this class is meant to be used in
  38. // both the IO thread (for receiving decode requests) and the main thread (for
  39. // processing completed decodes).
  40. class GPU_IPC_SERVICE_EXPORT ImageDecodeAcceleratorStub
  41. : public base::RefCountedThreadSafe<ImageDecodeAcceleratorStub> {
  42. public:
  43. // TODO(andrescj): right now, we only accept one worker to be used for JPEG
  44. // decoding. If we want to use multiple workers, we need to ensure that sync
  45. // tokens are released in order.
  46. ImageDecodeAcceleratorStub(ImageDecodeAcceleratorWorker* worker,
  47. GpuChannel* channel,
  48. int32_t route_id);
  49. ImageDecodeAcceleratorStub(const ImageDecodeAcceleratorStub&) = delete;
  50. ImageDecodeAcceleratorStub& operator=(const ImageDecodeAcceleratorStub&) =
  51. delete;
  52. // Processes a decode request. Must be called on the IO thread.
  53. void ScheduleImageDecode(mojom::ScheduleImageDecodeParamsPtr params,
  54. uint64_t release_count);
  55. // Called on the main thread to indicate that |channel_| should no longer be
  56. // used.
  57. void Shutdown();
  58. void SetImageFactoryForTesting(ImageFactory* image_factory);
  59. private:
  60. friend class base::RefCountedThreadSafe<ImageDecodeAcceleratorStub>;
  61. ~ImageDecodeAcceleratorStub();
  62. // Creates the service-side cache entry for a completed decode and releases
  63. // the decode sync token. If the decode was unsuccessful, no cache entry is
  64. // created but the decode sync token is still released.
  65. void ProcessCompletedDecode(mojom::ScheduleImageDecodeParamsPtr params_ptr,
  66. uint64_t decode_release_count);
  67. // Releases the decode sync token corresponding to |decode_release_count| and
  68. // disables |sequence_| if there are no more decodes to process for now.
  69. void FinishCompletedDecode(uint64_t decode_release_count)
  70. EXCLUSIVE_LOCKS_REQUIRED(lock_);
  71. // The |worker_| calls this when a decode is completed. |result| is enqueued
  72. // and |sequence_| is enabled so that ProcessCompletedDecode() picks it up.
  73. void OnDecodeCompleted(
  74. gfx::Size expected_output_size,
  75. std::unique_ptr<ImageDecodeAcceleratorWorker::DecodeResult> result);
  76. // The object to which the actual decoding can be delegated.
  77. raw_ptr<ImageDecodeAcceleratorWorker> worker_ = nullptr;
  78. base::Lock lock_;
  79. raw_ptr<GpuChannel> channel_ GUARDED_BY(lock_) = nullptr;
  80. SequenceId sequence_ GUARDED_BY(lock_);
  81. scoped_refptr<SyncPointClientState> sync_point_client_state_
  82. GUARDED_BY(lock_);
  83. base::queue<std::unique_ptr<ImageDecodeAcceleratorWorker::DecodeResult>>
  84. pending_completed_decodes_ GUARDED_BY(lock_);
  85. raw_ptr<ImageFactory> external_image_factory_for_testing_ = nullptr;
  86. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  87. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  88. };
  89. } // namespace gpu
  90. #endif // GPU_IPC_SERVICE_IMAGE_DECODE_ACCELERATOR_STUB_H_