Home | History | Annotate | Download | only in tests
      1 
      2 /*
      3  * Copyright 2012 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 // This test is specific to the GPU backend.
     10 #if SK_SUPPORT_GPU
     11 
     12 #include "Test.h"
     13 #include "SkGpuDevice.h"
     14 
     15 static const int X_SIZE = 12;
     16 static const int Y_SIZE = 12;
     17 
     18 static void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContext* context) {
     19 
     20 #if SK_SCALAR_IS_FIXED
     21     // GPU device known not to work in the fixed pt build.
     22     return;
     23 #endif
     24 
     25     unsigned char textureData[X_SIZE][Y_SIZE];
     26 
     27     memset(textureData, 0, X_SIZE * Y_SIZE);
     28 
     29     GrTextureDesc desc;
     30 
     31     // let Skia know we will be using this texture as a render target
     32     desc.fFlags     = kRenderTarget_GrTextureFlagBit;
     33     // it is a single channel texture
     34     desc.fConfig    = kAlpha_8_GrPixelConfig;
     35     desc.fWidth     = X_SIZE;
     36     desc.fHeight    = Y_SIZE;
     37 
     38     // We are initializing the texture with zeros here
     39     GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
     40     if (!texture) {
     41         return;
     42     }
     43 
     44     GrAutoUnref au(texture);
     45 
     46     // create a distinctive texture
     47     for (int y = 0; y < Y_SIZE; ++y) {
     48         for (int x = 0; x < X_SIZE; ++x) {
     49             textureData[x][y] = x*Y_SIZE+y;
     50         }
     51     }
     52 
     53     // upload the texture
     54     texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     55                          textureData, 0);
     56 
     57     unsigned char readback[X_SIZE][Y_SIZE];
     58 
     59     // clear readback to something non-zero so we can detect readback failures
     60     memset(readback, 0x1, X_SIZE * Y_SIZE);
     61 
     62     // read the texture back
     63     texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     64                         readback, 0);
     65 
     66     // make sure the original & read back versions match
     67     bool match = true;
     68 
     69     for (int y = 0; y < Y_SIZE; ++y) {
     70         for (int x = 0; x < X_SIZE; ++x) {
     71             if (textureData[x][y] != readback[x][y]) {
     72                 match = false;
     73             }
     74         }
     75     }
     76 
     77     REPORTER_ASSERT(reporter, match);
     78 
     79     // Now try writing on the single channel texture
     80     SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, texture->asRenderTarget()));
     81     SkCanvas canvas(device);
     82 
     83     SkPaint paint;
     84 
     85     const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
     86 
     87     paint.setColor(SK_ColorWHITE);
     88 
     89     canvas.drawRect(rect, paint);
     90 
     91     texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     92                         readback, 0);
     93 
     94     match = true;
     95 
     96     for (int y = 0; y < Y_SIZE; ++y) {
     97         for (int x = 0; x < X_SIZE; ++x) {
     98             if (0xFF != readback[x][y]) {
     99                 match = false;
    100             }
    101         }
    102     }
    103 
    104     REPORTER_ASSERT(reporter, match);
    105 }
    106 
    107 #ifndef SK_BUILD_FOR_ANDROID
    108 #include "TestClassDef.h"
    109 DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
    110 
    111 #endif
    112 #endif
    113