blob: a0c5409142fde382cb2d3e83747d3c31ca6fbb78 [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,
Robert Phillipse2f7d182016-12-15 09:23:05 -050018 GrSurface* texture, uint32_t expectedPixelValues[]) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050019 int pixelCnt = texture->width() * texture->height();
bsalomone5286e02016-01-14 09:24:09 -080020 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
21 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
Brian Salomon739c5bf2016-11-07 09:53:44 -050022 bool read = texture->readPixels(0, 0, texture->width(), texture->height(),
23 kRGBA_8888_GrPixelConfig, pixels.get());
bsalomone5286e02016-01-14 09:24:09 -080024 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]) {
Brian Salomon739c5bf2016-11-07 09:53:44 -050029 ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i,
30 expectedPixelValues[i], pixels.get()[i]);
bsalomone5286e02016-01-14 09:24:09 -080031 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,
Robert Phillipse2f7d182016-12-15 09:23:05 -050055 GrTexture* rectTexture, uint32_t expectedPixelValues[]) {
56 GrSurfaceDesc copyDstDesc;
57 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
58 copyDstDesc.fWidth = rectTexture->width();
59 copyDstDesc.fHeight = rectTexture->height();
60
Brian Salomon8b1fb742016-11-03 15:21:06 -040061 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
Brian Salomon8b1fb742016-11-03 15:21:06 -040062 copyDstDesc.fFlags = flags;
Robert Phillipse2f7d182016-12-15 09:23:05 -050063
64 sk_sp<GrSurfaceProxy> dst(GrSurfaceProxy::TestCopy(context, copyDstDesc,
65 rectTexture, SkBudgeted::kYes));
66
67 GrSurface* dstSurf = dst->instantiate(context->textureProvider());
68
69 test_read_pixels(reporter, context, dstSurf, expectedPixelValues);
Brian Salomon8b1fb742016-11-03 15:21:06 -040070 }
bsalomone5286e02016-01-14 09:24:09 -080071}
72
73static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
74 GrTexture* rectangleTexture) {
Robert Phillipse2f7d182016-12-15 09:23:05 -050075
76 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(
77 sk_ref_sp(rectangleTexture)));
78
bsalomone5286e02016-01-14 09:24:09 -080079 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
80 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
81 for (int y = 0; y < rectangleTexture->width(); ++y) {
82 for (int x = 0; x < rectangleTexture->height(); ++x) {
83 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
84 }
85 }
Brian Salomon8b1fb742016-11-03 15:21:06 -040086 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
87 GrSurfaceDesc copySrcDesc;
88 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
89 copySrcDesc.fWidth = rectangleTexture->width();
90 copySrcDesc.fHeight = rectangleTexture->height();
91 copySrcDesc.fFlags = flags;
bsalomone5286e02016-01-14 09:24:09 -080092
Robert Phillipse2f7d182016-12-15 09:23:05 -050093 sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(),
94 context->textureProvider(),
95 copySrcDesc,
96 SkBudgeted::kYes, pixels.get(), 0));
97 sContext->copy(src.get());
98
Brian Salomon8b1fb742016-11-03 15:21:06 -040099 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
100 }
bsalomone5286e02016-01-14 09:24:09 -0800101}
102
Brian Salomon739c5bf2016-11-07 09:53:44 -0500103// skbug.com/5932
104static void test_basic_draw(skiatest::Reporter* reporter, GrContext* context,
105 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
106 sk_sp<GrRenderTargetContext> rtContext(
107 context->makeRenderTargetContext(SkBackingFit::kExact, rectangleTexture->width(),
108 rectangleTexture->height(), rectangleTexture->config(),
109 nullptr));
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000110 SkMatrix m;
111 m.setIDiv(rectangleTexture->width(), rectangleTexture->height());
Brian Salomon514baff2016-11-17 15:17:07 -0500112 for (auto filter : {GrSamplerParams::kNone_FilterMode,
113 GrSamplerParams::kBilerp_FilterMode,
114 GrSamplerParams::kMipMap_FilterMode}) {
Brian Salomon739c5bf2016-11-07 09:53:44 -0500115 rtContext->clear(nullptr, 0xDDCCBBAA, true);
Joe Gregorioa7d61a62017-01-17 19:20:54 +0000116 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(rectangleTexture,
117 nullptr, m, filter));
Brian Salomon739c5bf2016-11-07 09:53:44 -0500118 GrPaint paint;
119 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
120 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -0500121 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomon739c5bf2016-11-07 09:53:44 -0500122 test_read_pixels(reporter, context, rtContext->asTexture().get(), expectedPixelValues);
123 }
124}
125
bsalomone5286e02016-01-14 09:24:09 -0800126static void test_clear(skiatest::Reporter* reporter, GrContext* context,
127 GrTexture* rectangleTexture) {
128 if (rectangleTexture->asRenderTarget()) {
Brian Osman11052242016-10-27 14:47:55 -0400129 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700130 sk_ref_sp(rectangleTexture->asRenderTarget()),
131 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400132 if (!rtc) {
133 ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture.");
bsalomone5286e02016-01-14 09:24:09 -0800134 return;
135 }
136
137 // Clear the whole thing.
138 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -0400139 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -0800140
141 int w = rectangleTexture->width();
142 int h = rectangleTexture->height();
143 int pixelCnt = w * h;
144 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
145
146 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800147 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800148 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
149 expectedBytes0[0] = GrColorUnpackR(color0);
150 expectedBytes0[1] = GrColorUnpackG(color0);
151 expectedBytes0[2] = GrColorUnpackB(color0);
152 expectedBytes0[3] = GrColorUnpackA(color0);
153 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
154 expectedPixels.get()[i] = expectedColor0;
155 }
156
157 // Clear the the top to a different color.
158 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
159 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -0400160 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -0800161
benjaminwagner2a641ee2016-01-15 06:21:18 -0800162 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800163 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
164 expectedBytes1[0] = GrColorUnpackR(color1);
165 expectedBytes1[1] = GrColorUnpackG(color1);
166 expectedBytes1[2] = GrColorUnpackB(color1);
167 expectedBytes1[3] = GrColorUnpackA(color1);
168
169 for (int y = 0; y < h/2; ++y) {
170 for (int x = 0; x < w; ++x) {
171 expectedPixels.get()[y * h + x] = expectedColor1;
172 }
173 }
174
175 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
176 }
177}
178
bsalomon758586c2016-04-06 14:02:39 -0700179DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700180 GrContext* context = ctxInfo.grContext();
181 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800182 static const int kWidth = 13;
183 static const int kHeight = 13;
184
185 GrColor pixels[kWidth * kHeight];
186 for (int y = 0; y < kHeight; ++y) {
187 for (int x = 0; x < kWidth; ++x) {
188 pixels[y * kWidth + x] = y * kWidth + x;
189 }
190 }
191
192 for (int origin = 0; origin < 2; ++origin) {
193 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
194 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
195 pixels);
196
197 if (!rectTexID) {
198 return;
199 }
200
201 // Let GrContext know that we messed with the GL context directly.
202 context->resetContext();
203
204 // Wrap the rectangle texture ID in a GrTexture
205 GrGLTextureInfo rectangleInfo;
206 rectangleInfo.fID = rectTexID;
207 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
208
209 GrBackendTextureDesc rectangleDesc;
210 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
211 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
212 rectangleDesc.fWidth = kWidth;
213 rectangleDesc.fHeight = kHeight;
214 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
215 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
216
217 GrColor refPixels[kWidth * kHeight];
218 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
219 for (int y = 0; y < kHeight; ++y) {
220 for (int x = 0; x < kWidth; ++x) {
221 int y0 = flipRef ? kHeight - y - 1 : y;
222 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
223 }
224 }
225
bungeman6bd52842016-10-27 09:30:08 -0700226 sk_sp<GrTexture> rectangleTexture(
bsalomone5286e02016-01-14 09:24:09 -0800227 context->textureProvider()->wrapBackendTexture(rectangleDesc));
228 if (!rectangleTexture) {
229 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
230 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
231 continue;
232 }
233
bungeman6bd52842016-10-27 09:30:08 -0700234 test_read_pixels(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800235
Brian Salomon739c5bf2016-11-07 09:53:44 -0500236 test_basic_draw(reporter, context, rectangleTexture.get(), refPixels);
237
bungeman6bd52842016-10-27 09:30:08 -0700238 test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800239
bungeman6bd52842016-10-27 09:30:08 -0700240 test_copy_surface_dst(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800241
bungeman6bd52842016-10-27 09:30:08 -0700242 test_write_pixels(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800243
bungeman6bd52842016-10-27 09:30:08 -0700244 test_clear(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800245
246 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
247 }
248}
249
250#endif