blob: 01c7537c9d9970cc89ed66e3ffdb1eb02737c69d [file] [log] [blame]
Brian Salomon434c5342016-11-10 11:19:51 -05001/*
2 * Copyright 2016 Google Inc.
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 "Test.h"
9
10#if SK_SUPPORT_GPU
11#include "GrContext.h"
12#include "GrRenderTargetContext.h"
13#include "GrTexture.h"
14#include "effects/GrSimpleTextureEffect.h"
15
16template <typename I>
17static SK_WHEN(std::is_integral<I>::value && 4 == sizeof(I), void)
18check_pixels(skiatest::Reporter* reporter, int w, int h,
19 const I exepctedData[], const I actualData[]) {
20 for (int j = 0; j < h; ++j) {
21 for (int i = 0; i < w; ++i) {
22 I expected = exepctedData[j * w + i];
23 I actual = actualData[j * w + i];
24 if (expected != actual) {
25 ERRORF(reporter, "Expected 0x08%x, got 0x%08x at %d, %d.", expected, actual, i, j);
26 return;
27 }
28 }
29 }
30}
31
32DEF_GPUTEST_FOR_RENDERING_CONTEXTS(IntTexture, reporter, ctxInfo) {
33 GrContext* context = ctxInfo.grContext();
34 if (!context->caps()->isConfigTexturable(kRGBA_8888_sint_GrPixelConfig)) {
35 return;
36 }
37 static const int kS = UINT8_MAX + 1;
38 GrSurfaceDesc desc;
39 desc.fConfig = kRGBA_8888_sint_GrPixelConfig;
40 desc.fWidth = kS;
41 desc.fHeight = kS;
42 sk_sp<GrTexture> texture;
43
44 std::unique_ptr<int32_t> testData(new int32_t[kS * kS]);
45 for (int j = 0; j < kS; ++j) {
46 for (int i = 0; i < kS; ++i) {
47 int32_t r = i - INT8_MIN;
48 int32_t g = j - INT8_MIN;
49 int32_t b = INT8_MAX - r;
50 int32_t a = INT8_MAX - g;
51 testData.get()[j * kS + i] = a << 24 | b << 16 | g << 8 | r;
52 }
53 }
54
55 // Test that attempting to create a integer texture with multiple MIP level fails.
56 GrMipLevel levels[2];
57 levels[0].fPixels = testData.get();
58 levels[0].fRowBytes = kS * sizeof(int32_t);
59 levels[1].fPixels = testData.get();
60 levels[1].fRowBytes = (kS / 2) * sizeof(int32_t);
61 texture.reset(context->textureProvider()->createMipMappedTexture(desc, SkBudgeted::kYes, levels,
62 2));
63 REPORTER_ASSERT(reporter, !texture);
64
65 // Test that we can create a integer texture.
66 texture.reset(context->textureProvider()->createTexture(desc, SkBudgeted::kYes,
67 levels[0].fPixels,
68 levels[0].fRowBytes));
69 REPORTER_ASSERT(reporter, texture);
70 if (!texture) {
71 return;
72 }
73
74 // Test that reading to a non-integer config fails.
75 std::unique_ptr<int32_t> readData(new int32_t[kS * kS]);
76 bool success = texture->readPixels(0, 0, kS, kS, kRGBA_8888_GrPixelConfig, readData.get());
77 REPORTER_ASSERT(reporter, !success);
78 std::unique_ptr<uint16_t> halfData(new uint16_t[4 * kS * kS]);
79 success = texture->readPixels(0, 0, kS, kS, kRGBA_half_GrPixelConfig, halfData.get());
80 REPORTER_ASSERT(reporter, !success);
81
82 // Can read back as ints. (ES only requires being able to read back into 32bit ints which
83 // we don't support. Right now this test is counting on GR_RGBA_INTEGER/GL_BYTE being the
84 // implementation-dependent second format).
85 sk_bzero(readData.get(), sizeof(int32_t) * kS * kS);
86 success = texture->readPixels(0, 0, kS, kS, kRGBA_8888_sint_GrPixelConfig, readData.get());
87 REPORTER_ASSERT(reporter, success);
88 if (success) {
89 check_pixels(reporter, kS, kS, testData.get(), readData.get());
90 }
91
92 // readPixels should fail if we attempt to use the unpremul flag with an integer texture.
93 success = texture->readPixels(0, 0, kS, kS, kRGBA_8888_sint_GrPixelConfig, readData.get(), 0,
94 GrContext::kUnpremul_PixelOpsFlag);
95 REPORTER_ASSERT(reporter, !success);
96
97 // Test that copying from one integer texture to another succeeds.
98 sk_sp<GrTexture> copy(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
99 REPORTER_ASSERT(reporter, copy);
100 if (!copy) {
101 return;
102 }
103 success = context->copySurface(copy.get(), texture.get());
104 REPORTER_ASSERT(reporter, success);
105 if (!success) {
106 return;
107 }
108 sk_bzero(readData.get(), sizeof(int32_t) * kS * kS);
109 success = texture->readPixels(0, 0, kS, kS, kRGBA_8888_sint_GrPixelConfig, readData.get());
110 REPORTER_ASSERT(reporter, success);
111 if (success) {
112 check_pixels(reporter, kS, kS, testData.get(), readData.get());
113 }
114
115 // Test that copying to a non-integer texture fails.
116 GrSurfaceDesc nonIntDesc = desc;
117 nonIntDesc.fConfig = kRGBA_8888_GrPixelConfig;
118 copy.reset(context->textureProvider()->createTexture(nonIntDesc, SkBudgeted::kYes));
119 REPORTER_ASSERT(reporter, copy);
120 if (!copy) {
121 return;
122 }
123 success = context->copySurface(copy.get(), texture.get());
124 REPORTER_ASSERT(reporter, !success);
125 nonIntDesc.fConfig = kRGBA_half_GrPixelConfig;
126 copy.reset(context->textureProvider()->createTexture(nonIntDesc, SkBudgeted::kYes));
127 REPORTER_ASSERT(reporter, copy ||
128 !context->caps()->isConfigTexturable(kRGBA_half_GrPixelConfig));
129 if (copy) {
130 success = context->copySurface(copy.get(), texture.get());
131 REPORTER_ASSERT(reporter, !success);
132 }
133
134 // We overwrite the top left quarter of the texture with the bottom right quarter of the
135 // original data.
136 const void* bottomRightQuarter = testData.get() + kS / 2 * kS + kS / 2;
137 size_t rowBytes = kS * sizeof(int32_t);
138
139 // Can't write pixels from a non-int config.
140 success = texture->writePixels(0, 0, kS/2, kS/2, kRGBA_8888_GrPixelConfig, bottomRightQuarter,
141 rowBytes);
142 REPORTER_ASSERT(reporter, !success);
143
144 // Can't use unpremul flag.
145 success = texture->writePixels(0, 0, kS/2, kS/2, kRGBA_8888_sint_GrPixelConfig,
146 bottomRightQuarter, rowBytes,
147 GrContext::kUnpremul_PixelOpsFlag);
148 REPORTER_ASSERT(reporter, !success);
149
150 success = texture->writePixels(0, 0, kS/2, kS/2, kRGBA_8888_sint_GrPixelConfig,
151 bottomRightQuarter, rowBytes);
152 REPORTER_ASSERT(reporter, success);
153 if (!success) {
154 return;
155 }
156 sk_bzero(readData.get(), sizeof(int32_t) * kS * kS);
157 success = texture->readPixels(0, 0, kS, kS, kRGBA_8888_sint_GrPixelConfig, readData.get());
158 REPORTER_ASSERT(reporter, success);
159 if (!success) {
160 return;
161 }
162 std::unique_ptr<int32_t> overwrittenTestData(new int32_t[kS * kS]);
163 memcpy(overwrittenTestData.get(), testData.get(), sizeof(int32_t) * kS * kS);
164 char* dst = (char*)overwrittenTestData.get();
165 char* src = (char*)(testData.get() + kS/2 * kS + kS/2);
166 for (int i = 0; i < kS/2; ++i) {
167 memcpy(dst, src, sizeof(int32_t) * kS/2);
168 dst += rowBytes;
169 src += rowBytes;
170 }
171 check_pixels(reporter, kS, kS, overwrittenTestData.get(), readData.get());
172
173 // Test drawing from the integer texture to a fixed point texture. To avoid any premul issues
174 // we init the int texture with 0s and 1s and make alpha always be 1. We expect that 1s turn
175 // into 0xffs and zeros stay zero.
176 std::unique_ptr<uint32_t> expectedData(new uint32_t[kS * kS]);
177 std::unique_ptr<uint32_t> actualData(new uint32_t[kS * kS]);
178 for (int i = 0; i < kS*kS; ++i) {
179 int32_t a = 0x1;
180 int32_t b = ((i & 0x1) ? 1 : 0);
181 int32_t g = ((i & 0x1) ? 0 : 1);
182 int32_t r = ((i & 0x2) ? 1 : 0);
183 testData.get()[i] = (a << 24) | (b << 16) | (g << 8) | r;
184 expectedData.get()[i] = ((0xFF * a) << 24) | ((0xFF * b) << 16) |
185 ((0xFF * g) << 8) | (0xFF * r);
186 }
187 texture->writePixels(0, 0, kS, kS, kRGBA_8888_sint_GrPixelConfig, testData.get());
188 sk_sp<GrRenderTargetContext> rtContext = context->makeRenderTargetContext(
189 SkBackingFit::kExact, kS, kS, kRGBA_8888_GrPixelConfig, nullptr);
190
191 for (auto filter : {GrTextureParams::kNone_FilterMode,
192 GrTextureParams::kBilerp_FilterMode,
193 GrTextureParams::kMipMap_FilterMode}) {
194 SkMatrix m;
195 m.setIDiv(kS, kS);
196 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(texture.get(), nullptr, m,
197 filter));
198 REPORTER_ASSERT(reporter, fp);
199 if (!fp) {
200 return;
201 }
202 rtContext->clear(nullptr, 0xDDAABBCC, true);
203 GrPaint paint;
204 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
205 paint.addColorFragmentProcessor(fp);
206 rtContext->drawPaint(GrNoClip(), paint, SkMatrix::I());
207 SkImageInfo readInfo = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
208 kPremul_SkAlphaType);
209 rtContext->readPixels(readInfo, actualData.get(), 0, 0, 0);
210 check_pixels(reporter, kS, kS, expectedData.get(), actualData.get());
211 }
212
213 // No rendering to integer textures.
214 GrSurfaceDesc intRTDesc = desc;
215 intRTDesc.fFlags = kRenderTarget_GrSurfaceFlag;
216 texture.reset(context->textureProvider()->createTexture(intRTDesc, SkBudgeted::kYes));
217 REPORTER_ASSERT(reporter, !texture);
218}
219
220#endif