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 && !defined(SK_BUILD_FOR_ANDROID) | 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 textureData[X_SIZE][Y_SIZE]; | 21 unsigned char alphaData[X_SIZE][Y_SIZE]; |
22 | 22 |
23 memset(textureData, 0, X_SIZE * Y_SIZE); | 23 memset(alphaData, 0, X_SIZE * Y_SIZE); |
24 | 24 |
25 GrSurfaceDesc desc; | 25 bool match; |
26 | |
27 // let Skia know we will be using this texture as a render target | |
28 desc.fFlags = kRenderTarget_GrSurfaceFlag; | |
29 // it is a single channel texture | |
30 desc.fConfig = kAlpha_8_GrPixelConfig; | |
31 desc.fWidth = X_SIZE; | |
32 desc.fHeight = Y_SIZE; | |
33 | |
34 // We are initializing the texture with zeros here | |
35 GrTexture* texture = context->textureProvider()->createTexture(desc, false,
textureData, 0); | |
36 if (!texture) { | |
37 return; | |
38 } | |
39 | |
40 SkAutoTUnref<GrTexture> au(texture); | |
41 | |
42 // create a distinctive texture | |
43 for (int y = 0; y < Y_SIZE; ++y) { | |
44 for (int x = 0; x < X_SIZE; ++x) { | |
45 textureData[x][y] = x*Y_SIZE+y; | |
46 } | |
47 } | |
48 | |
49 // upload the texture | |
50 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | |
51 textureData, 0); | |
52 | |
53 unsigned char readback[X_SIZE][Y_SIZE]; | 26 unsigned char readback[X_SIZE][Y_SIZE]; |
54 | 27 |
55 // clear readback to something non-zero so we can detect readback failures | 28 for (int rt = 0; rt < 2; ++rt) { |
56 memset(readback, 0x1, X_SIZE * Y_SIZE); | 29 GrSurfaceDesc desc; |
| 30 // let Skia know we will be using this texture as a render target |
| 31 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlag
s; |
| 32 // it is a single channel texture |
| 33 desc.fConfig = kAlpha_8_GrPixelConfig; |
| 34 desc.fWidth = X_SIZE; |
| 35 desc.fHeight = Y_SIZE; |
57 | 36 |
58 // read the texture back | 37 // We are initializing the texture with zeros here |
59 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 38 SkAutoTUnref<GrTexture> texture( |
60 readback, 0); | 39 context->textureProvider()->createTexture(desc, false, alphaData, 0)
); |
| 40 if (!texture) { |
| 41 if (!rt) { |
| 42 ERRORF(reporter, "Could not create alpha texture."); |
| 43 } |
| 44 continue; |
| 45 } |
61 | 46 |
62 // make sure the original & read back versions match | 47 // create a distinctive texture |
63 bool match = true; | 48 for (int y = 0; y < Y_SIZE; ++y) { |
| 49 for (int x = 0; x < X_SIZE; ++x) { |
| 50 alphaData[x][y] = y*X_SIZE+x; |
| 51 } |
| 52 } |
64 | 53 |
65 for (int y = 0; y < Y_SIZE; ++y) { | 54 // upload the texture |
66 for (int x = 0; x < X_SIZE; ++x) { | 55 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
67 if (textureData[x][y] != readback[x][y]) { | 56 alphaData, 0); |
68 match = false; | 57 |
| 58 // clear readback to something non-zero so we can detect readback failur
es |
| 59 memset(readback, 0x1, X_SIZE * Y_SIZE); |
| 60 |
| 61 // read the texture back |
| 62 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 63 readback, 0); |
| 64 |
| 65 // 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; |
| 97 |
| 98 for (int y = 0; y < Y_SIZE && match; ++y) { |
| 99 for (int x = 0; x < X_SIZE && match; ++x) { |
| 100 if (0xFF != readback[x][y]) { |
| 101 ERRORF(reporter, |
| 102 "Failed alpha readback after clear. Expected: 0xF
F, Got: 0x%02x at " |
| 103 "(%d,%d)", readback[x][y], x, y); |
| 104 match = false; |
| 105 } |
| 106 } |
69 } | 107 } |
70 } | 108 } |
71 } | 109 } |
72 | 110 |
73 REPORTER_ASSERT(reporter, match); | 111 static const GrPixelConfig kRGBAConfigs[] { |
| 112 kRGBA_8888_GrPixelConfig, |
| 113 kBGRA_8888_GrPixelConfig, |
| 114 kSRGBA_8888_GrPixelConfig |
| 115 }; |
74 | 116 |
75 // Now try writing on the single channel texture | 117 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a tex
ture-only src and |
76 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | 118 // once with a render target. |
77 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarge
t(), &props, | 119 for (auto cfg : kRGBAConfigs) { |
78 SkGpuDevice::kUninit_I
nitContents)); | 120 for (int rt = 0; rt < 2; ++rt) { |
79 SkCanvas canvas(device); | 121 GrSurfaceDesc desc; |
| 122 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurface
Flags; |
| 123 desc.fConfig = cfg; |
| 124 desc.fWidth = X_SIZE; |
| 125 desc.fHeight = Y_SIZE; |
80 | 126 |
81 SkPaint paint; | 127 uint32_t rgbaData[X_SIZE][Y_SIZE]; |
| 128 // Make the alpha channel of the rgba texture come from alphaData. |
| 129 for (int y = 0; y < Y_SIZE; ++y) { |
| 130 for (int x = 0; x < X_SIZE; ++x) { |
| 131 rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]); |
| 132 } |
| 133 } |
| 134 SkAutoTUnref<GrTexture> texture( |
| 135 context->textureProvider()->createTexture(desc, false, rgbaData,
0)); |
| 136 if (!texture) { |
| 137 // We always expect to be able to create a RGBA texture |
| 138 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) { |
| 139 ERRORF(reporter, "Failed to create RGBA texture."); |
| 140 } |
| 141 continue; |
| 142 } |
82 | 143 |
83 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); | 144 // clear readback to something non-zero so we can detect readback fa
ilures |
| 145 memset(readback, 0x0, X_SIZE * Y_SIZE); |
84 | 146 |
85 paint.setColor(SK_ColorWHITE); | 147 // read the texture back |
| 148 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixe
lConfig, readback, |
| 149 0); |
86 | 150 |
87 canvas.drawRect(rect, paint); | 151 match = true; |
88 | 152 |
89 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 153 for (int y = 0; y < Y_SIZE && match; ++y) { |
90 readback, 0); | 154 for (int x = 0; x < X_SIZE && match; ++x) { |
91 | 155 if (alphaData[x][y] != readback[x][y]) { |
92 match = true; | 156 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, |
93 | 157 kAlpha_8_GrPixelConfig, readback, 0)
; |
94 for (int y = 0; y < Y_SIZE; ++y) { | 158 ERRORF(reporter, |
95 for (int x = 0; x < X_SIZE; ++x) { | 159 "Failed alpha readback from cfg %d. Expected: 0x%
02x, Got: 0x%02x at" |
96 if (0xFF != readback[x][y]) { | 160 " (%d,%d), rt:%d", desc.fConfig, alphaData[x][y],
readback[x][y], x, |
97 match = false; | 161 y, rt); |
| 162 match = false; |
| 163 } |
| 164 } |
98 } | 165 } |
99 } | 166 } |
100 } | 167 } |
101 | |
102 REPORTER_ASSERT(reporter, match); | |
103 } | 168 } |
104 | 169 |
105 #endif | 170 #endif |
OLD | NEW |