blob: 523c2e343757f95b0c6f2679ba05f93b6562d6ea [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
Robert Phillips2b270e52017-01-27 14:28:29 +000017static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
18 GrSurfaceContext* srcContext, uint32_t expectedPixelValues[]) {
19 int pixelCnt = srcContext->width() * srcContext->height();
20 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
21 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
22
23 SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
24 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
25 bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0);
26 if (!read) {
27 ERRORF(reporter, "Error reading rectangle texture.");
28 }
29
30 for (int i = 0; i < pixelCnt; ++i) {
31 if (pixels.get()[i] != expectedPixelValues[i]) {
32 ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i,
33 expectedPixelValues[i], pixels.get()[i]);
34 break;
35 }
36 }
37}
38
39static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
40 GrSurfaceContext* rectSurfaceContext) {
41 int pixelCnt = rectSurfaceContext->width() * rectSurfaceContext->height();
42 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
43 for (int y = 0; y < rectSurfaceContext->width(); ++y) {
44 for (int x = 0; x < rectSurfaceContext->height(); ++x) {
45 pixels.get()[y * rectSurfaceContext->width() + x] = GrColorPackRGBA(x, y, x + y, x * y);
46 }
47 }
48
49 SkImageInfo ii = SkImageInfo::Make(rectSurfaceContext->width(), rectSurfaceContext->height(),
50 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
51 bool write = rectSurfaceContext->writePixels(ii, pixels.get(), 0, 0, 0);
52 if (!write) {
53 ERRORF(reporter, "Error writing to rectangle texture.");
54 }
55
56 test_read_pixels(reporter, context, rectSurfaceContext, pixels.get());
57}
58
59static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context,
60 GrSurfaceProxy* rectProxy, uint32_t expectedPixelValues[]) {
61 GrSurfaceDesc copyDstDesc;
62 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
63 copyDstDesc.fWidth = rectProxy->width();
64 copyDstDesc.fHeight = rectProxy->height();
65
66 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
67 copyDstDesc.fFlags = flags;
68
69 sk_sp<GrSurfaceContext> dstContext(GrSurfaceProxy::TestCopy(context, copyDstDesc,
70 rectProxy));
71
72 test_read_pixels(reporter, context, dstContext.get(), expectedPixelValues);
73 }
74}
75
76static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
77 GrSurfaceContext* rectContext) {
78
79 int pixelCnt = rectContext->width() * rectContext->height();
80 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
81 for (int y = 0; y < rectContext->width(); ++y) {
82 for (int x = 0; x < rectContext->height(); ++x) {
83 pixels.get()[y * rectContext->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
84 }
85 }
86 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
87 GrSurfaceDesc copySrcDesc;
88 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
89 copySrcDesc.fWidth = rectContext->width();
90 copySrcDesc.fHeight = rectContext->height();
91 copySrcDesc.fFlags = flags;
92
93 sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(*context->caps(),
94 context->textureProvider(),
95 copySrcDesc,
96 SkBudgeted::kYes, pixels.get(), 0));
97 rectContext->copy(src.get());
98
99 test_read_pixels(reporter, context, rectContext, pixels.get());
100 }
101}
102
Brian Salomon739c5bf2016-11-07 09:53:44 -0500103// skbug.com/5932
Robert Phillipsd46697a2017-01-25 12:10:37 -0500104static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrContext* context,
105 sk_sp<GrSurfaceProxy> rectProxy, uint32_t expectedPixelValues[]) {
Brian Salomon739c5bf2016-11-07 09:53:44 -0500106 sk_sp<GrRenderTargetContext> rtContext(
Robert Phillipsd46697a2017-01-25 12:10:37 -0500107 context->makeRenderTargetContext(SkBackingFit::kExact, rectProxy->width(),
108 rectProxy->height(), rectProxy->config(),
Brian Salomon739c5bf2016-11-07 09:53:44 -0500109 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 Phillipsd46697a2017-01-25 12:10:37 -0500114 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(
115 context,
116 sk_ref_sp(rectProxy->asTextureProxy()),
117 nullptr,
118 SkMatrix::I(), filter));
Brian Salomon739c5bf2016-11-07 09:53:44 -0500119 GrPaint paint;
120 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
121 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -0500122 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Robert Phillips2b270e52017-01-27 14:28:29 +0000123 test_read_pixels(reporter, context, rtContext.get(), expectedPixelValues);
Brian Salomon739c5bf2016-11-07 09:53:44 -0500124 }
125}
126
bsalomone5286e02016-01-14 09:24:09 -0800127static void test_clear(skiatest::Reporter* reporter, GrContext* context,
Robert Phillipsd46697a2017-01-25 12:10:37 -0500128 GrSurfaceContext* rectContext) {
129 if (GrRenderTargetContext* rtc = rectContext->asRenderTargetContext()) {
bsalomone5286e02016-01-14 09:24:09 -0800130 // Clear the whole thing.
131 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -0400132 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -0800133
Robert Phillipsd46697a2017-01-25 12:10:37 -0500134 int w = rtc->width();
135 int h = rtc->height();
bsalomone5286e02016-01-14 09:24:09 -0800136 int pixelCnt = w * h;
137 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
138
139 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800140 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800141 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
142 expectedBytes0[0] = GrColorUnpackR(color0);
143 expectedBytes0[1] = GrColorUnpackG(color0);
144 expectedBytes0[2] = GrColorUnpackB(color0);
145 expectedBytes0[3] = GrColorUnpackA(color0);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500146 for (int i = 0; i < rtc->width() * rtc->height(); ++i) {
bsalomone5286e02016-01-14 09:24:09 -0800147 expectedPixels.get()[i] = expectedColor0;
148 }
149
150 // Clear the the top to a different color.
151 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
152 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -0400153 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -0800154
benjaminwagner2a641ee2016-01-15 06:21:18 -0800155 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800156 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
157 expectedBytes1[0] = GrColorUnpackR(color1);
158 expectedBytes1[1] = GrColorUnpackG(color1);
159 expectedBytes1[2] = GrColorUnpackB(color1);
160 expectedBytes1[3] = GrColorUnpackA(color1);
161
162 for (int y = 0; y < h/2; ++y) {
163 for (int x = 0; x < w; ++x) {
164 expectedPixels.get()[y * h + x] = expectedColor1;
165 }
166 }
167
Robert Phillips2b270e52017-01-27 14:28:29 +0000168 test_read_pixels(reporter, context, rtc, expectedPixels.get());
bsalomone5286e02016-01-14 09:24:09 -0800169 }
170}
171
bsalomon758586c2016-04-06 14:02:39 -0700172DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700173 GrContext* context = ctxInfo.grContext();
174 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800175 static const int kWidth = 13;
176 static const int kHeight = 13;
177
178 GrColor pixels[kWidth * kHeight];
179 for (int y = 0; y < kHeight; ++y) {
180 for (int x = 0; x < kWidth; ++x) {
181 pixels[y * kWidth + x] = y * kWidth + x;
182 }
183 }
184
185 for (int origin = 0; origin < 2; ++origin) {
186 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
187 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
188 pixels);
189
190 if (!rectTexID) {
191 return;
192 }
193
194 // Let GrContext know that we messed with the GL context directly.
195 context->resetContext();
196
197 // Wrap the rectangle texture ID in a GrTexture
198 GrGLTextureInfo rectangleInfo;
199 rectangleInfo.fID = rectTexID;
200 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
201
202 GrBackendTextureDesc rectangleDesc;
203 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
204 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
205 rectangleDesc.fWidth = kWidth;
206 rectangleDesc.fHeight = kHeight;
207 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
208 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
209
210 GrColor refPixels[kWidth * kHeight];
211 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
212 for (int y = 0; y < kHeight; ++y) {
213 for (int x = 0; x < kWidth; ++x) {
214 int y0 = flipRef ? kHeight - y - 1 : y;
215 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
216 }
217 }
218
Robert Phillipsd46697a2017-01-25 12:10:37 -0500219 sk_sp<GrSurfaceProxy> rectProxy;
220
221 {
222 sk_sp<GrTexture> rectangleTexture(
223 context->textureProvider()->wrapBackendTexture(rectangleDesc));
224 if (!rectangleTexture) {
225 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
226 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
227 continue;
228 }
229
230 rectProxy = GrSurfaceProxy::MakeWrapped(std::move(rectangleTexture));
231 if (!rectProxy) {
232 ERRORF(reporter, "Error creating proxy for rectangle texture.");
233 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
234 continue;
235 }
bsalomone5286e02016-01-14 09:24:09 -0800236 }
237
Robert Phillipsd46697a2017-01-25 12:10:37 -0500238 test_basic_draw_as_src(reporter, context, rectProxy, refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800239
Robert Phillips2b270e52017-01-27 14:28:29 +0000240 test_copy_surface_src(reporter, context, rectProxy.get(), refPixels);
Brian Salomon739c5bf2016-11-07 09:53:44 -0500241
Robert Phillipsd46697a2017-01-25 12:10:37 -0500242 sk_sp<GrSurfaceContext> rectContext = context->contextPriv().makeWrappedSurfaceContext(
243 std::move(rectProxy), nullptr);
244 SkASSERT(rectContext);
bsalomone5286e02016-01-14 09:24:09 -0800245
Robert Phillips2b270e52017-01-27 14:28:29 +0000246 test_read_pixels(reporter, context, rectContext.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800247
Robert Phillips2b270e52017-01-27 14:28:29 +0000248 test_copy_surface_dst(reporter, context, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800249
Robert Phillips2b270e52017-01-27 14:28:29 +0000250 test_write_pixels(reporter, context, rectContext.get());
Robert Phillipsd46697a2017-01-25 12:10:37 -0500251
252 test_clear(reporter, context, rectContext.get());
bsalomone5286e02016-01-14 09:24:09 -0800253
254 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
255 }
256}
257
258#endif