Skip to content

Commit cb1adb4

Browse files
rphilliSkia Commit-Bot
authored andcommitted
Add srcData version of createBackendTexture API
Change-Id: I9679774d69e087a4ceb24de78e98585382bf8593 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/218553 Commit-Queue: Robert Phillips <robertphillips@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
1 parent 8429422 commit cb1adb4

File tree

11 files changed

+135
-125
lines changed

11 files changed

+135
-125
lines changed

gm/imagefromyuvtextures.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,51 +123,32 @@ class ImageFromYUVTextures : public GpuGM {
123123
}
124124

125125
void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
126-
GrGpu* gpu = context->priv().getGpu();
127-
if (!gpu) {
128-
return;
129-
}
130-
131126
for (int i = 0; i < 3; ++i) {
132127
SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
133-
yuvTextures[i] = gpu->createTestingOnlyBackendTexture(fYUVBmps[i].width(),
134-
fYUVBmps[i].height(),
135-
kAlpha_8_SkColorType,
136-
GrMipMapped::kNo,
137-
GrRenderable::kNo,
138-
fYUVBmps[i].getPixels(),
139-
fYUVBmps[i].rowBytes(),
140-
nullptr);
128+
yuvTextures[i] = context->priv().createBackendTexture(&fYUVBmps[i].pixmap(), 1,
129+
GrRenderable::kNo);
141130
}
142-
context->resetContext();
143131
}
144132

145133
void createResultTexture(GrContext* context, int width, int height,
146134
GrBackendTexture* resultTexture) {
147135
*resultTexture = context->createBackendTexture(
148136
width, height, kRGBA_8888_SkColorType, SkColors::kTransparent,
149137
GrMipMapped::kNo, GrRenderable::kYes);
150-
151-
context->resetContext();
152138
}
153139

154140
void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
155141
if (context->abandoned()) {
156142
return;
157143
}
158144

159-
GrGpu* gpu = context->priv().getGpu();
160-
if (!gpu) {
161-
return;
162-
}
145+
GrFlushInfo flushInfo;
146+
flushInfo.fFlags = kSyncCpu_GrFlushFlag;
147+
context->flush(flushInfo);
163148

164-
context->flush();
165-
gpu->testingOnly_flushGpuAndSync();
166149
for (int i = 0; i < n; ++i) {
167150
context->deleteBackendTexture(textures[i]);
168151
}
169-
170-
context->resetContext();
171152
}
172153

173154
void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {

gm/wacky_yuv_formats.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,9 @@ static void draw_row_label(SkCanvas* canvas, int y, int yuvFormat) {
818818
canvas->drawString(rowLabel, 0, y, font, paint);
819819
}
820820

821-
static GrBackendTexture create_yuva_texture(GrGpu* gpu, const SkBitmap& bm,
821+
static GrBackendTexture create_yuva_texture(GrContext* context, const SkBitmap& bm,
822822
SkYUVAIndex yuvaIndices[4], int texIndex) {
823-
const GrCaps* caps = gpu->caps();
823+
824824

825825
SkASSERT(texIndex >= 0 && texIndex <= 3);
826826
int channelCount = 0;
@@ -832,6 +832,9 @@ static GrBackendTexture create_yuva_texture(GrGpu* gpu, const SkBitmap& bm,
832832
// Need to create an RG texture for two-channel planes
833833
GrBackendTexture tex;
834834
if (2 == channelCount) {
835+
const GrCaps* caps = context->priv().caps();
836+
GrGpu* gpu = context->priv().getGpu();
837+
835838
SkASSERT(kRGBA_8888_SkColorType == bm.colorType());
836839
SkAutoTMalloc<char> pixels(2 * bm.width()*bm.height());
837840
char* currPixel = pixels;
@@ -850,12 +853,7 @@ static GrBackendTexture create_yuva_texture(GrGpu* gpu, const SkBitmap& bm,
850853
pixels, 2*bm.width(), nullptr);
851854
}
852855
if (!tex.isValid()) {
853-
tex = gpu->createTestingOnlyBackendTexture(
854-
bm.width(),
855-
bm.height(),
856-
bm.colorType(),
857-
GrMipMapped::kNo, GrRenderable::kNo,
858-
bm.getPixels(), bm.rowBytes(), nullptr);
856+
tex = context->priv().createBackendTexture(&bm.pixmap(), 1, GrRenderable::kNo);
859857
}
860858
return tex;
861859
}
@@ -961,16 +959,11 @@ class WackyYUVFormatsGM : public GM {
961959
return;
962960
}
963961

964-
GrGpu* gpu = context->priv().getGpu();
965-
if (!gpu) {
966-
return;
967-
}
968-
969962
GrBackendTexture yuvaTextures[4];
970963
SkPixmap yuvaPixmaps[4];
971964

972965
for (int i = 0; i < numTextures; ++i) {
973-
yuvaTextures[i] = create_yuva_texture(gpu, resultBMs[i],
966+
yuvaTextures[i] = create_yuva_texture(context, resultBMs[i],
974967
yuvaIndices, i);
975968
if (yuvaTextures[i].isValid()) {
976969
fBackendTextures.push_back(yuvaTextures[i]);
@@ -1168,14 +1161,9 @@ class YUVMakeColorSpaceGM : public GpuGM {
11681161
continue;
11691162
}
11701163

1171-
GrGpu* gpu = context->priv().getGpu();
1172-
if (!gpu) {
1173-
return;
1174-
}
1175-
11761164
GrBackendTexture yuvaTextures[4];
11771165
for (int i = 0; i < numTextures; ++i) {
1178-
yuvaTextures[i] = create_yuva_texture(gpu, resultBMs[i], yuvaIndices, i);
1166+
yuvaTextures[i] = create_yuva_texture(context, resultBMs[i], yuvaIndices, i);
11791167
if (yuvaTextures[i].isValid()) {
11801168
fBackendTextures.push_back(yuvaTextures[i]);
11811169
}

src/gpu/GrContextPriv.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,60 @@ std::unique_ptr<GrFragmentProcessor> GrContextPriv::createUPMToPMEffect(
367367

368368
return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
369369
}
370+
371+
//////////////////////////////////////////////////////////////////////////////
372+
373+
#include "src/core/SkMipMap.h"
374+
375+
GrBackendTexture GrContextPriv::createBackendTexture(const SkPixmap srcData[], int numLevels,
376+
GrRenderable renderable) {
377+
if (!fContext->asDirectContext()) {
378+
return {};
379+
}
380+
381+
if (this->abandoned()) {
382+
return {};
383+
}
384+
385+
if (!srcData || !numLevels) {
386+
return {};
387+
}
388+
389+
int baseWidth = srcData[0].width();
390+
int baseHeight = srcData[0].height();
391+
SkColorType colorType = srcData[0].colorType();
392+
393+
if (numLevels > 1) {
394+
if (numLevels != SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1) {
395+
return {};
396+
}
397+
398+
int currentWidth = baseWidth;
399+
int currentHeight = baseHeight;
400+
for (int i = 1; i < numLevels; ++i) {
401+
currentWidth = SkTMax(1, currentWidth / 2);
402+
currentHeight = SkTMax(1, currentHeight / 2);
403+
404+
if (srcData[i].colorType() != colorType) {
405+
return {};
406+
}
407+
408+
if (srcData[i].width() != currentWidth || srcData[i].height() != currentHeight) {
409+
return {};
410+
}
411+
}
412+
}
413+
414+
GrBackendFormat backendFormat = this->caps()->getBackendFormatFromColorType(colorType);
415+
if (!backendFormat.isValid()) {
416+
return {};
417+
}
418+
419+
GrGpu* gpu = fContext->fGpu.get();
420+
421+
// TODO: propagate the array of pixmaps interface to GrGpu
422+
return gpu->createBackendTexture(baseWidth, baseHeight, backendFormat,
423+
GrMipMapped::kNo, // TODO: use real mipmap setting here
424+
renderable, srcData[0].addr(), srcData[0].rowBytes(),
425+
nullptr);
426+
}

src/gpu/GrContextPriv.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ class GrContextPriv {
253253
void testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject*);
254254
#endif
255255

256+
// If possible, create a backend texture initialized with the provided pixmap data. The client
257+
// should ensure that the returned backend texture is valid.
258+
// If successful, the created backend texture will be compatible with the provided
259+
// pixmap(s).
260+
// If numLevels is 1 a non-mipMapped texture will result. If a mipMapped texture is desired
261+
// the data for all the mipmap levels must be provided.
262+
GrBackendTexture createBackendTexture(const SkPixmap srcData[], int numLevels, GrRenderable);
263+
256264
private:
257265
explicit GrContextPriv(GrContext* context) : fContext(context) {}
258266
GrContextPriv(const GrContextPriv&); // unimpl

src/gpu/GrGpu.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -450,20 +450,6 @@ void GrGpu::dumpJSON(SkJSONWriter* writer) const {
450450
void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
451451
#endif
452452

453-
GrBackendTexture GrGpu::createTestingOnlyBackendTexture(int w, int h, SkColorType colorType,
454-
GrMipMapped mipMapped,
455-
GrRenderable renderable,
456-
const void* pixels, size_t rowBytes,
457-
const SkColor4f* color) {
458-
GrBackendFormat format = this->caps()->getBackendFormatFromColorType(colorType);
459-
if (!format.isValid()) {
460-
return GrBackendTexture();
461-
}
462-
463-
return this->createBackendTexture(w, h, format, mipMapped, renderable,
464-
pixels, rowBytes, color);
465-
}
466-
467453
#if GR_TEST_UTILS
468454

469455
#if GR_GPU_STATS

src/gpu/GrGpu.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,6 @@ class GrGpu : public SkRefCnt {
378378
Stats* stats() { return &fStats; }
379379
void dumpJSON(SkJSONWriter*) const;
380380

381-
// TODO: remove this method
382-
GrBackendTexture createTestingOnlyBackendTexture(int w, int h, SkColorType,
383-
GrMipMapped, GrRenderable,
384-
const void* pixels,
385-
size_t rowBytes,
386-
const SkColor4f* color);
387-
388381
/**
389382
* Creates a texture directly in the backend API without wrapping it in a GrTexture.
390383
* Must be matched with a call to deleteBackendTexture().

tests/GrSurfaceTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(InitialTextureClear, reporter, context_info)
254254
}
255255

256256
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
257-
auto fillPixels = [](const SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
257+
auto fillPixels = [](SkPixmap* p, const std::function<uint32_t(int x, int y)>& f) {
258258
for (int y = 0; y < p->height(); ++y) {
259259
for (int x = 0; x < p->width(); ++x) {
260260
*p->writable_addr32(x, y) = f(x, y);
@@ -277,9 +277,10 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
277277
static constexpr int kSize = 100;
278278
SkAutoPixmapStorage pixels;
279279
pixels.alloc(SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
280-
fillPixels(&pixels, [](int x, int y) {
281-
return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
282-
});
280+
fillPixels(&pixels,
281+
[](int x, int y) {
282+
return (0xFFU << 24) | (x << 16) | (y << 8) | uint8_t((x * y) & 0xFF);
283+
});
283284

284285
GrContext* context = context_info.grContext();
285286
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
@@ -288,9 +289,8 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadOnlyTexture, reporter, context_info) {
288289
// that they'd succeed if the texture wasn't kRead. We want to be sure we're failing with
289290
// kRead for the right reason.
290291
for (auto ioType : {kRead_GrIOType, kRW_GrIOType}) {
291-
auto backendTex = context->priv().getGpu()->createTestingOnlyBackendTexture(
292-
kSize, kSize, kRGBA_8888_SkColorType, GrMipMapped::kNo, GrRenderable::kYes,
293-
pixels.addr(), 0, nullptr);
292+
auto backendTex = context->priv().createBackendTexture(&pixels, 1, GrRenderable::kYes);
293+
294294
auto proxy = proxyProvider->wrapBackendTexture(backendTex, kTopLeft_GrSurfaceOrigin,
295295
kBorrow_GrWrapOwnership,
296296
GrWrapCacheable::kNo, ioType);

tests/GrTestingBackendTextureUploadTest.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "include/gpu/GrTexture.h"
1111
#include "src/core/SkConvertPixels.h"
12+
#include "src/core/SkAutoPixmapStorage.h"
1213
#include "src/gpu/GrContextPriv.h"
1314
#include "src/gpu/GrGpu.h"
1415
#include "src/gpu/SkGr.h"
@@ -20,12 +21,12 @@ void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context,
2021

2122
const int kWidth = 16;
2223
const int kHeight = 16;
23-
SkAutoTMalloc<GrColor> srcBuffer;
24-
if (doDataUpload) {
25-
srcBuffer.reset(kWidth * kHeight);
26-
fill_pixel_data(kWidth, kHeight, srcBuffer.get());
27-
}
28-
SkAutoTMalloc<GrColor> dstBuffer(kWidth * kHeight);
24+
25+
SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
26+
27+
SkAutoPixmapStorage expectedPixels, actualPixels;
28+
expectedPixels.alloc(ii);
29+
actualPixels.alloc(ii);
2930

3031
const GrCaps* caps = context->priv().caps();
3132
GrGpu* gpu = context->priv().getGpu();
@@ -44,10 +45,23 @@ void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context,
4445
return;
4546
}
4647

47-
GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
48-
kWidth, kHeight, ct,
49-
mipMapped, renderable, srcBuffer, 0,
50-
&SkColors::kTransparent);
48+
GrBackendTexture backendTex;
49+
50+
if (doDataUpload) {
51+
SkASSERT(GrMipMapped::kNo == mipMapped);
52+
53+
fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
54+
55+
backendTex = context->priv().createBackendTexture(&expectedPixels, 1, renderable);
56+
} else {
57+
backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
58+
mipMapped, renderable);
59+
60+
size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
61+
// createBackendTexture will fill the texture with 0's if no data is provided, so
62+
// we set the expected result likewise.
63+
memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
64+
}
5165
if (!backendTex.isValid()) {
5266
return;
5367
}
@@ -62,18 +76,14 @@ void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context,
6276
}
6377
REPORTER_ASSERT(reporter, wrappedTex);
6478

65-
int rowBytes = GrColorTypeBytesPerPixel(grCT) * kWidth;
6679
bool result = gpu->readPixels(wrappedTex.get(), 0, 0, kWidth,
67-
kHeight, grCT, dstBuffer, rowBytes);
80+
kHeight, grCT,
81+
actualPixels.writable_addr32(0, 0),
82+
actualPixels.rowBytes());
6883

69-
if (!doDataUpload) {
70-
// createTestingOnlyBackendTexture will fill the texture with 0's if no data is provided, so
71-
// we set the expected result likewise.
72-
srcBuffer.reset(kWidth * kHeight);
73-
memset(srcBuffer, 0, kWidth * kHeight * sizeof(GrColor));
74-
}
7584
REPORTER_ASSERT(reporter, result);
76-
REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer, dstBuffer,
85+
REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(expectedPixels.addr32(),
86+
actualPixels.addr32(),
7787
kWidth, kHeight));
7888
}
7989

tests/TestUtils.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,15 @@ void fill_pixel_data(int width, int height, GrColor* data) {
100100
bool create_backend_texture(GrContext* context, GrBackendTexture* backendTex,
101101
const SkImageInfo& ii, GrMipMapped mipMapped, SkColor color,
102102
GrRenderable renderable) {
103-
GrGpu* gpu = context->priv().getGpu();
104-
if (!gpu) {
105-
return false;
106-
}
107-
108103
SkBitmap bm;
109104
bm.allocPixels(ii);
110-
// TODO: a SkBitmap::eraseColor would be better here
111105
sk_memset32(bm.getAddr32(0, 0), color, ii.width() * ii.height());
112106

113-
*backendTex = gpu->createTestingOnlyBackendTexture(ii.width(), ii.height(), ii.colorType(),
114-
mipMapped, renderable,
115-
bm.getPixels(), bm.rowBytes(), nullptr);
116-
if (!backendTex->isValid() || !gpu->isTestingOnlyBackendTexture(*backendTex)) {
117-
return false;
118-
}
107+
SkASSERT(GrMipMapped::kNo == mipMapped);
108+
// TODO: replace w/ the color-init version of createBackendTexture once Metal supports it.
109+
*backendTex = context->priv().createBackendTexture(&bm.pixmap(), 1, renderable);
119110

120-
return true;
111+
return backendTex->isValid();
121112
}
122113

123114
void delete_backend_texture(GrContext* context, const GrBackendTexture& backendTex) {
@@ -127,12 +118,12 @@ void delete_backend_texture(GrContext* context, const GrBackendTexture& backendT
127118
context->deleteBackendTexture(backendTex);
128119
}
129120

130-
bool does_full_buffer_contain_correct_color(GrColor* srcBuffer,
131-
GrColor* dstBuffer,
121+
bool does_full_buffer_contain_correct_color(const GrColor* srcBuffer,
122+
const GrColor* dstBuffer,
132123
int width,
133124
int height) {
134-
GrColor* srcPtr = srcBuffer;
135-
GrColor* dstPtr = dstBuffer;
125+
const GrColor* srcPtr = srcBuffer;
126+
const GrColor* dstPtr = dstBuffer;
136127
for (int j = 0; j < height; ++j) {
137128
for (int i = 0; i < width; ++i) {
138129
if (srcPtr[i] != dstPtr[i]) {

0 commit comments

Comments
 (0)