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

Side by Side Diff: tests/ReadWriteAlphaTest.cpp

Issue 1576023002: Make readback of alpha channel work for RGBA. (Closed) Base URL: https://skia.googlesource.com/skia.git@swiz
Patch Set: more 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/glsl/GrGLSLCaps.h ('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 && !defined(SK_BUILD_FOR_ANDROID)
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 GrSurfaceDesc desc;
26 26
27 // let Skia know we will be using this texture as a render target 27 // let Skia know we will be using this texture as a render target
28 desc.fFlags = kRenderTarget_GrSurfaceFlag; 28 desc.fFlags = kRenderTarget_GrSurfaceFlag;
29 // it is a single channel texture 29 // it is a single channel texture
30 desc.fConfig = kAlpha_8_GrPixelConfig; 30 desc.fConfig = kAlpha_8_GrPixelConfig;
31 desc.fWidth = X_SIZE; 31 desc.fWidth = X_SIZE;
32 desc.fHeight = Y_SIZE; 32 desc.fHeight = Y_SIZE;
33 33
34 // We are initializing the texture with zeros here 34 // We are initializing the texture with zeros here
35 GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0); 35 SkAutoTUnref<GrTexture> texture(
36 context->textureProvider()->createTexture(desc, false, alphaData, 0));
36 if (!texture) { 37 if (!texture) {
37 return; 38 // Try again as a non-RT;
39 desc.fFlags = kNone_GrSurfaceFlags;
40 texture.reset(context->textureProvider()->createTexture(desc, false, alp haData, 0));
41 if (!texture) {
42 ERRORF(reporter, "Could not create A8 texture.");
43 return;
44 }
38 } 45 }
39 46
40 SkAutoTUnref<GrTexture> au(texture);
41
42 // create a distinctive texture 47 // create a distinctive texture
43 for (int y = 0; y < Y_SIZE; ++y) { 48 for (int y = 0; y < Y_SIZE; ++y) {
44 for (int x = 0; x < X_SIZE; ++x) { 49 for (int x = 0; x < X_SIZE; ++x) {
45 textureData[x][y] = x*Y_SIZE+y; 50 alphaData[x][y] = x*Y_SIZE+y;
46 } 51 }
47 } 52 }
48 53
49 // upload the texture 54 // upload the texture
50 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, 55 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
51 textureData, 0); 56 alphaData, 0);
52 57
53 unsigned char readback[X_SIZE][Y_SIZE]; 58 unsigned char readback[X_SIZE][Y_SIZE];
54 59
55 // clear readback to something non-zero so we can detect readback failures 60 // clear readback to something non-zero so we can detect readback failures
56 memset(readback, 0x1, X_SIZE * Y_SIZE); 61 memset(readback, 0x1, X_SIZE * Y_SIZE);
57 62
58 // read the texture back 63 // read the texture back
59 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, 64 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
60 readback, 0); 65 readback, 0);
61 66
62 // make sure the original & read back versions match 67 // make sure the original & read back versions match
63 bool match = true; 68 bool match = true;
64 69
65 for (int y = 0; y < Y_SIZE; ++y) { 70 for (int y = 0; y < Y_SIZE && match; ++y) {
66 for (int x = 0; x < X_SIZE; ++x) { 71 for (int x = 0; x < X_SIZE && match; ++x) {
67 if (textureData[x][y] != readback[x][y]) { 72 if (alphaData[x][y] != readback[x][y]) {
73 ERRORF(reporter, "Failed alpha readback. Expected: 0x%02x, Got: 0x%02x at (%d,%d)",
74 alphaData[x][y], readback[x][y], x, y);
68 match = false; 75 match = false;
69 } 76 }
70 } 77 }
71 } 78 }
72 79
73 REPORTER_ASSERT(reporter, match); 80 // Now try writing on the single channel texture (if we could create as a RT ).
81 if (texture->asRenderTarget()) {
82 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
83 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderT arget(), &props,
84 SkGpuDevice::kUnin it_InitContents));
85 SkCanvas canvas(device);
74 86
75 // Now try writing on the single channel texture 87 SkPaint paint;
76 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
77 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarge t(), &props,
78 SkGpuDevice::kUninit_I nitContents));
79 SkCanvas canvas(device);
80 88
81 SkPaint paint; 89 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10) ;
82 90
83 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); 91 paint.setColor(SK_ColorWHITE);
84 92
85 paint.setColor(SK_ColorWHITE); 93 canvas.drawRect(rect, paint);
86 94
87 canvas.drawRect(rect, paint); 95 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readb ack, 0);
88 96
89 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, 97 match = true;
90 readback, 0);
91 98
92 match = true; 99 for (int y = 0; y < Y_SIZE && match; ++y) {
93 100 for (int x = 0; x < X_SIZE && match; ++x) {
94 for (int y = 0; y < Y_SIZE; ++y) { 101 if (0xFF != readback[x][y]) {
95 for (int x = 0; x < X_SIZE; ++x) { 102 ERRORF(reporter,
96 if (0xFF != readback[x][y]) { 103 "Failed alpha readback after clear. Expected: 0xFF, G ot: 0x%02x at "
97 match = false; 104 "(%d,%d)", readback[x][y], x, y);
105 match = false;
106 }
98 } 107 }
99 } 108 }
100 } 109 }
101 110
102 REPORTER_ASSERT(reporter, match); 111 // Attempt to read back just alpha from a RGBA texture. Once with a texture- only src and
112 // once with a render target.
113 for (int i = 0; i < 2; ++i) {
114 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags ;
115 desc.fConfig = kRGBA_8888_GrPixelConfig;
116 uint32_t rgbaData[X_SIZE][Y_SIZE];
117 // Make the alpha channel of the rgba texture come from alphaData.
118 for (int y = 0; y < Y_SIZE; ++y) {
119 for (int x = 0; x < X_SIZE; ++x) {
120 rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]);
121 }
122 }
123 texture.reset(context->textureProvider()->createTexture(desc, false, rgb aData, 0));
124 if (!texture) {
125 ERRORF(reporter, "Failed to create RGBA texture.");
126 return;
127 }
128
129 // clear readback to something non-zero so we can detect readback failur es
130 memset(readback, 0x0, X_SIZE * Y_SIZE);
131
132 // read the texture back
133 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelCon fig, readback, 0);
134
135 match = true;
136
137 for (int y = 0; y < Y_SIZE && match; ++y) {
138 for (int x = 0; x < X_SIZE && match; ++x) {
139 if (alphaData[x][y] != readback[x][y]) {
140 ERRORF(reporter,
141 "Failed alpha readback from RGBA. Expected: 0x%02x, G ot: 0x%02x at "
142 "(%d,%d)", alphaData[x][y], readback[x][y], x, y);
143 match = false;
144 }
145 }
146 }
147 }
103 } 148 }
104 149
105 #endif 150 #endif
OLDNEW
« no previous file with comments | « src/gpu/glsl/GrGLSLCaps.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698