bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 13 | #include "gl/GrGLGpu.h" |
| 14 | #include "gl/GrGLUtil.h" |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 15 | #include "gl/GLTestContext.h" |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 16 | |
| 17 | static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context, |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 18 | GrTexture* texture, uint32_t expectedPixelValues[]) { |
| 19 | int pixelCnt = texture->width() * texture->height(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 20 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 21 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 22 | bool read = texture->readPixels(0, 0, texture->width(), texture->height(), |
| 23 | kRGBA_8888_GrPixelConfig, pixels.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 24 | 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 Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 29 | ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i, |
| 30 | expectedPixelValues[i], pixels.get()[i]); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 31 | break; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static 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 | |
| 54 | static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context, |
| 55 | GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) { |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 56 | 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 Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 62 | 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 66 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 78 | 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 Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 84 | sk_sp<GrTexture> src(context->textureProvider()->createTexture( |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 85 | copySrcDesc, SkBudgeted::kYes, pixels.get(), 0)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 86 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 87 | context->copySurface(rectangleTexture, src.get()); |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 88 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 89 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 92 | // skbug.com/5932 |
| 93 | static 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()); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame^] | 101 | for (auto filter : {GrSamplerParams::kNone_FilterMode, |
| 102 | GrSamplerParams::kBilerp_FilterMode, |
| 103 | GrSamplerParams::kMipMap_FilterMode}) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 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 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 115 | static void test_clear(skiatest::Reporter* reporter, GrContext* context, |
| 116 | GrTexture* rectangleTexture) { |
| 117 | if (rectangleTexture->asRenderTarget()) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 118 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext( |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 119 | sk_ref_sp(rectangleTexture->asRenderTarget()), |
| 120 | nullptr)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 121 | if (!rtc) { |
| 122 | ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture."); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Clear the whole thing. |
| 127 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 128 | rtc->clear(nullptr, color0, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 129 | |
| 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. |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 136 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 137 | 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 Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 149 | rtc->clear(&rect, color1, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 150 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 151 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 152 | 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 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 168 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 169 | GrContext* context = ctxInfo.grContext(); |
| 170 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 171 | 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 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 215 | sk_sp<GrTexture> rectangleTexture( |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 216 | 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 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 223 | test_read_pixels(reporter, context, rectangleTexture.get(), refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 224 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 225 | test_basic_draw(reporter, context, rectangleTexture.get(), refPixels); |
| 226 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 227 | test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 228 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 229 | test_copy_surface_dst(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 230 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 231 | test_write_pixels(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 232 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 233 | test_clear(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 234 | |
| 235 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | #endif |