| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef skgpu_graphite_task_UploadTask_DEFINED |
| 9 |
#define skgpu_graphite_task_UploadTask_DEFINED |
| 10 |
|
| 11 |
#include "src/gpu/graphite/task/Task.h" |
| 12 |
|
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkSpan.h" |
| 15 |
#include "include/private/base/SkTArray.h" |
| 16 |
#include "src/gpu/graphite/CommandTypes.h" |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
#include <cstdint> |
| 20 |
#include <functional> |
| 21 |
#include <memory> |
| 22 |
|
| 23 |
class SkColorInfo; |
| 24 |
struct SkIRect; |
| 25 |
enum class SkTextureCompressionType; |
| 26 |
|
| 27 |
namespace skgpu::graphite { |
| 28 |
|
| 29 |
class Buffer; |
| 30 |
class Caps; |
| 31 |
class CommandBuffer; |
| 32 |
class Context; |
| 33 |
class Recorder; |
| 34 |
class ResourceProvider; |
| 35 |
class RuntimeEffectDictionary; |
| 36 |
class ScratchResourceManager; |
| 37 |
class TextureProxy; |
| 38 |
|
| 39 |
struct MipLevel { |
| 40 |
const void* fPixels = nullptr; |
| 41 |
size_t fRowBytes = 0; |
| 42 |
}; |
| 43 |
|
| 44 |
/** |
| 45 |
* The ConditionalUploadContext, if set, is used to determine whether an upload needs to occur |
| 46 |
* on Recording playback. Clients will need to create their own subclasses to store the |
| 47 |
* necessary data and override the needsUpload() method to do this check. |
| 48 |
*/ |
| 49 |
class ConditionalUploadContext { |
| 50 |
public: |
| 51 |
virtual ~ConditionalUploadContext() {} |
| 52 |
|
| 53 |
// Return true if the upload needs to occur; false if it should be skipped this time. |
| 54 |
virtual bool needsUpload(Context*) = 0; |
| 55 |
|
| 56 |
// Return true if the upload should be kept in the task (and possibly re-executed on replay |
| 57 |
// depending on needsUpload()'s return value), or false if it should be discarded and never |
| 58 |
// attempt to be uploaded on any replay. |
| 59 |
virtual bool uploadSubmitted() { return true; } |
| 60 |
}; |
| 61 |
|
| 62 |
/** |
| 63 |
* ImageUploadContext is an implementation of ConditionalUploadContext that returns true on |
| 64 |
* the first call to needsUpload() and then returns false on subsequent calls. This is used to |
| 65 |
* upload an image once and then avoid redundant uploads after that. |
| 66 |
*/ |
| 67 |
class ImageUploadContext : public ConditionalUploadContext { |
| 68 |
public: |
| 69 |
~ImageUploadContext() override {} |
| 70 |
|
| 71 |
// Always upload, since it will be discarded right afterwards |
| 72 |
bool needsUpload(Context*) override { return true; } |
| 73 |
|
| 74 |
// Always return false so the upload instance is discarded after the first execution |
| 75 |
bool uploadSubmitted() override { return false; } |
| 76 |
}; |
| 77 |
|
| 78 |
/** |
| 79 |
* A set of `MipLevel`s, comprising the source data for an upload operation. |
| 80 |
* |
| 81 |
* While preparing the upload source, this class additionally caches some needed information, such |
| 82 |
* as whether the upload can be done on the host. |
| 83 |
*/ |
| 84 |
class UploadSource { |
| 85 |
public: |
| 86 |
static UploadSource Make(const Caps*, |
| 87 |
const TextureProxy& textureProxy, |
| 88 |
const SkColorInfo& srcColorInfo, |
| 89 |
const SkColorInfo& dstColorInfo, |
| 90 |
SkSpan<const MipLevel> levels, |
| 91 |
const SkIRect& dstRect); |
| 92 |
static UploadSource MakeCompressed(const Caps*, |
| 93 |
const TextureProxy& textureProxy, |
| 94 |
const void* data, |
| 95 |
size_t dataSize); |
| 96 |
|
| 97 |
UploadSource(UploadSource&&); |
| 98 |
UploadSource& operator=(UploadSource&&); |
| 99 |
~UploadSource(); |
| 100 |
|
| 101 |
bool isValid() const { return !fLevels.empty(); } |
| 102 |
|
| 103 |
SkSpan<const MipLevel> levels() const { return fLevels; } |
| 104 |
bool canUploadOnHost() const { return fCanUploadOnHost; } |
| 105 |
bool isRGB888Format() const { return fIsRGB888Format; } |
| 106 |
SkTextureCompressionType compression() const { return fCompression; } |
| 107 |
size_t bytesPerPixel() const { return fBytesPerPixel; } |
| 108 |
|
| 109 |
private: |
| 110 |
static UploadSource Invalid() { return {}; } |
| 111 |
|
| 112 |
UploadSource(); |
| 113 |
|
| 114 |
skia_private::STArray<16, MipLevel> fLevels; |
| 115 |
|
| 116 |
// Whether the texture supports uploads directly from host memory. |
| 117 |
bool fCanUploadOnHost = false; |
| 118 |
// Whether the texture is RGB888, which is typically emulated by RGBA8888. |
| 119 |
bool fIsRGB888Format = false; |
| 120 |
// Compression type, if any. |
| 121 |
SkTextureCompressionType fCompression; |
| 122 |
// Bytes per pixel or block (if compressed) |
| 123 |
size_t fBytesPerPixel = 0; |
| 124 |
}; |
| 125 |
|
| 126 |
/** |
| 127 |
* An UploadInstance represents a single set of uploads from a buffer to texture that |
| 128 |
* can be processed in a single command. |
| 129 |
*/ |
| 130 |
class UploadInstance { |
| 131 |
public: |
| 132 |
static UploadInstance Make(Recorder*, |
| 133 |
sk_sp<TextureProxy> textureProxy, |
| 134 |
const SkColorInfo& srcColorInfo, |
| 135 |
const SkColorInfo& dstColorInfo, |
| 136 |
const UploadSource& source, |
| 137 |
const SkIRect& dstRect, |
| 138 |
std::unique_ptr<ConditionalUploadContext>); |
| 139 |
static UploadInstance MakeCompressed(Recorder*, |
| 140 |
sk_sp<TextureProxy> textureProxy, |
| 141 |
const UploadSource& source); |
| 142 |
|
| 143 |
static UploadInstance Invalid() { return {}; } |
| 144 |
|
| 145 |
UploadInstance(UploadInstance&&); |
| 146 |
UploadInstance& operator=(UploadInstance&&); |
| 147 |
~UploadInstance(); |
| 148 |
|
| 149 |
bool isValid() const { return fBuffer != nullptr && fTextureProxy != nullptr; } |
| 150 |
|
| 151 |
bool prepareResources(ResourceProvider*); |
| 152 |
|
| 153 |
// Adds upload command to the given CommandBuffer, returns false if the instance should be |
| 154 |
// discarded. |
| 155 |
Task::Status addCommand(Context*, CommandBuffer*, Task::ReplayTargetData) const; |
| 156 |
|
| 157 |
private: |
| 158 |
friend class UploadTask; |
| 159 |
|
| 160 |
UploadInstance(); |
| 161 |
// Copy data is appended directly after the object is created |
| 162 |
UploadInstance(const Buffer*, |
| 163 |
size_t bytesPerPixel, |
| 164 |
sk_sp<TextureProxy>, |
| 165 |
std::unique_ptr<ConditionalUploadContext> = nullptr); |
| 166 |
|
| 167 |
const Buffer* fBuffer; |
| 168 |
size_t fBytesPerPixel; |
| 169 |
sk_sp<TextureProxy> fTextureProxy; |
| 170 |
skia_private::STArray<1, BufferTextureCopyData> fCopyData; |
| 171 |
std::unique_ptr<ConditionalUploadContext> fConditionalContext; |
| 172 |
}; |
| 173 |
|
| 174 |
/** |
| 175 |
* An UploadList is a mutable collection of UploadCommands. |
| 176 |
* |
| 177 |
* Currently commands are accumulated in order and processed in the same order. Dependency |
| 178 |
* management is expected to be handled by the TaskGraph. |
| 179 |
* |
| 180 |
* When an upload is appended to the list its data will be copied to a Buffer in |
| 181 |
* preparation for a deferred upload. |
| 182 |
*/ |
| 183 |
class UploadList { |
| 184 |
public: |
| 185 |
bool recordUpload(Recorder*, |
| 186 |
sk_sp<TextureProxy> targetProxy, |
| 187 |
const SkColorInfo& srcColorInfo, |
| 188 |
const SkColorInfo& dstColorInfo, |
| 189 |
const UploadSource& source, |
| 190 |
const SkIRect& dstRect, |
| 191 |
std::unique_ptr<ConditionalUploadContext>); |
| 192 |
|
| 193 |
int size() { return fInstances.size(); } |
| 194 |
|
| 195 |
private: |
| 196 |
friend class UploadTask; |
| 197 |
|
| 198 |
skia_private::STArray<1, UploadInstance> fInstances; |
| 199 |
}; |
| 200 |
|
| 201 |
/* |
| 202 |
* An UploadTask is a immutable collection of UploadCommands. |
| 203 |
* |
| 204 |
* When adding commands to the commandBuffer the texture proxies in those |
| 205 |
* commands will be instantiated and the copy command added. |
| 206 |
*/ |
| 207 |
class UploadTask final : public Task { |
| 208 |
public: |
| 209 |
static sk_sp<UploadTask> Make(UploadList*); |
| 210 |
static sk_sp<UploadTask> Make(UploadInstance); |
| 211 |
|
| 212 |
~UploadTask() override; |
| 213 |
|
| 214 |
Status prepareResources(ResourceProvider*, |
| 215 |
ScratchResourceManager*, |
| 216 |
sk_sp<const RuntimeEffectDictionary>) override; |
| 217 |
|
| 218 |
Status addCommands(Context*, CommandBuffer*, ReplayTargetData) override; |
| 219 |
|
| 220 |
bool visitProxies(const std::function<bool(const TextureProxy*)>& visitor, |
| 221 |
bool readsOnly) override { |
| 222 |
// Textures being uploaded to are never read from, so skip all visiting unless readsOnly |
| 223 |
// is false. |
| 224 |
if (!readsOnly) { |
| 225 |
for (int32_t i = 0; i < fInstances.size(); ++i) { |
| 226 |
if (fInstances[i].isValid() && !visitor(fInstances[i].fTextureProxy.get())) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
SK_DUMP_TASKS_CODE(const char* getTaskName() const override { return "Upload Task"; }) |
| 235 |
|
| 236 |
private: |
| 237 |
UploadTask(skia_private::TArray<UploadInstance>&&); |
| 238 |
UploadTask(UploadInstance); |
| 239 |
|
| 240 |
skia_private::STArray<1, UploadInstance> fInstances; |
| 241 |
}; |
| 242 |
|
| 243 |
} // namespace skgpu::graphite |
| 244 |
|
| 245 |
#endif // skgpu_graphite_task_UploadTask_DEFINED |