Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tests/ReadWriteAlphaTest.cpp

Issue 1584563002: Make A8 readback work in more cases and improve testing. (Closed) Base URL: https://skia.googlesource.com/skia.git@outputswiz
Patch Set: fix test for ios Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/gl/GrGLGpu.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.");
egdaniel 2016/01/13 19:58:18 just to confirm this check here. We are requiring
bsalomon 2016/01/13 20:04:02 That's correct. Some drivers do not render to eith
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] = x*Y_SIZE+y;
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 // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a tex ture-only src and
112 // once with a render target.
113 for (int cfg = 0; cfg < 2; ++cfg) {
114 for (int rt = 0; rt < 2; ++rt) {
115 GrSurfaceDesc desc;
116 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurface Flags;
117 desc.fConfig = cfg ? kBGRA_8888_GrPixelConfig : kRGBA_8888_GrPixe lConfig;
118 desc.fWidth = X_SIZE;
119 desc.fHeight = Y_SIZE;
74 120
75 // Now try writing on the single channel texture 121 uint32_t rgbaData[X_SIZE][Y_SIZE];
76 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); 122 // Make the alpha channel of the rgba texture come from alphaData.
77 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarge t(), &props, 123 for (int y = 0; y < Y_SIZE; ++y) {
78 SkGpuDevice::kUninit_I nitContents)); 124 for (int x = 0; x < X_SIZE; ++x) {
79 SkCanvas canvas(device); 125 rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]);
126 }
127 }
128 SkAutoTUnref<GrTexture> texture(
129 context->textureProvider()->createTexture(desc, false, rgbaData, 0));
130 if (!texture) {
131 // We always expect to be able to create a RGBA texture
132 if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
133 ERRORF(reporter, "Failed to create RGBA texture.");
134 }
135 continue;
136 }
80 137
81 SkPaint paint; 138 // clear readback to something non-zero so we can detect readback fa ilures
139 memset(readback, 0x0, X_SIZE * Y_SIZE);
82 140
83 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); 141 // read the texture back
142 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixe lConfig, readback,
143 0);
84 144
85 paint.setColor(SK_ColorWHITE); 145 match = true;
86 146
87 canvas.drawRect(rect, paint); 147 for (int y = 0; y < Y_SIZE && match; ++y) {
88 148 for (int x = 0; x < X_SIZE && match; ++x) {
89 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, 149 if (alphaData[x][y] != readback[x][y]) {
90 readback, 0); 150 texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
91 151 kAlpha_8_GrPixelConfig, readback, 0) ;
92 match = true; 152 ERRORF(reporter,
93 153 "Failed alpha readback from cfg %d. Expected: 0x% 02x, Got: 0x%02x at "
94 for (int y = 0; y < Y_SIZE; ++y) { 154 "(%d,%d), rt:%d", desc.fConfig, alphaData[x][y], readback[x][y], x,
95 for (int x = 0; x < X_SIZE; ++x) { 155 y, rt);
96 if (0xFF != readback[x][y]) { 156 match = false;
97 match = false; 157 }
158 }
98 } 159 }
99 } 160 }
100 } 161 }
101
102 REPORTER_ASSERT(reporter, match);
103 } 162 }
104 163
105 #endif 164 #endif
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLGpu.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698