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, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 18 | GrSurface* texture, uint32_t expectedPixelValues[]) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 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, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 55 | 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 61 | for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) { |
Brian Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 62 | copyDstDesc.fFlags = flags; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 63 | |
| 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 70 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context, |
| 74 | GrTexture* rectangleTexture) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 75 | |
| 76 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext( |
| 77 | sk_ref_sp(rectangleTexture))); |
| 78 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 79 | 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 86 | 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; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 92 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 93 | 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 Salomon | 8b1fb74 | 2016-11-03 15:21:06 -0400 | [diff] [blame] | 99 | test_read_pixels(reporter, context, rectangleTexture, pixels.get()); |
| 100 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 103 | // skbug.com/5932 |
| 104 | static 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)); |
| 110 | SkMatrix m; |
| 111 | m.setIDiv(rectangleTexture->width(), rectangleTexture->height()); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 112 | for (auto filter : {GrSamplerParams::kNone_FilterMode, |
| 113 | GrSamplerParams::kBilerp_FilterMode, |
| 114 | GrSamplerParams::kMipMap_FilterMode}) { |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 115 | rtContext->clear(nullptr, 0xDDCCBBAA, true); |
| 116 | sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(rectangleTexture, |
| 117 | nullptr, m, filter)); |
| 118 | GrPaint paint; |
| 119 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 120 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame^] | 121 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 122 | test_read_pixels(reporter, context, rtContext->asTexture().get(), expectedPixelValues); |
| 123 | } |
| 124 | } |
| 125 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 126 | static void test_clear(skiatest::Reporter* reporter, GrContext* context, |
| 127 | GrTexture* rectangleTexture) { |
| 128 | if (rectangleTexture->asRenderTarget()) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 129 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext( |
robertphillips | 4fd74ae | 2016-08-03 14:26:53 -0700 | [diff] [blame] | 130 | sk_ref_sp(rectangleTexture->asRenderTarget()), |
| 131 | nullptr)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 132 | if (!rtc) { |
| 133 | ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture."); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Clear the whole thing. |
| 138 | GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 139 | rtc->clear(nullptr, color0, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 140 | |
| 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. |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 147 | uint32_t expectedColor0 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 148 | 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 Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 160 | rtc->clear(&rect, color1, false); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 161 | |
benjaminwagner | 2a641ee | 2016-01-15 06:21:18 -0800 | [diff] [blame] | 162 | uint32_t expectedColor1 = 0; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 163 | 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 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 179 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 180 | GrContext* context = ctxInfo.grContext(); |
| 181 | sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext(); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 182 | 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 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 226 | sk_sp<GrTexture> rectangleTexture( |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 227 | 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 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 234 | test_read_pixels(reporter, context, rectangleTexture.get(), refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 235 | |
Brian Salomon | 739c5bf | 2016-11-07 09:53:44 -0500 | [diff] [blame] | 236 | test_basic_draw(reporter, context, rectangleTexture.get(), refPixels); |
| 237 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 238 | test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 239 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 240 | test_copy_surface_dst(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 241 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 242 | test_write_pixels(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 243 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 244 | test_clear(reporter, context, rectangleTexture.get()); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 245 | |
| 246 | GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID)); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | #endif |