blob: 2dbb8884257f833dba281e8f60f853b9b59ed8ea [file] [log] [blame]
bsalomone5286e02016-01-14 09:24:09 -08001/*
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"
bsalomon273c0f52016-03-31 10:59:06 -070014#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080015
16static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
17 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
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_8888_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.get()[i]);
31 break;
32 }
33 }
34}
35
36static 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
54static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context,
55 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
56 GrSurfaceDesc copyDstDesc;
57 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
58 copyDstDesc.fWidth = rectangleTexture->width();
59 copyDstDesc.fHeight = rectangleTexture->height();
60 copyDstDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon5ec26ae2016-02-25 08:33:02 -080061 SkAutoTUnref<GrTexture> dst(context->textureProvider()->createTexture(
62 copyDstDesc, SkBudgeted::kYes));
bsalomone5286e02016-01-14 09:24:09 -080063 context->copySurface(dst, rectangleTexture);
64 test_read_pixels(reporter, context, dst, expectedPixelValues);
65}
66
67static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
68 GrTexture* rectangleTexture) {
69 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
70 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
71 for (int y = 0; y < rectangleTexture->width(); ++y) {
72 for (int x = 0; x < rectangleTexture->height(); ++x) {
73 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
74 }
75 }
76
77 GrSurfaceDesc copySrcDesc;
78 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
79 copySrcDesc.fWidth = rectangleTexture->width();
80 copySrcDesc.fHeight = rectangleTexture->height();
81 copySrcDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon5ec26ae2016-02-25 08:33:02 -080082 SkAutoTUnref<GrTexture> src(context->textureProvider()->createTexture(
83 copySrcDesc, SkBudgeted::kYes, pixels.get(), 0));
bsalomone5286e02016-01-14 09:24:09 -080084
85 context->copySurface(rectangleTexture, src);
86 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
87}
88
89static void test_clear(skiatest::Reporter* reporter, GrContext* context,
90 GrTexture* rectangleTexture) {
91 if (rectangleTexture->asRenderTarget()) {
robertphillips6c7e3252016-04-27 10:47:51 -070092 sk_sp<GrDrawContext> dc(
robertphillips6738c702016-07-27 12:13:51 -070093 context->makeDrawContext(sk_ref_sp(rectangleTexture->asRenderTarget()),
94 nullptr));
bsalomone5286e02016-01-14 09:24:09 -080095 if (!dc) {
96 ERRORF(reporter, "Could not get GrDrawContext for rectangle texture.");
97 return;
98 }
99
100 // Clear the whole thing.
101 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
102 dc->clear(nullptr, color0, false);
103
104 int w = rectangleTexture->width();
105 int h = rectangleTexture->height();
106 int pixelCnt = w * h;
107 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
108
109 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800110 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800111 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
112 expectedBytes0[0] = GrColorUnpackR(color0);
113 expectedBytes0[1] = GrColorUnpackG(color0);
114 expectedBytes0[2] = GrColorUnpackB(color0);
115 expectedBytes0[3] = GrColorUnpackA(color0);
116 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
117 expectedPixels.get()[i] = expectedColor0;
118 }
119
120 // Clear the the top to a different color.
121 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
122 SkIRect rect = SkIRect::MakeWH(w, h/2);
123 dc->clear(&rect, color1, false);
124
benjaminwagner2a641ee2016-01-15 06:21:18 -0800125 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800126 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
127 expectedBytes1[0] = GrColorUnpackR(color1);
128 expectedBytes1[1] = GrColorUnpackG(color1);
129 expectedBytes1[2] = GrColorUnpackB(color1);
130 expectedBytes1[3] = GrColorUnpackA(color1);
131
132 for (int y = 0; y < h/2; ++y) {
133 for (int x = 0; x < w; ++x) {
134 expectedPixels.get()[y * h + x] = expectedColor1;
135 }
136 }
137
138 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
139 }
140}
141
bsalomon758586c2016-04-06 14:02:39 -0700142DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700143 GrContext* context = ctxInfo.grContext();
144 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800145 static const int kWidth = 13;
146 static const int kHeight = 13;
147
148 GrColor pixels[kWidth * kHeight];
149 for (int y = 0; y < kHeight; ++y) {
150 for (int x = 0; x < kWidth; ++x) {
151 pixels[y * kWidth + x] = y * kWidth + x;
152 }
153 }
154
155 for (int origin = 0; origin < 2; ++origin) {
156 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
157 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
158 pixels);
159
160 if (!rectTexID) {
161 return;
162 }
163
164 // Let GrContext know that we messed with the GL context directly.
165 context->resetContext();
166
167 // Wrap the rectangle texture ID in a GrTexture
168 GrGLTextureInfo rectangleInfo;
169 rectangleInfo.fID = rectTexID;
170 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
171
172 GrBackendTextureDesc rectangleDesc;
173 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
174 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
175 rectangleDesc.fWidth = kWidth;
176 rectangleDesc.fHeight = kHeight;
177 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
178 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
179
180 GrColor refPixels[kWidth * kHeight];
181 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
182 for (int y = 0; y < kHeight; ++y) {
183 for (int x = 0; x < kWidth; ++x) {
184 int y0 = flipRef ? kHeight - y - 1 : y;
185 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
186 }
187 }
188
189 SkAutoTUnref<GrTexture> rectangleTexture(
190 context->textureProvider()->wrapBackendTexture(rectangleDesc));
191 if (!rectangleTexture) {
192 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
193 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
194 continue;
195 }
196
197 test_read_pixels(reporter, context, rectangleTexture, refPixels);
198
199 test_copy_surface_src(reporter, context, rectangleTexture, refPixels);
200
201 test_copy_surface_dst(reporter, context, rectangleTexture);
202
203 test_write_pixels(reporter, context, rectangleTexture);
204
205 test_clear(reporter, context, rectangleTexture);
206
207 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
208 }
209}
210
211#endif