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

Side by Side Diff: tests/ReadWriteAlphaTest.cpp

Issue 1530713002: make alpha read back of bgra/rgba work (Closed) Base URL: https://skia.googlesource.com/skia.git@align
Patch Set: cleanup Created 5 years 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/builders/GrGLProgramBuilder.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 && !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); 98
99 for (int y = 0; y < Y_SIZE && match; ++y) {
100 for (int x = 0; x < X_SIZE && match; ++x) {
101 if (0xFF != readback[x][y]) {
102 ERRORF(reporter,
103 "Failed alpha readback after clear. Expected: 0xFF, G ot: 0x%02x at (%d,%d)",
104 readback[x][y], x, y);
105 match = false;
106 }
107 }
108 }
109 }
110
111 // Attempt to read back just alpha from a RGBA texture
112 desc.fFlags = kNone_GrSurfaceFlags;
113 desc.fConfig = kRGBA_8888_GrPixelConfig;
114 uint32_t rgbaData[X_SIZE][Y_SIZE];
115 // Make the alpha channel of the rgba texture come from alphaData.
116 for (int y = 0; y < Y_SIZE; ++y) {
117 for (int x = 0; x < X_SIZE; ++x) {
118 rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]);
119 }
120 }
121 texture.reset(context->textureProvider()->createTexture(desc, false, rgbaDat a, 0));
122 if (!texture) {
123 ERRORF(reporter, "Failed to create RGBA texture.");
124 return;
125 }
126
127 // clear readback to something non-zero so we can detect readback failures
128 memset(readback, 0x0, X_SIZE * Y_SIZE);
129
130 // read the texture back
131 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig, readback, 0);
91 132
92 match = true; 133 match = true;
93 134
94 for (int y = 0; y < Y_SIZE; ++y) { 135 for (int y = 0; y < Y_SIZE && match; ++y) {
95 for (int x = 0; x < X_SIZE; ++x) { 136 for (int x = 0; x < X_SIZE && match; ++x) {
96 if (0xFF != readback[x][y]) { 137 if (alphaData[x][y] != readback[x][y]) {
138 ERRORF(reporter, "Failed alpha readback from RGBA. Expected: 0x% 02x, Got: 0x%02x at (%d,%d)",
139 alphaData[x][y], readback[x][y], x, y);
97 match = false; 140 match = false;
98 } 141 }
99 } 142 }
100 } 143 }
101
102 REPORTER_ASSERT(reporter, match);
103 } 144 }
104 145
105 #endif 146 #endif
OLDNEW
« no previous file with comments | « src/gpu/gl/builders/GrGLProgramBuilder.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698