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

Side by Side Diff: tests/RectangleTextureTest.cpp

Issue 1583863002: Beginning of support for texture rectangles. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comments 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/GrGLSLShaderBuilder.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
(Empty)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9 #if SK_SUPPORT_GPU
10 #include "GrContext.h"
11 #include "GrDrawContext.h"
12 #include "gl/GrGLGpu.h"
13 #include "gl/GrGLUtil.h"
14 #include "gl/SkGLContext.h"
15
16 static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
17 GrTexture* rectangleTexture, uint32_t expectedPixel Values[]) {
18 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
19 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
20 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
21 bool read = rectangleTexture->readPixels(0, 0, rectangleTexture->width(),
22 rectangleTexture->height(), kRGBA_88 88_GrPixelConfig,
23 pixels.get());
24 if (!read) {
25 ERRORF(reporter, "Error reading rectangle texture.");
26 }
27 for (int i = 0; i < pixelCnt; ++i) {
28 if (pixels.get()[i] != expectedPixelValues[i]) {
29 ERRORF(reporter, "Error, rectangle texture pixel value %d should be 0x%08x,"
30 " got 0x%08x.", i, expectedPixelValues[i], pixels.g et()[i]);
31 break;
32 }
33 }
34 }
35
36 static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
37 GrTexture* rectangleTexture) {
38 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
39 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
40 for (int y = 0; y < rectangleTexture->width(); ++y) {
41 for (int x = 0; x < rectangleTexture->height(); ++x) {
42 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y);
43 }
44 }
45 bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(),
46 rectangleTexture->height(), kRGBA _8888_GrPixelConfig,
47 pixels.get());
48 if (!write) {
49 ERRORF(reporter, "Error writing to rectangle texture.");
50 }
51 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
52 }
53
54 static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* conte xt,
55 GrTexture* rectangleTexture, uint32_t expected PixelValues[]) {
56 GrSurfaceDesc copyDstDesc;
57 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
58 copyDstDesc.fWidth = rectangleTexture->width();
59 copyDstDesc.fHeight = rectangleTexture->height();
60 copyDstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
61 SkAutoTUnref<GrTexture> dst(context->textureProvider()->createTexture(copyDs tDesc, true));
62 context->copySurface(dst, rectangleTexture);
63 test_read_pixels(reporter, context, dst, expectedPixelValues);
64 }
65
66 static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* conte xt,
67 GrTexture* rectangleTexture) {
68 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
69 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
70 for (int y = 0; y < rectangleTexture->width(); ++y) {
71 for (int x = 0; x < rectangleTexture->height(); ++x) {
72 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
73 }
74 }
75
76 GrSurfaceDesc copySrcDesc;
77 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
78 copySrcDesc.fWidth = rectangleTexture->width();
79 copySrcDesc.fHeight = rectangleTexture->height();
80 copySrcDesc.fFlags = kRenderTarget_GrSurfaceFlag;
81 SkAutoTUnref<GrTexture> src(context->textureProvider()->createTexture(copySr cDesc, true,
82 pixels .get(), 0));
83
84 context->copySurface(rectangleTexture, src);
85 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
86 }
87
88 static void test_clear(skiatest::Reporter* reporter, GrContext* context,
89 GrTexture* rectangleTexture) {
90 if (rectangleTexture->asRenderTarget()) {
91 SkAutoTUnref<GrDrawContext> dc(context->drawContext(rectangleTexture->as RenderTarget()));
92 if (!dc) {
93 ERRORF(reporter, "Could not get GrDrawContext for rectangle texture. ");
94 return;
95 }
96
97 // Clear the whole thing.
98 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
99 dc->clear(nullptr, color0, false);
100
101 int w = rectangleTexture->width();
102 int h = rectangleTexture->height();
103 int pixelCnt = w * h;
104 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
105
106 // The clear color is a GrColor, our readback is to kRGBA_8888, which ma y be different.
107 uint32_t expectedColor0;
108 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
109 expectedBytes0[0] = GrColorUnpackR(color0);
110 expectedBytes0[1] = GrColorUnpackG(color0);
111 expectedBytes0[2] = GrColorUnpackB(color0);
112 expectedBytes0[3] = GrColorUnpackA(color0);
113 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height (); ++i) {
114 expectedPixels.get()[i] = expectedColor0;
115 }
116
117 // Clear the the top to a different color.
118 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
119 SkIRect rect = SkIRect::MakeWH(w, h/2);
120 dc->clear(&rect, color1, false);
121
122 uint32_t expectedColor1;
123 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
124 expectedBytes1[0] = GrColorUnpackR(color1);
125 expectedBytes1[1] = GrColorUnpackG(color1);
126 expectedBytes1[2] = GrColorUnpackB(color1);
127 expectedBytes1[3] = GrColorUnpackA(color1);
128
129 for (int y = 0; y < h/2; ++y) {
130 for (int x = 0; x < w; ++x) {
131 expectedPixels.get()[y * h + x] = expectedColor1;
132 }
133 }
134
135 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get ());
136 }
137 }
138
139 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RectangleTexture, reporter, context, glContex t) {
140 static const int kWidth = 13;
141 static const int kHeight = 13;
142
143 GrColor pixels[kWidth * kHeight];
144 for (int y = 0; y < kHeight; ++y) {
145 for (int x = 0; x < kWidth; ++x) {
146 pixels[y * kWidth + x] = y * kWidth + x;
147 }
148 }
149
150 for (int origin = 0; origin < 2; ++origin) {
151 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
152 GR_GL_RGBA, GR_GL _UNSIGNED_BYTE,
153 pixels);
154
155 if (!rectTexID) {
156 return;
157 }
158
159 // Let GrContext know that we messed with the GL context directly.
160 context->resetContext();
161
162 // Wrap the rectangle texture ID in a GrTexture
163 GrGLTextureInfo rectangleInfo;
164 rectangleInfo.fID = rectTexID;
165 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
166
167 GrBackendTextureDesc rectangleDesc;
168 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
169 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
170 rectangleDesc.fWidth = kWidth;
171 rectangleDesc.fHeight = kHeight;
172 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_ GrSurfaceOrigin;
173 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectan gleInfo);
174
175 GrColor refPixels[kWidth * kHeight];
176 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
177 for (int y = 0; y < kHeight; ++y) {
178 for (int x = 0; x < kWidth; ++x) {
179 int y0 = flipRef ? kHeight - y - 1 : y;
180 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
181 }
182 }
183
184 SkAutoTUnref<GrTexture> rectangleTexture(
185 context->textureProvider()->wrapBackendTexture(rectangleDesc));
186 if (!rectangleTexture) {
187 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
188 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
189 continue;
190 }
191
192 test_read_pixels(reporter, context, rectangleTexture, refPixels);
193
194 test_copy_surface_src(reporter, context, rectangleTexture, refPixels);
195
196 test_copy_surface_dst(reporter, context, rectangleTexture);
197
198 test_write_pixels(reporter, context, rectangleTexture);
199
200 test_clear(reporter, context, rectangleTexture);
201
202 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
203 }
204 }
205
206 #endif
OLDNEW
« no previous file with comments | « src/gpu/glsl/GrGLSLShaderBuilder.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698