| 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. | 
| 11 #if SK_SUPPORT_GPU | 11 #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID) | 
| 12 | 12 | 
| 13 #include "GrContext.h" | 13 #include "GrContext.h" | 
| 14 #include "SkGpuDevice.h" | 14 #include "SkGpuDevice.h" | 
| 15 | 15 | 
| 16 // This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT pr
     operly. | 16 // This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT pr
     operly. | 
| 17 static const int X_SIZE = 13; | 17 static const int X_SIZE = 13; | 
| 18 static const int Y_SIZE = 13; | 18 static const int Y_SIZE = 13; | 
| 19 | 19 | 
| 20 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) { | 20 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) { | 
| 21     unsigned char alphaData[X_SIZE][Y_SIZE]; | 21     unsigned char textureData[X_SIZE][Y_SIZE]; | 
| 22 | 22 | 
| 23     memset(alphaData, 0, X_SIZE * Y_SIZE); | 23     memset(textureData, 0, X_SIZE * Y_SIZE); | 
| 24 | 24 | 
| 25     bool match; | 25     GrSurfaceDesc desc; | 
|  | 26 | 
|  | 27     // let Skia know we will be using this texture as a render target | 
|  | 28     desc.fFlags     = kRenderTarget_GrSurfaceFlag; | 
|  | 29     // it is a single channel texture | 
|  | 30     desc.fConfig    = kAlpha_8_GrPixelConfig; | 
|  | 31     desc.fWidth     = X_SIZE; | 
|  | 32     desc.fHeight    = Y_SIZE; | 
|  | 33 | 
|  | 34     // We are initializing the texture with zeros here | 
|  | 35     GrTexture* texture = context->textureProvider()->createTexture(desc, false, 
     textureData, 0); | 
|  | 36     if (!texture) { | 
|  | 37         return; | 
|  | 38     } | 
|  | 39 | 
|  | 40     SkAutoTUnref<GrTexture> au(texture); | 
|  | 41 | 
|  | 42     // create a distinctive texture | 
|  | 43     for (int y = 0; y < Y_SIZE; ++y) { | 
|  | 44         for (int x = 0; x < X_SIZE; ++x) { | 
|  | 45             textureData[x][y] = x*Y_SIZE+y; | 
|  | 46         } | 
|  | 47     } | 
|  | 48 | 
|  | 49     // upload the texture | 
|  | 50     texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 
|  | 51                          textureData, 0); | 
|  | 52 | 
| 26     unsigned char readback[X_SIZE][Y_SIZE]; | 53     unsigned char readback[X_SIZE][Y_SIZE]; | 
| 27 | 54 | 
| 28     for (int rt = 0; rt < 2; ++rt) { | 55     // clear readback to something non-zero so we can detect readback failures | 
| 29         GrSurfaceDesc desc; | 56     memset(readback, 0x1, X_SIZE * Y_SIZE); | 
| 30         // let Skia know we will be using this texture as a render target |  | 
| 31         desc.fFlags     = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlag
     s; |  | 
| 32         // it is a single channel texture |  | 
| 33         desc.fConfig    = kAlpha_8_GrPixelConfig; |  | 
| 34         desc.fWidth     = X_SIZE; |  | 
| 35         desc.fHeight    = Y_SIZE; |  | 
| 36 | 57 | 
| 37         // We are initializing the texture with zeros here | 58     // read the texture back | 
| 38         SkAutoTUnref<GrTexture> texture( | 59     texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 
| 39             context->textureProvider()->createTexture(desc, false, alphaData, 0)
     ); | 60                         readback, 0); | 
| 40         if (!texture) { |  | 
| 41             if (!rt) { |  | 
| 42                 ERRORF(reporter, "Could not create alpha texture."); |  | 
| 43             } |  | 
| 44             continue; |  | 
| 45         } |  | 
| 46 | 61 | 
| 47         // create a distinctive texture | 62     // make sure the original & read back versions match | 
| 48         for (int y = 0; y < Y_SIZE; ++y) { | 63     bool match = true; | 
| 49             for (int x = 0; x < X_SIZE; ++x) { |  | 
| 50                 alphaData[x][y] = x*Y_SIZE+y; |  | 
| 51             } |  | 
| 52         } |  | 
| 53 | 64 | 
| 54         // upload the texture | 65     for (int y = 0; y < Y_SIZE; ++y) { | 
| 55         texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 66         for (int x = 0; x < X_SIZE; ++x) { | 
| 56                              alphaData, 0); | 67             if (textureData[x][y] != readback[x][y]) { | 
| 57 | 68                 match = false; | 
| 58         // clear readback to something non-zero so we can detect readback failur
     es |  | 
| 59         memset(readback, 0x1, X_SIZE * Y_SIZE); |  | 
| 60 |  | 
| 61         // read the texture back |  | 
| 62         texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |  | 
| 63                             readback, 0); |  | 
| 64 |  | 
| 65         // make sure the original & read back versions match |  | 
| 66         match = true; |  | 
| 67 |  | 
| 68         for (int y = 0; y < Y_SIZE && match; ++y) { |  | 
| 69             for (int x = 0; x < X_SIZE && match; ++x) { |  | 
| 70                 if (alphaData[x][y] != readback[x][y]) { |  | 
| 71                     SkDebugf("Failed alpha readback. Expected: 0x%02x, " |  | 
| 72                              "Got: 0x%02x at (%d,%d), rt: %d", alphaData[x][y], 
     readback[x][y], x, |  | 
| 73                              y, rt); |  | 
| 74                     match = false; |  | 
| 75                 } |  | 
| 76             } |  | 
| 77         } |  | 
| 78 |  | 
| 79         // Now try writing on the single channel texture (if we could create as 
     a RT). |  | 
| 80         if (texture->asRenderTarget()) { |  | 
| 81             SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |  | 
| 82             SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create( |  | 
| 83                 texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitCont
     ents)); |  | 
| 84             SkCanvas canvas(device); |  | 
| 85 |  | 
| 86             SkPaint paint; |  | 
| 87 |  | 
| 88             const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE +
      10); |  | 
| 89 |  | 
| 90             paint.setColor(SK_ColorWHITE); |  | 
| 91 |  | 
| 92             canvas.drawRect(rect, paint); |  | 
| 93 |  | 
| 94             texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, r
     eadback, 0); |  | 
| 95 |  | 
| 96             match = true; |  | 
| 97 |  | 
| 98             for (int y = 0; y < Y_SIZE && match; ++y) { |  | 
| 99                 for (int x = 0; x < X_SIZE && match; ++x) { |  | 
| 100                     if (0xFF != readback[x][y]) { |  | 
| 101                         ERRORF(reporter, |  | 
| 102                                "Failed alpha readback after clear. Expected: 0xF
     F, Got: 0x%02x at " |  | 
| 103                                "(%d,%d)", readback[x][y], x, y); |  | 
| 104                         match = false; |  | 
| 105                     } |  | 
| 106                 } |  | 
| 107             } | 69             } | 
| 108         } | 70         } | 
| 109     } | 71     } | 
| 110 | 72 | 
| 111     // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a tex
     ture-only src and | 73     REPORTER_ASSERT(reporter, match); | 
| 112     // once with a render target. |  | 
| 113     for (int cfg = 0; cfg < 2; ++cfg) { |  | 
| 114         for (int rt = 0; rt < 2; ++rt) { |  | 
| 115             GrSurfaceDesc desc; |  | 
| 116             desc.fFlags     = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurface
     Flags; |  | 
| 117             desc.fConfig    = cfg ? kBGRA_8888_GrPixelConfig : kRGBA_8888_GrPixe
     lConfig; |  | 
| 118             desc.fWidth     = X_SIZE; |  | 
| 119             desc.fHeight    = Y_SIZE; |  | 
| 120 | 74 | 
| 121             uint32_t rgbaData[X_SIZE][Y_SIZE]; | 75     // Now try writing on the single channel texture | 
| 122             // Make the alpha channel of the rgba texture come from alphaData. | 76     SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | 
| 123             for (int y = 0; y < Y_SIZE; ++y) { | 77     SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarge
     t(), &props, | 
| 124                 for (int x = 0; x < X_SIZE; ++x) { | 78                                                           SkGpuDevice::kUninit_I
     nitContents)); | 
| 125                     rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]); | 79     SkCanvas canvas(device); | 
| 126                 } |  | 
| 127             } |  | 
| 128             SkAutoTUnref<GrTexture> texture( |  | 
| 129                 context->textureProvider()->createTexture(desc, false, rgbaData,
      0)); |  | 
| 130             if (!texture) { |  | 
| 131                 // We always expect to be able to create a RGBA texture |  | 
| 132                 if (!rt  && kRGBA_8888_GrPixelConfig == desc.fConfig) { |  | 
| 133                     ERRORF(reporter, "Failed to create RGBA texture."); |  | 
| 134                 } |  | 
| 135                 continue; |  | 
| 136             } |  | 
| 137 | 80 | 
| 138             // clear readback to something non-zero so we can detect readback fa
     ilures | 81     SkPaint paint; | 
| 139             memset(readback, 0x0, X_SIZE * Y_SIZE); |  | 
| 140 | 82 | 
| 141             // read the texture back | 83     const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10); | 
| 142             texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixe
     lConfig, readback, |  | 
| 143                                 0); |  | 
| 144 | 84 | 
| 145             match = true; | 85     paint.setColor(SK_ColorWHITE); | 
| 146 | 86 | 
| 147             for (int y = 0; y < Y_SIZE && match; ++y) { | 87     canvas.drawRect(rect, paint); | 
| 148                 for (int x = 0; x < X_SIZE && match; ++x) { | 88 | 
| 149                     if (alphaData[x][y] != readback[x][y]) { | 89     texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, | 
| 150                         texture->readPixels(0, 0, desc.fWidth, desc.fHeight, | 90                         readback, 0); | 
| 151                                             kAlpha_8_GrPixelConfig, readback, 0)
     ; | 91 | 
| 152                         ERRORF(reporter, | 92     match = true; | 
| 153                                "Failed alpha readback from cfg %d. Expected: 0x%
     02x, Got: 0x%02x at " | 93 | 
| 154                                "(%d,%d), rt:%d", desc.fConfig, alphaData[x][y], 
     readback[x][y], x, | 94     for (int y = 0; y < Y_SIZE; ++y) { | 
| 155                                y, rt); | 95         for (int x = 0; x < X_SIZE; ++x) { | 
| 156                         match = false; | 96             if (0xFF != readback[x][y]) { | 
| 157                     } | 97                 match = false; | 
| 158                 } |  | 
| 159             } | 98             } | 
| 160         } | 99         } | 
| 161     } | 100     } | 
|  | 101 | 
|  | 102     REPORTER_ASSERT(reporter, match); | 
| 162 } | 103 } | 
| 163 | 104 | 
| 164 #endif | 105 #endif | 
| OLD | NEW | 
|---|