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 && !defined(SK_BUILD_FOR_ANDROID)
     11 
     12 #include "Test.h"
     13 #include "SkGpuDevice.h"
     14 #include "GrContextFactory.h"
     15 
     16 static const int X_SIZE = 12;
     17 static const int Y_SIZE = 12;
     18 
     19 static void ReadWriteAlphaTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
     20     for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
     21         GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
     22         if (!GrContextFactory::IsRenderingGLContext(glType)) {
     23             continue;
     24         }
     25         GrContext* context = factory->get(glType);
     26         if (NULL == context) {
     27             continue;
     28         }
     29 
     30         unsigned char textureData[X_SIZE][Y_SIZE];
     31 
     32         memset(textureData, 0, X_SIZE * Y_SIZE);
     33 
     34         GrTextureDesc desc;
     35 
     36         // let Skia know we will be using this texture as a render target
     37         desc.fFlags     = kRenderTarget_GrTextureFlagBit;
     38         // it is a single channel texture
     39         desc.fConfig    = kAlpha_8_GrPixelConfig;
     40         desc.fWidth     = X_SIZE;
     41         desc.fHeight    = Y_SIZE;
     42 
     43         // We are initializing the texture with zeros here
     44         GrTexture* texture = context->createUncachedTexture(desc, textureData, 0);
     45         if (!texture) {
     46             return;
     47         }
     48 
     49         GrAutoUnref au(texture);
     50 
     51         // create a distinctive texture
     52         for (int y = 0; y < Y_SIZE; ++y) {
     53             for (int x = 0; x < X_SIZE; ++x) {
     54                 textureData[x][y] = x*Y_SIZE+y;
     55             }
     56         }
     57 
     58         // upload the texture
     59         texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     60                              textureData, 0);
     61 
     62         unsigned char readback[X_SIZE][Y_SIZE];
     63 
     64         // clear readback to something non-zero so we can detect readback failures
     65         memset(readback, 0x1, X_SIZE * Y_SIZE);
     66 
     67         // read the texture back
     68         texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     69                             readback, 0);
     70 
     71         // make sure the original & read back versions match
     72         bool match = true;
     73 
     74         for (int y = 0; y < Y_SIZE; ++y) {
     75             for (int x = 0; x < X_SIZE; ++x) {
     76                 if (textureData[x][y] != readback[x][y]) {
     77                     match = false;
     78                 }
     79             }
     80         }
     81 
     82         REPORTER_ASSERT(reporter, match);
     83 
     84         // Now try writing on the single channel texture
     85         SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, texture->asRenderTarget()));
     86         SkCanvas canvas(device);
     87 
     88         SkPaint paint;
     89 
     90         const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
     91 
     92         paint.setColor(SK_ColorWHITE);
     93 
     94         canvas.drawRect(rect, paint);
     95 
     96         texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
     97                             readback, 0);
     98 
     99         match = true;
    100 
    101         for (int y = 0; y < Y_SIZE; ++y) {
    102             for (int x = 0; x < X_SIZE; ++x) {
    103                 if (0xFF != readback[x][y]) {
    104                     match = false;
    105                 }
    106             }
    107         }
    108 
    109         REPORTER_ASSERT(reporter, match);
    110     }
    111 }
    112 
    113 #include "TestClassDef.h"
    114 DEFINE_GPUTESTCLASS("ReadWriteAlpha", ReadWriteAlphaTestClass, ReadWriteAlphaTest)
    115 
    116 #endif
    117