blob: 10c392ddb0577276fa6aa9c5256d3c7498a545e9 [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"
robertphillips4fd74ae2016-08-03 14:26:53 -070011#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
bsalomone5286e02016-01-14 09:24:09 -080013#include "gl/GrGLGpu.h"
14#include "gl/GrGLUtil.h"
bsalomon273c0f52016-03-31 10:59:06 -070015#include "gl/GLTestContext.h"
bsalomone5286e02016-01-14 09:24:09 -080016
17static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
Ben Wagnerce4d04a2016-11-06 12:46:37 +000018 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
19 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
bsalomone5286e02016-01-14 09:24:09 -080020 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
21 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
Ben Wagnerce4d04a2016-11-06 12:46:37 +000022 bool read = rectangleTexture->readPixels(0, 0, rectangleTexture->width(),
23 rectangleTexture->height(), kRGBA_8888_GrPixelConfig,
24 pixels.get());
bsalomone5286e02016-01-14 09:24:09 -080025 if (!read) {
26 ERRORF(reporter, "Error reading rectangle texture.");
27 }
28 for (int i = 0; i < pixelCnt; ++i) {
29 if (pixels.get()[i] != expectedPixelValues[i]) {
Ben Wagnerce4d04a2016-11-06 12:46:37 +000030 ERRORF(reporter, "Error, rectangle texture pixel value %d should be 0x%08x,"
31 " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]);
bsalomone5286e02016-01-14 09:24:09 -080032 break;
33 }
34 }
35}
36
37static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
38 GrTexture* rectangleTexture) {
39 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
40 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
41 for (int y = 0; y < rectangleTexture->width(); ++y) {
42 for (int x = 0; x < rectangleTexture->height(); ++x) {
43 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y);
44 }
45 }
46 bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(),
47 rectangleTexture->height(), kRGBA_8888_GrPixelConfig,
48 pixels.get());
49 if (!write) {
50 ERRORF(reporter, "Error writing to rectangle texture.");
51 }
52 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
53}
54
55static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context,
56 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
Brian Salomon8b1fb742016-11-03 15:21:06 -040057 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
58 GrSurfaceDesc copyDstDesc;
59 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
60 copyDstDesc.fWidth = rectangleTexture->width();
61 copyDstDesc.fHeight = rectangleTexture->height();
62 copyDstDesc.fFlags = flags;
Hal Canary342b7ac2016-11-04 11:49:42 -040063 sk_sp<GrTexture> dst(
64 context->textureProvider()->createTexture(copyDstDesc, SkBudgeted::kYes));
65 context->copySurface(dst.get(), rectangleTexture);
66 test_read_pixels(reporter, context, dst.get(), expectedPixelValues);
Brian Salomon8b1fb742016-11-03 15:21:06 -040067 }
bsalomone5286e02016-01-14 09:24:09 -080068}
69
70static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
71 GrTexture* rectangleTexture) {
72 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
73 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
74 for (int y = 0; y < rectangleTexture->width(); ++y) {
75 for (int x = 0; x < rectangleTexture->height(); ++x) {
76 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
77 }
78 }
Brian Salomon8b1fb742016-11-03 15:21:06 -040079 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
80 GrSurfaceDesc copySrcDesc;
81 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
82 copySrcDesc.fWidth = rectangleTexture->width();
83 copySrcDesc.fHeight = rectangleTexture->height();
84 copySrcDesc.fFlags = flags;
Hal Canary342b7ac2016-11-04 11:49:42 -040085 sk_sp<GrTexture> src(context->textureProvider()->createTexture(
Brian Salomon8b1fb742016-11-03 15:21:06 -040086 copySrcDesc, SkBudgeted::kYes, pixels.get(), 0));
bsalomone5286e02016-01-14 09:24:09 -080087
Hal Canary342b7ac2016-11-04 11:49:42 -040088 context->copySurface(rectangleTexture, src.get());
Brian Salomon8b1fb742016-11-03 15:21:06 -040089 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
90 }
bsalomone5286e02016-01-14 09:24:09 -080091}
92
93static void test_clear(skiatest::Reporter* reporter, GrContext* context,
94 GrTexture* rectangleTexture) {
95 if (rectangleTexture->asRenderTarget()) {
Brian Osman11052242016-10-27 14:47:55 -040096 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -070097 sk_ref_sp(rectangleTexture->asRenderTarget()),
98 nullptr));
Brian Osman11052242016-10-27 14:47:55 -040099 if (!rtc) {
100 ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture.");
bsalomone5286e02016-01-14 09:24:09 -0800101 return;
102 }
103
104 // Clear the whole thing.
105 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -0400106 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -0800107
108 int w = rectangleTexture->width();
109 int h = rectangleTexture->height();
110 int pixelCnt = w * h;
111 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
112
113 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800114 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800115 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
116 expectedBytes0[0] = GrColorUnpackR(color0);
117 expectedBytes0[1] = GrColorUnpackG(color0);
118 expectedBytes0[2] = GrColorUnpackB(color0);
119 expectedBytes0[3] = GrColorUnpackA(color0);
120 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
121 expectedPixels.get()[i] = expectedColor0;
122 }
123
124 // Clear the the top to a different color.
125 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
126 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -0400127 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -0800128
benjaminwagner2a641ee2016-01-15 06:21:18 -0800129 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800130 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
131 expectedBytes1[0] = GrColorUnpackR(color1);
132 expectedBytes1[1] = GrColorUnpackG(color1);
133 expectedBytes1[2] = GrColorUnpackB(color1);
134 expectedBytes1[3] = GrColorUnpackA(color1);
135
136 for (int y = 0; y < h/2; ++y) {
137 for (int x = 0; x < w; ++x) {
138 expectedPixels.get()[y * h + x] = expectedColor1;
139 }
140 }
141
142 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
143 }
144}
145
bsalomon758586c2016-04-06 14:02:39 -0700146DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700147 GrContext* context = ctxInfo.grContext();
148 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800149 static const int kWidth = 13;
150 static const int kHeight = 13;
151
152 GrColor pixels[kWidth * kHeight];
153 for (int y = 0; y < kHeight; ++y) {
154 for (int x = 0; x < kWidth; ++x) {
155 pixels[y * kWidth + x] = y * kWidth + x;
156 }
157 }
158
159 for (int origin = 0; origin < 2; ++origin) {
160 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
161 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
162 pixels);
163
164 if (!rectTexID) {
165 return;
166 }
167
168 // Let GrContext know that we messed with the GL context directly.
169 context->resetContext();
170
171 // Wrap the rectangle texture ID in a GrTexture
172 GrGLTextureInfo rectangleInfo;
173 rectangleInfo.fID = rectTexID;
174 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
175
176 GrBackendTextureDesc rectangleDesc;
177 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
178 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
179 rectangleDesc.fWidth = kWidth;
180 rectangleDesc.fHeight = kHeight;
181 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
182 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
183
184 GrColor refPixels[kWidth * kHeight];
185 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
186 for (int y = 0; y < kHeight; ++y) {
187 for (int x = 0; x < kWidth; ++x) {
188 int y0 = flipRef ? kHeight - y - 1 : y;
189 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
190 }
191 }
192
bungeman6bd52842016-10-27 09:30:08 -0700193 sk_sp<GrTexture> rectangleTexture(
bsalomone5286e02016-01-14 09:24:09 -0800194 context->textureProvider()->wrapBackendTexture(rectangleDesc));
195 if (!rectangleTexture) {
196 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
197 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
198 continue;
199 }
200
bungeman6bd52842016-10-27 09:30:08 -0700201 test_read_pixels(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800202
bungeman6bd52842016-10-27 09:30:08 -0700203 test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800204
bungeman6bd52842016-10-27 09:30:08 -0700205 test_copy_surface_dst(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800206
bungeman6bd52842016-10-27 09:30:08 -0700207 test_write_pixels(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800208
bungeman6bd52842016-10-27 09:30:08 -0700209 test_clear(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800210
211 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
212 }
213}
214
215#endif