| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 | 9 |
| 10 // This test is specific to the GPU backend. | 10 // This test is specific to the GPU backend. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) { | 37 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, ctxInfo) { |
| 38 unsigned char alphaData[X_SIZE * Y_SIZE]; | 38 unsigned char alphaData[X_SIZE * Y_SIZE]; |
| 39 | 39 |
| 40 static const int kClearValue = 0x2; | 40 static const int kClearValue = 0x2; |
| 41 | 41 |
| 42 bool match; | 42 bool match; |
| 43 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1}; | 43 static const size_t kRowBytes[] = {0, X_SIZE, X_SIZE + 1, 2 * X_SIZE - 1}; |
| 44 for (int rt = 0; rt < 2; ++rt) { | 44 { |
| 45 GrSurfaceDesc desc; | 45 GrSurfaceDesc desc; |
| 46 // let Skia know we will be using this texture as a render target | 46 desc.fFlags = kNone_GrSurfaceFlags; |
| 47 desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlag
s; | 47 desc.fConfig = kAlpha_8_GrPixelConfig; // it is a single channel t
exture |
| 48 // it is a single channel texture | |
| 49 desc.fConfig = kAlpha_8_GrPixelConfig; | |
| 50 desc.fWidth = X_SIZE; | 48 desc.fWidth = X_SIZE; |
| 51 desc.fHeight = Y_SIZE; | 49 desc.fHeight = Y_SIZE; |
| 52 | 50 |
| 53 // We are initializing the texture with zeros here | 51 // We are initializing the texture with zeros here |
| 54 memset(alphaData, 0, X_SIZE * Y_SIZE); | 52 memset(alphaData, 0, X_SIZE * Y_SIZE); |
| 55 SkAutoTUnref<GrTexture> texture( | 53 SkAutoTUnref<GrTexture> texture( |
| 56 ctxInfo.grContext()->textureProvider()->createTexture(desc, SkBudget
ed::kNo, alphaData, | 54 ctxInfo.grContext()->textureProvider()->createTexture(desc, SkBudget
ed::kNo, alphaData, |
| 57 0)); | 55 0)); |
| 58 if (!texture) { | 56 if (!texture) { |
| 59 if (!rt) { | 57 ERRORF(reporter, "Could not create alpha texture."); |
| 60 ERRORF(reporter, "Could not create alpha texture."); | 58 return; |
| 61 } | |
| 62 continue; | |
| 63 } | 59 } |
| 64 | 60 |
| 61 const SkImageInfo ii = SkImageInfo::MakeA8(X_SIZE, Y_SIZE); |
| 62 sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(), |
| 63 SkBudgeted::kNo, ii)); |
| 64 |
| 65 // create a distinctive texture | 65 // create a distinctive texture |
| 66 for (int y = 0; y < Y_SIZE; ++y) { | 66 for (int y = 0; y < Y_SIZE; ++y) { |
| 67 for (int x = 0; x < X_SIZE; ++x) { | 67 for (int x = 0; x < X_SIZE; ++x) { |
| 68 alphaData[y * X_SIZE + x] = y*X_SIZE+x; | 68 alphaData[y * X_SIZE + x] = y*X_SIZE+x; |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 for (auto rowBytes : kRowBytes) { | 72 for (auto rowBytes : kRowBytes) { |
| 73 // upload the texture (do per-rowbytes iteration because we may over
write below). | 73 // upload the texture (do per-rowbytes iteration because we may over
write below). |
| 74 bool result = texture->writePixels(0, 0, desc.fWidth, desc.fHeight,
desc.fConfig, | 74 bool result = texture->writePixels(0, 0, desc.fWidth, desc.fHeight,
desc.fConfig, |
| 75 alphaData, 0); | 75 alphaData, 0); |
| 76 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels fa
iled"); | 76 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 writePixels fa
iled"); |
| 77 | 77 |
| 78 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; | 78 size_t nonZeroRowBytes = rowBytes ? rowBytes : X_SIZE; |
| 79 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y
_SIZE]); | 79 SkAutoTDeleteArray<uint8_t> readback(new uint8_t[nonZeroRowBytes * Y
_SIZE]); |
| 80 // clear readback to something non-zero so we can detect readback fa
ilures | 80 // clear readback to something non-zero so we can detect readback fa
ilures |
| 81 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); | 81 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); |
| 82 | 82 |
| 83 // read the texture back | 83 // read the texture back |
| 84 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.f
Config, | 84 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.f
Config, |
| 85 readback.get(), rowBytes); | 85 readback.get(), rowBytes); |
| 86 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels fai
led"); | 86 REPORTER_ASSERT_MESSAGE(reporter, result, "Initial A8 readPixels fai
led"); |
| 87 | 87 |
| 88 // make sure the original & read back versions match | 88 // make sure the original & read back versions match |
| 89 SkString msg; | 89 SkString msg; |
| 90 msg.printf("rt:%d, rb:%d A8", rt, SkToU32(rowBytes)); | 90 msg.printf("rb:%d A8", SkToU32(rowBytes)); |
| 91 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZer
oRowBytes, | 91 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), nonZer
oRowBytes, |
| 92 alphaData, msg); | 92 alphaData, msg); |
| 93 | 93 |
| 94 // Now try writing on the single channel texture (if we could create
as a RT). | 94 // Now try writing to a single channel surface (if we could create o
ne). |
| 95 if (texture->asRenderTarget()) { | 95 if (surf) { |
| 96 sk_sp<SkSurface> surf(SkSurface::MakeRenderTargetDirect(texture-
>asRenderTarget(), | |
| 97 nullptr)
); | |
| 98 SkCanvas* canvas = surf->getCanvas(); | 96 SkCanvas* canvas = surf->getCanvas(); |
| 99 | 97 |
| 100 SkPaint paint; | 98 SkPaint paint; |
| 101 | 99 |
| 102 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SI
ZE + 10); | 100 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SI
ZE + 10); |
| 103 | 101 |
| 104 paint.setColor(SK_ColorWHITE); | 102 paint.setColor(SK_ColorWHITE); |
| 105 | 103 |
| 106 canvas->drawRect(rect, paint); | 104 canvas->drawRect(rect, paint); |
| 107 | 105 |
| 108 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); | 106 memset(readback.get(), kClearValue, nonZeroRowBytes * Y_SIZE); |
| 109 result = texture->readPixels(0, 0, desc.fWidth, desc.fHeight, | 107 result = surf->readPixels(ii, readback.get(), nonZeroRowBytes, 0
, 0); |
| 110 desc.fConfig, readback.get(), rowBy
tes); | |
| 111 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after c
lear failed"); | 108 REPORTER_ASSERT_MESSAGE(reporter, result, "A8 readPixels after c
lear failed"); |
| 112 | 109 |
| 113 match = true; | 110 match = true; |
| 114 for (int y = 0; y < Y_SIZE && match; ++y) { | 111 for (int y = 0; y < Y_SIZE && match; ++y) { |
| 115 for (int x = 0; x < X_SIZE && match; ++x) { | 112 for (int x = 0; x < X_SIZE && match; ++x) { |
| 116 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x
]; | 113 uint8_t rbValue = readback.get()[y * nonZeroRowBytes + x
]; |
| 117 if (0xFF != rbValue) { | 114 if (0xFF != rbValue) { |
| 118 ERRORF(reporter, | 115 ERRORF(reporter, |
| 119 "Failed alpha readback after clear. Expected:
0xFF, Got: 0x%02x" | 116 "Failed alpha readback after clear. Expected:
0xFF, Got: 0x%02x" |
| 120 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(
rowBytes)); | 117 " at (%d,%d), rb:%d", rbValue, x, y, SkToU32(
rowBytes)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 SkString msg; | 180 SkString msg; |
| 184 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes)); | 181 msg.printf("rt:%d, rb:%d 8888", rt, SkToU32(rowBytes)); |
| 185 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), no
nZeroRowBytes, | 182 validate_alpha_data(reporter, X_SIZE, Y_SIZE, readback.get(), no
nZeroRowBytes, |
| 186 alphaData, msg); | 183 alphaData, msg); |
| 187 } | 184 } |
| 188 } | 185 } |
| 189 } | 186 } |
| 190 } | 187 } |
| 191 | 188 |
| 192 #endif | 189 #endif |
| OLD | NEW |