Skip to content

Commit 38e6d22

Browse files
SenorBlancoSkia Commit-Bot
authored andcommitted
Dawn: refactor texture uploading from GrDawnTexture to GrDawnGpu.
We'll need this for a proper implementation of onUpdateBackendTexture(). Change-Id: I4b9ac93934ef39ed3c82a55f7d74fc1f826f7740 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323558 Reviewed-by: Greg Daniel <egdaniel@google.com> Commit-Queue: Stephen White <senorblanco@google.com>
1 parent 041fd0a commit 38e6d22

File tree

4 files changed

+45
-55
lines changed

4 files changed

+45
-55
lines changed

src/gpu/dawn/GrDawnGpu.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "include/gpu/GrBackendSurface.h"
1212
#include "include/gpu/GrContextOptions.h"
1313
#include "include/gpu/GrDirectContext.h"
14+
#include "src/core/SkConvertPixels.h"
1415
#include "src/gpu/GrContextPriv.h"
1516
#include "src/gpu/GrDataUtils.h"
1617
#include "src/gpu/GrGeometryProcessor.h"
@@ -165,8 +166,8 @@ bool GrDawnGpu::onWritePixels(GrSurface* surface, int left, int top, int width,
165166
if (!texture) {
166167
return false;
167168
}
168-
texture->upload(srcColorType, texels, mipLevelCount,
169-
SkIRect::MakeXYWH(left, top, width, height), this->getCopyEncoder());
169+
this->uploadTextureData(srcColorType, texels, mipLevelCount,
170+
SkIRect::MakeXYWH(left, top, width, height), texture->texture());
170171
return true;
171172
}
172173

@@ -340,6 +341,45 @@ GrBackendTexture GrDawnGpu::onCreateBackendTexture(SkISize dimensions,
340341
return GrBackendTexture(dimensions.width(), dimensions.height(), info);
341342
}
342343

344+
void GrDawnGpu::uploadTextureData(GrColorType srcColorType, const GrMipLevel texels[],
345+
int mipLevelCount, const SkIRect& rect,
346+
wgpu::Texture texture) {
347+
uint32_t x = rect.x();
348+
uint32_t y = rect.y();
349+
uint32_t width = rect.width();
350+
uint32_t height = rect.height();
351+
352+
for (int i = 0; i < mipLevelCount; i++) {
353+
const void* src = texels[i].fPixels;
354+
size_t srcRowBytes = texels[i].fRowBytes;
355+
SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
356+
size_t trimRowBytes = width * SkColorTypeBytesPerPixel(colorType);
357+
size_t dstRowBytes = GrDawnRoundRowBytes(trimRowBytes);
358+
size_t size = dstRowBytes * height;
359+
GrStagingBufferManager::Slice slice =
360+
this->stagingBufferManager()->allocateStagingBufferSlice(size);
361+
SkRectMemcpy(slice.fOffsetMapPtr, dstRowBytes, src, srcRowBytes, trimRowBytes, height);
362+
363+
wgpu::BufferCopyView srcBuffer = {};
364+
srcBuffer.buffer = static_cast<GrDawnBuffer*>(slice.fBuffer)->get();
365+
srcBuffer.layout.offset = slice.fOffset;
366+
srcBuffer.layout.bytesPerRow = dstRowBytes;
367+
srcBuffer.layout.rowsPerImage = height;
368+
369+
wgpu::TextureCopyView dstTexture;
370+
dstTexture.texture = texture;
371+
dstTexture.mipLevel = i;
372+
dstTexture.origin = {x, y, 0};
373+
374+
wgpu::Extent3D copySize = {width, height, 1};
375+
this->getCopyEncoder().CopyBufferToTexture(&srcBuffer, &dstTexture, &copySize);
376+
x /= 2;
377+
y /= 2;
378+
width = std::max(1u, width / 2);
379+
height = std::max(1u, height / 2);
380+
}
381+
}
382+
343383
bool GrDawnGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
344384
sk_sp<GrRefCntedCallback> finishedCallback,
345385
const BackendTextureData* data) {

src/gpu/dawn/GrDawnGpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class GrDawnGpu : public GrGpu {
198198

199199
bool onSubmitToGpu(bool syncCpu) override;
200200

201+
void uploadTextureData(GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount,
202+
const SkIRect& rect, wgpu::Texture texture);
203+
201204
void moveStagingBuffersToBusyAndMapAsync();
202205
void checkForCompletedStagingBuffers();
203206

src/gpu/dawn/GrDawnTexture.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "src/gpu/dawn/GrDawnTexture.h"
99

10-
#include "src/core/SkConvertPixels.h"
1110
#include "src/gpu/dawn/GrDawnGpu.h"
1211
#include "src/gpu/dawn/GrDawnTextureRenderTarget.h"
1312
#include "src/gpu/dawn/GrDawnUtil.h"
@@ -113,50 +112,3 @@ void GrDawnTexture::onAbandon() {
113112
GrBackendTexture GrDawnTexture::getBackendTexture() const {
114113
return GrBackendTexture(this->width(), this->height(), fInfo);
115114
}
116-
117-
void GrDawnTexture::upload(GrColorType srcColorType, const GrMipLevel texels[],
118-
int mipLevels, wgpu::CommandEncoder copyEncoder) {
119-
this->upload(srcColorType, texels, mipLevels, SkIRect::MakeWH(width(), height()),
120-
copyEncoder);
121-
}
122-
123-
void GrDawnTexture::upload(GrColorType srcColorType, const GrMipLevel texels[],
124-
int mipLevels, const SkIRect& rect,
125-
wgpu::CommandEncoder copyEncoder) {
126-
wgpu::Device device = this->getDawnGpu()->device();
127-
128-
uint32_t x = rect.x();
129-
uint32_t y = rect.y();
130-
uint32_t width = rect.width();
131-
uint32_t height = rect.height();
132-
133-
for (int i = 0; i < mipLevels; i++) {
134-
const void* src = texels[i].fPixels;
135-
size_t srcRowBytes = texels[i].fRowBytes;
136-
SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
137-
size_t trimRowBytes = width * SkColorTypeBytesPerPixel(colorType);
138-
size_t dstRowBytes = GrDawnRoundRowBytes(trimRowBytes);
139-
size_t size = dstRowBytes * height;
140-
GrStagingBufferManager::Slice slice =
141-
this->getDawnGpu()->stagingBufferManager()->allocateStagingBufferSlice(size);
142-
SkRectMemcpy(slice.fOffsetMapPtr, dstRowBytes, src, srcRowBytes, trimRowBytes, height);
143-
144-
wgpu::BufferCopyView srcBuffer = {};
145-
srcBuffer.buffer = static_cast<GrDawnBuffer*>(slice.fBuffer)->get();
146-
srcBuffer.layout.offset = slice.fOffset;
147-
srcBuffer.layout.bytesPerRow = dstRowBytes;
148-
srcBuffer.layout.rowsPerImage = height;
149-
150-
wgpu::TextureCopyView dstTexture;
151-
dstTexture.texture = fInfo.fTexture;
152-
dstTexture.mipLevel = i;
153-
dstTexture.origin = {x, y, 0};
154-
155-
wgpu::Extent3D copySize = {width, height, 1};
156-
copyEncoder.CopyBufferToTexture(&srcBuffer, &dstTexture, &copySize);
157-
x /= 2;
158-
y /= 2;
159-
width = std::max(1u, width / 2);
160-
height = std::max(1u, height / 2);
161-
}
162-
}

src/gpu/dawn/GrDawnTexture.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class GrDawnTexture : public GrTexture {
3131

3232
void textureParamsModified() override {}
3333

34-
void upload(GrColorType, const GrMipLevel texels[], int mipLevels,
35-
wgpu::CommandEncoder copyEncoder);
36-
void upload(GrColorType, const GrMipLevel texels[], int mipLevels,
37-
const SkIRect& dstRect, wgpu::CommandEncoder copyEncoder);
38-
3934
wgpu::Texture texture() const { return fInfo.fTexture; }
4035
protected:
4136
GrDawnTexture(GrDawnGpu*, SkISize dimensions, const GrDawnTextureInfo&, GrMipmapStatus);

0 commit comments

Comments
 (0)