OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 | 9 |
10 // This test is specific to the GPU backend. | 10 // This test is specific to the GPU backend. |
11 #if SK_SUPPORT_GPU | 11 #if SK_SUPPORT_GPU |
12 | 12 |
13 #include "GrContext.h" | 13 #include "GrContext.h" |
14 #include "SkGpuDevice.h" | 14 #include "SkGpuDevice.h" |
15 | 15 |
16 // This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT pr operly. | 16 // This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT pr operly. |
17 static const int X_SIZE = 13; | 17 static const int X_SIZE = 13; |
18 static const int Y_SIZE = 13; | 18 static const int Y_SIZE = 13; |
19 | 19 |
20 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) { | 20 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) { |
21 unsigned char alphaData[X_SIZE][Y_SIZE]; | 21 unsigned char alphaData[X_SIZE * Y_SIZE]; |
22 | |
23 memset(alphaData, 0, X_SIZE * Y_SIZE); | |
24 | 22 |
25 bool match; | 23 bool match; |
26 unsigned char readback[X_SIZE][Y_SIZE]; | 24 |
25 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1}; | |
27 | 26 |
28 for (int rt = 0; rt < 2; ++rt) { | 27 for (int rt = 0; rt < 2; ++rt) { |
29 GrSurfaceDesc desc; | 28 GrSurfaceDesc desc; |
30 // let Skia know we will be using this texture as a render target | 29 // let Skia know we will be using this texture as a render target |
31 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlag s; | 30 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlag s; |
32 // it is a single channel texture | 31 // it is a single channel texture |
33 desc.fConfig = kAlpha_8_GrPixelConfig; | 32 desc.fConfig = kAlpha_8_GrPixelConfig; |
34 desc.fWidth = X_SIZE; | 33 desc.fWidth = X_SIZE; |
35 desc.fHeight = Y_SIZE; | 34 desc.fHeight = Y_SIZE; |
36 | 35 |
37 // We are initializing the texture with zeros here | 36 // We are initializing the texture with zeros here |
37 memset(alphaData, 0, X_SIZE * Y_SIZE); | |
38 SkAutoTUnref<GrTexture> texture( | 38 SkAutoTUnref<GrTexture> texture( |
39 context->textureProvider()->createTexture(desc, false, alphaData, 0) ); | 39 context->textureProvider()->createTexture(desc, false, alphaData, 0) ); |
40 if (!texture) { | 40 if (!texture) { |
41 if (!rt) { | 41 if (!rt) { |
42 ERRORF(reporter, "Could not create alpha texture."); | 42 ERRORF(reporter, "Could not create alpha texture."); |
43 } | 43 } |
44 continue; | 44 continue; |
45 } | 45 } |
46 | 46 |
47 // create a distinctive texture | 47 // create a distinctive texture |
48 for (int y = 0; y < Y_SIZE; ++y) { | 48 for (int y = 0; y < Y_SIZE; ++y) { |
49 for (int x = 0; x < X_SIZE; ++x) { | 49 for (int x = 0; x < X_SIZE; ++x) { |
50 alphaData[x][y] = y*X_SIZE+x; | 50 alphaData[y * X_SIZE + x] = y*X_SIZE+x; |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 // upload the texture | 54 for (auto rowBytes : kRowBytes) { |
55 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 55 // upload the texture (do per-rowbytes iteration because we may over write below). |
56 alphaData, 0); | 56 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
57 alphaData, 0); | |
57 | 58 |
58 // clear readback to something non-zero so we can detect readback failur es | 59 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; |
59 memset(readback, 0x1, X_SIZE * Y_SIZE); | 60 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y _SIZE]); |
61 // clear readback to something non-zero so we can detect readback fa ilures | |
62 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE); | |
60 | 63 |
61 // read the texture back | 64 // read the texture back |
62 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 65 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
63 readback, 0); | 66 readback.get(), rowBytes); |
64 | 67 |
65 // make sure the original & read back versions match | 68 // make sure the original & read back versions match |
66 match = true; | |
67 | |
68 for (int y = 0; y < Y_SIZE && match; ++y) { | |
69 for (int x = 0; x < X_SIZE && match; ++x) { | |
70 if (alphaData[x][y] != readback[x][y]) { | |
71 SkDebugf("Failed alpha readback. Expected: 0x%02x, " | |
72 "Got: 0x%02x at (%d,%d), rt: %d", alphaData[x][y], readback[x][y], x, | |
73 y, rt); | |
74 match = false; | |
75 } | |
76 } | |
77 } | |
78 | |
79 // Now try writing on the single channel texture (if we could create as a RT). | |
80 if (texture->asRenderTarget()) { | |
81 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | |
82 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create( | |
83 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitCont ents)); | |
84 SkCanvas canvas(device); | |
85 | |
86 SkPaint paint; | |
87 | |
88 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); | |
89 | |
90 paint.setColor(SK_ColorWHITE); | |
91 | |
92 canvas.drawRect(rect, paint); | |
93 | |
94 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, r eadback, 0); | |
95 | |
96 match = true; | 69 match = true; |
robertphillips
2016/02/01 15:38:31
Can we have a subroutine for this that we also use
bsalomon
2016/02/01 20:18:10
Done.
| |
97 | 70 |
98 for (int y = 0; y < Y_SIZE && match; ++y) { | 71 for (int y = 0; y < Y_SIZE && match; ++y) { |
99 for (int x = 0; x < X_SIZE && match; ++x) { | 72 for (int x = 0; x < X_SIZE && match; ++x) { |
100 if (0xFF != readback[x][y]) { | 73 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x]; |
101 ERRORF(reporter, | 74 if (alphaData[y * X_SIZE + x] != rbValue) { |
102 "Failed alpha readback after clear. Expected: 0xF F, Got: 0x%02x at " | 75 SkDebugf("Failed alpha readback. Expected: 0x%02x, " |
103 "(%d,%d)", readback[x][y], x, y); | 76 "Got: 0x%02x at (%d,%d), rt:%d, rb:%zd", alphaD ata[y * X_SIZE + x], |
77 rbValue, x, y, rt, rowBytes); | |
104 match = false; | 78 match = false; |
105 } | 79 } |
106 } | 80 } |
107 } | 81 } |
82 | |
83 // Now try writing on the single channel texture (if we could create as a RT). | |
84 if (texture->asRenderTarget()) { | |
85 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | |
86 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create( | |
87 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_Init Contents)); | |
88 SkCanvas canvas(device); | |
89 | |
90 SkPaint paint; | |
91 | |
92 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SI ZE + 10); | |
93 | |
94 paint.setColor(SK_ColorWHITE); | |
95 | |
96 canvas.drawRect(rect, paint); | |
97 | |
98 memset(readback.get(), 0x1, nonZeroRowBytes * Y_SIZE); | |
99 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfi g, readback.get(), | |
100 rowBytes); | |
101 | |
102 match = true; | |
103 for (int y = 0; y < Y_SIZE && match; ++y) { | |
104 for (int x = 0; x < X_SIZE && match; ++x) { | |
105 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x ]; | |
106 if (0xFF != rbValue) { | |
107 ERRORF(reporter, | |
108 "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x" | |
109 " at (%d,%d), rb:%zd", rbValue, x, y, rowByte s); | |
110 match = false; | |
111 } | |
112 } | |
113 } | |
114 } | |
108 } | 115 } |
109 } | 116 } |
110 | 117 |
111 static const GrPixelConfig kRGBAConfigs[] { | 118 static const GrPixelConfig kRGBAConfigs[] { |
112 kRGBA_8888_GrPixelConfig, | 119 kRGBA_8888_GrPixelConfig, |
113 kBGRA_8888_GrPixelConfig, | 120 kBGRA_8888_GrPixelConfig, |
114 kSRGBA_8888_GrPixelConfig | 121 kSRGBA_8888_GrPixelConfig |
115 }; | 122 }; |
116 | 123 |
124 for (int y = 0; y < Y_SIZE; ++y) { | |
125 for (int x = 0; x < X_SIZE; ++x) { | |
126 alphaData[y * X_SIZE + x] = y*X_SIZE+x; | |
127 } | |
128 } | |
129 | |
117 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a tex ture-only src and | 130 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a tex ture-only src and |
118 // once with a render target. | 131 // once with a render target. |
119 for (auto cfg : kRGBAConfigs) { | 132 for (auto cfg : kRGBAConfigs) { |
120 for (int rt = 0; rt < 2; ++rt) { | 133 for (int rt = 0; rt < 2; ++rt) { |
121 GrSurfaceDesc desc; | 134 GrSurfaceDesc desc; |
122 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurface Flags; | 135 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurface Flags; |
123 desc.fConfig = cfg; | 136 desc.fConfig = cfg; |
124 desc.fWidth = X_SIZE; | 137 desc.fWidth = X_SIZE; |
125 desc.fHeight = Y_SIZE; | 138 desc.fHeight = Y_SIZE; |
126 | 139 |
127 uint32_t rgbaData[X_SIZE][Y_SIZE]; | 140 uint32_t rgbaData[X_SIZE * Y_SIZE]; |
128 // Make the alpha channel of the rgba texture come from alphaData. | 141 // Make the alpha channel of the rgba texture come from alphaData. |
129 for (int y = 0; y < Y_SIZE; ++y) { | 142 for (int y = 0; y < Y_SIZE; ++y) { |
130 for (int x = 0; x < X_SIZE; ++x) { | 143 for (int x = 0; x < X_SIZE; ++x) { |
131 rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]); | 144 rgbaData[y * X_SIZE + x] = GrColorPackRGBA(6, 7, 8, alphaDat a[y * X_SIZE + x]); |
132 } | 145 } |
133 } | 146 } |
134 SkAutoTUnref<GrTexture> texture( | 147 SkAutoTUnref<GrTexture> texture( |
135 context->textureProvider()->createTexture(desc, false, rgbaData, 0)); | 148 context->textureProvider()->createTexture(desc, false, rgbaData, 0)); |
136 if (!texture) { | 149 if (!texture) { |
137 // We always expect to be able to create a RGBA texture | 150 // We always expect to be able to create a RGBA texture |
138 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) { | 151 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) { |
139 ERRORF(reporter, "Failed to create RGBA texture."); | 152 ERRORF(reporter, "Failed to create RGBA texture."); |
140 } | 153 } |
141 continue; | 154 continue; |
142 } | 155 } |
143 | 156 |
144 // clear readback to something non-zero so we can detect readback fa ilures | 157 for (auto rowBytes : kRowBytes) { |
145 memset(readback, 0x0, X_SIZE * Y_SIZE); | 158 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; |
146 | 159 |
147 // read the texture back | 160 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y_SIZE]); |
148 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixe lConfig, readback, | 161 // clear readback to something non-zero so we can detect readbac k failures |
robertphillips
2016/02/01 15:38:31
0x0 -> 0x1 ?
bsalomon
2016/02/01 20:18:10
Changed the comment, this doesn't need to be non-z
| |
149 0); | 162 memset(readback.get(), 0x0, nonZeroRowBytes * Y_SIZE); |
150 | 163 |
151 match = true; | 164 // read the texture back |
165 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_Gr PixelConfig, | |
166 readback.get(), rowBytes); | |
152 | 167 |
robertphillips
2016/02/01 15:38:31
... here ?
| |
153 for (int y = 0; y < Y_SIZE && match; ++y) { | 168 match = true; |
154 for (int x = 0; x < X_SIZE && match; ++x) { | 169 for (int y = 0; y < Y_SIZE && match; ++y) { |
155 if (alphaData[x][y] != readback[x][y]) { | 170 for (int x = 0; x < X_SIZE && match; ++x) { |
156 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, | 171 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x ]; |
157 kAlpha_8_GrPixelConfig, readback, 0) ; | 172 if (alphaData[y * X_SIZE + x] != rbValue) { |
158 ERRORF(reporter, | 173 ERRORF(reporter, |
159 "Failed alpha readback from cfg %d. Expected: 0x% 02x, Got: 0x%02x at" | 174 "Failed alpha readback from cfg %d. Expected: 0x%02x, Got: " |
160 " (%d,%d), rt:%d", desc.fConfig, alphaData[x][y], readback[x][y], x, | 175 "0x%02x at (%d,%d), rt:%d, rb:%zd", desc.fCon fig, |
161 y, rt); | 176 alphaData[y * X_SIZE + x], rbValue, x, y, rt, rowBytes); |
162 match = false; | 177 match = false; |
178 } | |
163 } | 179 } |
164 } | 180 } |
165 } | 181 } |
166 } | 182 } |
167 } | 183 } |
168 } | 184 } |
169 | 185 |
170 #endif | 186 #endif |
OLD | NEW |