DrawingJob.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef MOZILLA_GFX_COMMANDBUFFER_H_
  6. #define MOZILLA_GFX_COMMANDBUFFER_H_
  7. #include <stdint.h>
  8. #include "mozilla/RefPtr.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/gfx/Matrix.h"
  11. #include "mozilla/gfx/JobScheduler.h"
  12. #include "mozilla/gfx/IterableArena.h"
  13. #include "mozilla/RefCounted.h"
  14. #include "DrawCommand.h"
  15. namespace mozilla {
  16. namespace gfx {
  17. class DrawingCommand;
  18. class PrintCommand;
  19. class SignalCommand;
  20. class DrawingJob;
  21. class WaitCommand;
  22. class SyncObject;
  23. class MultiThreadedJobQueue;
  24. class DrawTarget;
  25. class DrawingJobBuilder;
  26. class CommandBufferBuilder;
  27. /// Contains a sequence of immutable drawing commands that are typically used by
  28. /// several DrawingJobs.
  29. ///
  30. /// CommandBuffer objects are built using CommandBufferBuilder.
  31. class CommandBuffer : public external::AtomicRefCounted<CommandBuffer>
  32. {
  33. public:
  34. MOZ_DECLARE_REFCOUNTED_TYPENAME(CommandBuffer)
  35. ~CommandBuffer();
  36. const DrawingCommand* GetDrawingCommand(ptrdiff_t aId);
  37. protected:
  38. explicit CommandBuffer(size_t aSize = 256)
  39. : mStorage(IterableArena::GROWABLE, aSize)
  40. {}
  41. IterableArena mStorage;
  42. friend class CommandBufferBuilder;
  43. };
  44. /// Generates CommandBuffer objects.
  45. ///
  46. /// The builder is a separate object to ensure that commands are not added to a
  47. /// submitted CommandBuffer.
  48. class CommandBufferBuilder
  49. {
  50. public:
  51. void BeginCommandBuffer(size_t aBufferSize = 256);
  52. already_AddRefed<CommandBuffer> EndCommandBuffer();
  53. /// Build the CommandBuffer, command after command.
  54. /// This must be used between BeginCommandBuffer and EndCommandBuffer.
  55. template<typename T, typename... Args>
  56. ptrdiff_t AddCommand(Args&&... aArgs)
  57. {
  58. static_assert(IsBaseOf<DrawingCommand, T>::value,
  59. "T must derive from DrawingCommand");
  60. return mCommands->mStorage.Alloc<T>(Forward<Args>(aArgs)...);
  61. }
  62. bool HasCommands() const { return !!mCommands; }
  63. protected:
  64. RefPtr<CommandBuffer> mCommands;
  65. };
  66. /// Stores multiple commands to be executed sequencially.
  67. class DrawingJob : public Job {
  68. public:
  69. ~DrawingJob();
  70. virtual JobStatus Run() override;
  71. protected:
  72. DrawingJob(DrawTarget* aTarget,
  73. IntPoint aOffset,
  74. SyncObject* aStart,
  75. SyncObject* aCompletion,
  76. WorkerThread* aPinToWorker = nullptr);
  77. /// Runs the tasks's destructors and resets the buffer.
  78. void Clear();
  79. std::vector<ptrdiff_t> mCommandOffsets;
  80. RefPtr<CommandBuffer> mCommandBuffer;
  81. uint32_t mCursor;
  82. RefPtr<DrawTarget> mDrawTarget;
  83. IntPoint mOffset;
  84. friend class DrawingJobBuilder;
  85. };
  86. /// Generates DrawingJob objects.
  87. ///
  88. /// The builder is a separate object to ensure that commands are not added to a
  89. /// submitted DrawingJob.
  90. class DrawingJobBuilder {
  91. public:
  92. DrawingJobBuilder();
  93. ~DrawingJobBuilder();
  94. /// Allocates a DrawingJob.
  95. ///
  96. /// call this method before starting to add commands.
  97. void BeginDrawingJob(DrawTarget* aTarget, IntPoint aOffset,
  98. SyncObject* aStart = nullptr);
  99. /// Build the DrawingJob, command after command.
  100. /// This must be used between BeginDrawingJob and EndDrawingJob.
  101. void AddCommand(ptrdiff_t offset)
  102. {
  103. mCommandOffsets.push_back(offset);
  104. }
  105. /// Finalizes and returns the drawing task.
  106. ///
  107. /// If aCompletion is not null, the sync object will be signaled after the
  108. /// task buffer is destroyed (and after the destructor of the tasks have run).
  109. /// In most cases this means after the completion of all tasks in the task buffer,
  110. /// but also when the task buffer is destroyed due to an error.
  111. DrawingJob* EndDrawingJob(CommandBuffer* aCmdBuffer,
  112. SyncObject* aCompletion = nullptr,
  113. WorkerThread* aPinToWorker = nullptr);
  114. /// Returns true between BeginDrawingJob and EndDrawingJob, false otherwise.
  115. bool HasDrawingJob() const { return !!mDrawTarget; }
  116. protected:
  117. std::vector<ptrdiff_t> mCommandOffsets;
  118. RefPtr<DrawTarget> mDrawTarget;
  119. IntPoint mOffset;
  120. RefPtr<SyncObject> mStart;
  121. };
  122. } // namespace
  123. } // namespace
  124. #endif