• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #include "src/gpu/graphite/CopyTask.h"
9 
10 #include "src/gpu/graphite/Buffer.h"
11 #include "src/gpu/graphite/CommandBuffer.h"
12 #include "src/gpu/graphite/Log.h"
13 #include "src/gpu/graphite/Texture.h"
14 #include "src/gpu/graphite/TextureProxy.h"
15 
16 namespace skgpu::graphite {
17 
Make(sk_sp<Buffer> srcBuffer,sk_sp<Buffer> dstBuffer)18 sk_sp<CopyBufferToBufferTask> CopyBufferToBufferTask::Make(sk_sp<Buffer> srcBuffer,
19                                                            sk_sp<Buffer> dstBuffer) {
20     SkASSERT(srcBuffer);
21     const size_t size = srcBuffer->size(); // Get size before we move it into Make()
22     return Make(std::move(srcBuffer), 0, std::move(dstBuffer), 0, size);
23 }
24 
Make(sk_sp<Buffer> srcBuffer,size_t srcOffset,sk_sp<Buffer> dstBuffer,size_t dstOffset,size_t size)25 sk_sp<CopyBufferToBufferTask> CopyBufferToBufferTask::Make(sk_sp<Buffer> srcBuffer,
26                                                            size_t srcOffset,
27                                                            sk_sp<Buffer> dstBuffer,
28                                                            size_t dstOffset,
29                                                            size_t size) {
30     SkASSERT(srcBuffer);
31     SkASSERT(size <= srcBuffer->size() - srcOffset);
32     SkASSERT(dstBuffer);
33     SkASSERT(size <= dstBuffer->size() - dstOffset);
34     return sk_sp<CopyBufferToBufferTask>(new CopyBufferToBufferTask(std::move(srcBuffer), srcOffset,
35                                                                     std::move(dstBuffer), dstOffset,
36                                                                     size));
37 }
38 
CopyBufferToBufferTask(sk_sp<Buffer> srcBuffer,size_t srcOffset,sk_sp<Buffer> dstBuffer,size_t dstOffset,size_t size)39 CopyBufferToBufferTask::CopyBufferToBufferTask(sk_sp<Buffer> srcBuffer, size_t srcOffset,
40                                                sk_sp<Buffer> dstBuffer, size_t dstOffset,
41                                                size_t size)
42         : fSrcBuffer(std::move(srcBuffer))
43         , fSrcOffset(srcOffset)
44         , fDstBuffer(std::move(dstBuffer))
45         , fDstOffset(dstOffset)
46         , fSize(size) {}
47 
48 CopyBufferToBufferTask::~CopyBufferToBufferTask() = default;
49 
prepareResources(ResourceProvider *,const RuntimeEffectDictionary *)50 bool CopyBufferToBufferTask::prepareResources(ResourceProvider*, const RuntimeEffectDictionary*) {
51     return true;
52 }
53 
addCommands(Context *,CommandBuffer * commandBuffer,ReplayTargetData)54 bool CopyBufferToBufferTask::addCommands(Context*, CommandBuffer* commandBuffer, ReplayTargetData) {
55     return commandBuffer->copyBufferToBuffer(fSrcBuffer, fSrcOffset, fDstBuffer, fDstOffset, fSize);
56 }
57 
Make(sk_sp<TextureProxy> textureProxy,SkIRect srcRect,sk_sp<Buffer> buffer,size_t bufferOffset,size_t bufferRowBytes)58 sk_sp<CopyTextureToBufferTask> CopyTextureToBufferTask::Make(sk_sp<TextureProxy> textureProxy,
59                                                              SkIRect srcRect,
60                                                              sk_sp<Buffer> buffer,
61                                                              size_t bufferOffset,
62                                                              size_t bufferRowBytes) {
63     return sk_sp<CopyTextureToBufferTask>(new CopyTextureToBufferTask(std::move(textureProxy),
64                                                                       srcRect,
65                                                                       std::move(buffer),
66                                                                       bufferOffset,
67                                                                       bufferRowBytes));
68 }
69 
CopyTextureToBufferTask(sk_sp<TextureProxy> textureProxy,SkIRect srcRect,sk_sp<Buffer> buffer,size_t bufferOffset,size_t bufferRowBytes)70 CopyTextureToBufferTask::CopyTextureToBufferTask(sk_sp<TextureProxy> textureProxy,
71                                                  SkIRect srcRect,
72                                                  sk_sp<Buffer> buffer,
73                                                  size_t bufferOffset,
74                                                  size_t bufferRowBytes)
75         : fTextureProxy(std::move(textureProxy))
76         , fSrcRect(srcRect)
77         , fBuffer(std::move(buffer))
78         , fBufferOffset(bufferOffset)
79         , fBufferRowBytes(bufferRowBytes) {
80 }
81 
~CopyTextureToBufferTask()82 CopyTextureToBufferTask::~CopyTextureToBufferTask() {}
83 
prepareResources(ResourceProvider * resourceProvider,const RuntimeEffectDictionary *)84 bool CopyTextureToBufferTask::prepareResources(ResourceProvider* resourceProvider,
85                                                const RuntimeEffectDictionary*) {
86     if (!fTextureProxy) {
87         SKGPU_LOG_E("No texture proxy specified for CopyTextureToBufferTask");
88         return false;
89     }
90     if (!TextureProxy::InstantiateIfNotLazy(resourceProvider, fTextureProxy.get())) {
91         SKGPU_LOG_E("Could not instantiate texture proxy for CopyTextureToBufferTask!");
92         return false;
93     }
94     return true;
95 }
96 
addCommands(Context *,CommandBuffer * commandBuffer,ReplayTargetData)97 bool CopyTextureToBufferTask::addCommands(Context*,
98                                           CommandBuffer* commandBuffer,
99                                           ReplayTargetData) {
100     return commandBuffer->copyTextureToBuffer(fTextureProxy->refTexture(),
101                                               fSrcRect,
102                                               std::move(fBuffer),
103                                               fBufferOffset,
104                                               fBufferRowBytes);
105 }
106 
107 //--------------------------------------------------------------------------------------------------
108 
Make(sk_sp<TextureProxy> srcProxy,SkIRect srcRect,sk_sp<TextureProxy> dstProxy,SkIPoint dstPoint)109 sk_sp<CopyTextureToTextureTask> CopyTextureToTextureTask::Make(sk_sp<TextureProxy> srcProxy,
110                                                                SkIRect srcRect,
111                                                                sk_sp<TextureProxy> dstProxy,
112                                                                SkIPoint dstPoint) {
113     return sk_sp<CopyTextureToTextureTask>(new CopyTextureToTextureTask(std::move(srcProxy),
114                                                                         srcRect,
115                                                                         std::move(dstProxy),
116                                                                         dstPoint));
117 }
118 
CopyTextureToTextureTask(sk_sp<TextureProxy> srcProxy,SkIRect srcRect,sk_sp<TextureProxy> dstProxy,SkIPoint dstPoint)119 CopyTextureToTextureTask::CopyTextureToTextureTask(sk_sp<TextureProxy> srcProxy,
120                                                    SkIRect srcRect,
121                                                    sk_sp<TextureProxy> dstProxy,
122                                                    SkIPoint dstPoint)
123         : fSrcProxy(std::move(srcProxy))
124         , fSrcRect(srcRect)
125         , fDstProxy(std::move(dstProxy))
126         , fDstPoint(dstPoint) {
127 }
128 
~CopyTextureToTextureTask()129 CopyTextureToTextureTask::~CopyTextureToTextureTask() {}
130 
prepareResources(ResourceProvider * resourceProvider,const RuntimeEffectDictionary *)131 bool CopyTextureToTextureTask::prepareResources(ResourceProvider* resourceProvider,
132                                                 const RuntimeEffectDictionary*) {
133     if (!fSrcProxy) {
134         SKGPU_LOG_E("No src texture proxy specified for CopyTextureToTextureTask");
135         return false;
136     }
137     if (!TextureProxy::InstantiateIfNotLazy(resourceProvider, fSrcProxy.get())) {
138         SKGPU_LOG_E("Could not instantiate src texture proxy for CopyTextureToTextureTask!");
139         return false;
140     }
141     if (!fDstProxy) {
142         SKGPU_LOG_E("No dst texture proxy specified for CopyTextureToTextureTask");
143         return false;
144     }
145     if (!TextureProxy::InstantiateIfNotLazy(resourceProvider, fDstProxy.get())) {
146         SKGPU_LOG_E("Could not instantiate dst texture proxy for CopyTextureToTextureTask!");
147         return false;
148     }
149     return true;
150 }
151 
addCommands(Context *,CommandBuffer * commandBuffer,ReplayTargetData)152 bool CopyTextureToTextureTask::addCommands(Context*,
153                                            CommandBuffer* commandBuffer,
154                                            ReplayTargetData) {
155     return commandBuffer->copyTextureToTexture(fSrcProxy->refTexture(),
156                                                fSrcRect,
157                                                fDstProxy->refTexture(),
158                                                fDstPoint);
159 }
160 
161 } // namespace skgpu::graphite
162