blob: 6d075df203d2a8736419d5b8686a5bfe972426ee [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));
Brian Salomon514baff2016-11-17 15:17:07 -0500110 for (auto filter : {GrSamplerParams::kNone_FilterMode,
111 GrSamplerParams::kBilerp_FilterMode,
112 GrSamplerParams::kMipMap_FilterMode}) {
Brian Salomon739c5bf2016-11-07 09:53:44 -0500113 rtContext->clear(nullptr, 0xDDCCBBAA, true);
Robert Phillips67c18d62017-01-20 12:44:06 -0500114 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(rectangleTexture, nullptr,
115 SkMatrix::I(), filter));
Brian Salomon739c5bf2016-11-07 09:53:44 -0500116 GrPaint paint;
117 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
118 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -0500119 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomon739c5bf2016-11-07 09:53:44 -0500120 test_read_pixels(reporter, context, rtContext->asTexture().get(), expectedPixelValues);
121 }
122}
123
bsalomone5286e02016-01-14 09:24:09 -0800124static void test_clear(skiatest::Reporter* reporter, GrContext* context,
125 GrTexture* rectangleTexture) {
126 if (rectangleTexture->asRenderTarget()) {
Brian Osman11052242016-10-27 14:47:55 -0400127 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700128 sk_ref_sp(rectangleTexture->asRenderTarget()),
129 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400130 if (!rtc) {
131 ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture.");
bsalomone5286e02016-01-14 09:24:09 -0800132 return;
133 }
134
135 // Clear the whole thing.
136 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -0400137 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -0800138
139 int w = rectangleTexture->width();
140 int h = rectangleTexture->height();
141 int pixelCnt = w * h;
142 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
143
144 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800145 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800146 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
147 expectedBytes0[0] = GrColorUnpackR(color0);
148 expectedBytes0[1] = GrColorUnpackG(color0);
149 expectedBytes0[2] = GrColorUnpackB(color0);
150 expectedBytes0[3] = GrColorUnpackA(color0);
151 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
152 expectedPixels.get()[i] = expectedColor0;
153 }
154
155 // Clear the the top to a different color.
156 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
157 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -0400158 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -0800159
benjaminwagner2a641ee2016-01-15 06:21:18 -0800160 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800161 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
162 expectedBytes1[0] = GrColorUnpackR(color1);
163 expectedBytes1[1] = GrColorUnpackG(color1);
164 expectedBytes1[2] = GrColorUnpackB(color1);
165 expectedBytes1[3] = GrColorUnpackA(color1);
166
167 for (int y = 0; y < h/2; ++y) {
168 for (int x = 0; x < w; ++x) {
169 expectedPixels.get()[y * h + x] = expectedColor1;
170 }
171 }
172
173 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
174 }
175}
176
bsalomon758586c2016-04-06 14:02:39 -0700177DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700178 GrContext* context = ctxInfo.grContext();
179 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800180 static const int kWidth = 13;
181 static const int kHeight = 13;
182
183 GrColor pixels[kWidth * kHeight];
184 for (int y = 0; y < kHeight; ++y) {
185 for (int x = 0; x < kWidth; ++x) {
186 pixels[y * kWidth + x] = y * kWidth + x;
187 }
188 }
189
190 for (int origin = 0; origin < 2; ++origin) {
191 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
192 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
193 pixels);
194
195 if (!rectTexID) {
196 return;
197 }
198
199 // Let GrContext know that we messed with the GL context directly.
200 context->resetContext();
201
202 // Wrap the rectangle texture ID in a GrTexture
203 GrGLTextureInfo rectangleInfo;
204 rectangleInfo.fID = rectTexID;
205 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
206
207 GrBackendTextureDesc rectangleDesc;
208 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
209 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
210 rectangleDesc.fWidth = kWidth;
211 rectangleDesc.fHeight = kHeight;
212 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
213 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
214
215 GrColor refPixels[kWidth * kHeight];
216 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
217 for (int y = 0; y < kHeight; ++y) {
218 for (int x = 0; x < kWidth; ++x) {
219 int y0 = flipRef ? kHeight - y - 1 : y;
220 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
221 }
222 }
223
bungeman6bd52842016-10-27 09:30:08 -0700224 sk_sp<GrTexture> rectangleTexture(
bsalomone5286e02016-01-14 09:24:09 -0800225 context->textureProvider()->wrapBackendTexture(rectangleDesc));
226 if (!rectangleTexture) {
227 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
228 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
229 continue;
230 }
231
bungeman6bd52842016-10-27 09:30:08 -0700232 test_read_pixels(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800233
Brian Salomon739c5bf2016-11-07 09:53:44 -0500234 test_basic_draw(reporter, context, rectangleTexture.get(), refPixels);
235
bungeman6bd52842016-10-27 09:30:08 -0700236 test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800237
bungeman6bd52842016-10-27 09:30:08 -0700238 test_copy_surface_dst(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800239
bungeman6bd52842016-10-27 09:30:08 -0700240 test_write_pixels(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800241
bungeman6bd52842016-10-27 09:30:08 -0700242 test_clear(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800243
244 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
245 }
246}
247
248#endif