blob: 21540d99c09453f0ed8ba88e7b6f6ee640a5386b [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,
Brian Salomon739c5bf2016-11-07 09:53:44 -050018 GrTexture* texture, uint32_t expectedPixelValues[]) {
19 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,
55 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
Brian Salomon8b1fb742016-11-03 15:21:06 -040056 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
57 GrSurfaceDesc copyDstDesc;
58 copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
59 copyDstDesc.fWidth = rectangleTexture->width();
60 copyDstDesc.fHeight = rectangleTexture->height();
61 copyDstDesc.fFlags = flags;
Hal Canary342b7ac2016-11-04 11:49:42 -040062 sk_sp<GrTexture> dst(
63 context->textureProvider()->createTexture(copyDstDesc, SkBudgeted::kYes));
64 context->copySurface(dst.get(), rectangleTexture);
65 test_read_pixels(reporter, context, dst.get(), expectedPixelValues);
Brian Salomon8b1fb742016-11-03 15:21:06 -040066 }
bsalomone5286e02016-01-14 09:24:09 -080067}
68
69static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
70 GrTexture* rectangleTexture) {
71 int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
72 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
73 for (int y = 0; y < rectangleTexture->width(); ++y) {
74 for (int x = 0; x < rectangleTexture->height(); ++x) {
75 pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
76 }
77 }
Brian Salomon8b1fb742016-11-03 15:21:06 -040078 for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
79 GrSurfaceDesc copySrcDesc;
80 copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
81 copySrcDesc.fWidth = rectangleTexture->width();
82 copySrcDesc.fHeight = rectangleTexture->height();
83 copySrcDesc.fFlags = flags;
Hal Canary342b7ac2016-11-04 11:49:42 -040084 sk_sp<GrTexture> src(context->textureProvider()->createTexture(
Brian Salomon8b1fb742016-11-03 15:21:06 -040085 copySrcDesc, SkBudgeted::kYes, pixels.get(), 0));
bsalomone5286e02016-01-14 09:24:09 -080086
Hal Canary342b7ac2016-11-04 11:49:42 -040087 context->copySurface(rectangleTexture, src.get());
Brian Salomon8b1fb742016-11-03 15:21:06 -040088 test_read_pixels(reporter, context, rectangleTexture, pixels.get());
89 }
bsalomone5286e02016-01-14 09:24:09 -080090}
91
Brian Salomon739c5bf2016-11-07 09:53:44 -050092// skbug.com/5932
93static void test_basic_draw(skiatest::Reporter* reporter, GrContext* context,
94 GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
95 sk_sp<GrRenderTargetContext> rtContext(
96 context->makeRenderTargetContext(SkBackingFit::kExact, rectangleTexture->width(),
97 rectangleTexture->height(), rectangleTexture->config(),
98 nullptr));
99 SkMatrix m;
100 m.setIDiv(rectangleTexture->width(), rectangleTexture->height());
101 for (auto filter : {GrTextureParams::kNone_FilterMode,
102 GrTextureParams::kBilerp_FilterMode,
103 GrTextureParams::kMipMap_FilterMode}) {
104 rtContext->clear(nullptr, 0xDDCCBBAA, true);
105 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(rectangleTexture,
106 nullptr, m, filter));
107 GrPaint paint;
108 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
109 paint.addColorFragmentProcessor(std::move(fp));
110 rtContext->drawPaint(GrNoClip(), paint, SkMatrix::I());
111 test_read_pixels(reporter, context, rtContext->asTexture().get(), expectedPixelValues);
112 }
113}
114
bsalomone5286e02016-01-14 09:24:09 -0800115static void test_clear(skiatest::Reporter* reporter, GrContext* context,
116 GrTexture* rectangleTexture) {
117 if (rectangleTexture->asRenderTarget()) {
Brian Osman11052242016-10-27 14:47:55 -0400118 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700119 sk_ref_sp(rectangleTexture->asRenderTarget()),
120 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400121 if (!rtc) {
122 ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture.");
bsalomone5286e02016-01-14 09:24:09 -0800123 return;
124 }
125
126 // Clear the whole thing.
127 GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
Brian Osman11052242016-10-27 14:47:55 -0400128 rtc->clear(nullptr, color0, false);
bsalomone5286e02016-01-14 09:24:09 -0800129
130 int w = rectangleTexture->width();
131 int h = rectangleTexture->height();
132 int pixelCnt = w * h;
133 SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
134
135 // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
benjaminwagner2a641ee2016-01-15 06:21:18 -0800136 uint32_t expectedColor0 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800137 uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
138 expectedBytes0[0] = GrColorUnpackR(color0);
139 expectedBytes0[1] = GrColorUnpackG(color0);
140 expectedBytes0[2] = GrColorUnpackB(color0);
141 expectedBytes0[3] = GrColorUnpackA(color0);
142 for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
143 expectedPixels.get()[i] = expectedColor0;
144 }
145
146 // Clear the the top to a different color.
147 GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
148 SkIRect rect = SkIRect::MakeWH(w, h/2);
Brian Osman11052242016-10-27 14:47:55 -0400149 rtc->clear(&rect, color1, false);
bsalomone5286e02016-01-14 09:24:09 -0800150
benjaminwagner2a641ee2016-01-15 06:21:18 -0800151 uint32_t expectedColor1 = 0;
bsalomone5286e02016-01-14 09:24:09 -0800152 uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
153 expectedBytes1[0] = GrColorUnpackR(color1);
154 expectedBytes1[1] = GrColorUnpackG(color1);
155 expectedBytes1[2] = GrColorUnpackB(color1);
156 expectedBytes1[3] = GrColorUnpackA(color1);
157
158 for (int y = 0; y < h/2; ++y) {
159 for (int x = 0; x < w; ++x) {
160 expectedPixels.get()[y * h + x] = expectedColor1;
161 }
162 }
163
164 test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
165 }
166}
167
bsalomon758586c2016-04-06 14:02:39 -0700168DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700169 GrContext* context = ctxInfo.grContext();
170 sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
bsalomone5286e02016-01-14 09:24:09 -0800171 static const int kWidth = 13;
172 static const int kHeight = 13;
173
174 GrColor pixels[kWidth * kHeight];
175 for (int y = 0; y < kHeight; ++y) {
176 for (int x = 0; x < kWidth; ++x) {
177 pixels[y * kWidth + x] = y * kWidth + x;
178 }
179 }
180
181 for (int origin = 0; origin < 2; ++origin) {
182 GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
183 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
184 pixels);
185
186 if (!rectTexID) {
187 return;
188 }
189
190 // Let GrContext know that we messed with the GL context directly.
191 context->resetContext();
192
193 // Wrap the rectangle texture ID in a GrTexture
194 GrGLTextureInfo rectangleInfo;
195 rectangleInfo.fID = rectTexID;
196 rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
197
198 GrBackendTextureDesc rectangleDesc;
199 rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
200 rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
201 rectangleDesc.fWidth = kWidth;
202 rectangleDesc.fHeight = kHeight;
203 rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
204 rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
205
206 GrColor refPixels[kWidth * kHeight];
207 bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
208 for (int y = 0; y < kHeight; ++y) {
209 for (int x = 0; x < kWidth; ++x) {
210 int y0 = flipRef ? kHeight - y - 1 : y;
211 refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
212 }
213 }
214
bungeman6bd52842016-10-27 09:30:08 -0700215 sk_sp<GrTexture> rectangleTexture(
bsalomone5286e02016-01-14 09:24:09 -0800216 context->textureProvider()->wrapBackendTexture(rectangleDesc));
217 if (!rectangleTexture) {
218 ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
219 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
220 continue;
221 }
222
bungeman6bd52842016-10-27 09:30:08 -0700223 test_read_pixels(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800224
Brian Salomon739c5bf2016-11-07 09:53:44 -0500225 test_basic_draw(reporter, context, rectangleTexture.get(), refPixels);
226
bungeman6bd52842016-10-27 09:30:08 -0700227 test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels);
bsalomone5286e02016-01-14 09:24:09 -0800228
bungeman6bd52842016-10-27 09:30:08 -0700229 test_copy_surface_dst(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800230
bungeman6bd52842016-10-27 09:30:08 -0700231 test_write_pixels(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800232
bungeman6bd52842016-10-27 09:30:08 -0700233 test_clear(reporter, context, rectangleTexture.get());
bsalomone5286e02016-01-14 09:24:09 -0800234
235 GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
236 }
237}
238
239#endif