blob: 301bf6b78e428c9d256d6dba0906ca174f10f33f [file] [log] [blame]
robertphillips@google.com69950682012-04-06 18:06:10 +00001/*
2 * Copyright 2012 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
reedf037e0b2014-10-30 11:34:15 -07008#include "Test.h"
9
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010// This test is specific to the GPU backend.
djsollen@google.comefbe8e92013-02-07 18:58:35 +000011#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000012
bsalomon@google.com67b915d2013-02-04 16:13:32 +000013#include "GrContextFactory.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014#include "SkGpuDevice.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000015
16static const int X_SIZE = 12;
17static const int Y_SIZE = 12;
18
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000019DEF_GPUTEST(ReadWriteAlpha, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000020 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
21 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
22 if (!GrContextFactory::IsRenderingGLContext(glType)) {
23 continue;
robertphillips@google.com69950682012-04-06 18:06:10 +000024 }
bsalomon@google.com67b915d2013-02-04 16:13:32 +000025 GrContext* context = factory->get(glType);
halcanary96fcdcc2015-08-27 07:41:13 -070026 if (nullptr == context) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +000027 continue;
28 }
robertphillips@google.com69950682012-04-06 18:06:10 +000029
bsalomon@google.com67b915d2013-02-04 16:13:32 +000030 unsigned char textureData[X_SIZE][Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000031
bsalomon@google.com67b915d2013-02-04 16:13:32 +000032 memset(textureData, 0, X_SIZE * Y_SIZE);
robertphillips@google.com69950682012-04-06 18:06:10 +000033
bsalomonf2703d82014-10-28 14:33:06 -070034 GrSurfaceDesc desc;
robertphillips@google.com69950682012-04-06 18:06:10 +000035
bsalomon@google.com67b915d2013-02-04 16:13:32 +000036 // let Skia know we will be using this texture as a render target
bsalomonf2703d82014-10-28 14:33:06 -070037 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000038 // it is a single channel texture
39 desc.fConfig = kAlpha_8_GrPixelConfig;
40 desc.fWidth = X_SIZE;
41 desc.fHeight = Y_SIZE;
robertphillips@google.com69950682012-04-06 18:06:10 +000042
bsalomon@google.com67b915d2013-02-04 16:13:32 +000043 // We are initializing the texture with zeros here
bsalomond309e7a2015-04-30 14:18:54 -070044 GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000045 if (!texture) {
46 return;
47 }
robertphillips@google.com69950682012-04-06 18:06:10 +000048
bsalomondcabb052014-07-21 14:24:01 -070049 SkAutoTUnref<GrTexture> au(texture);
bsalomon@google.com67b915d2013-02-04 16:13:32 +000050
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;
robertphillips@google.com69950682012-04-06 18:06:10 +000055 }
56 }
robertphillips@google.com69950682012-04-06 18:06:10 +000057
bsalomon@google.com67b915d2013-02-04 16:13:32 +000058 // upload the texture
59 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
60 textureData, 0);
robertphillips@google.com69950682012-04-06 18:06:10 +000061
bsalomon@google.com67b915d2013-02-04 16:13:32 +000062 unsigned char readback[X_SIZE][Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000063
bsalomon@google.com67b915d2013-02-04 16:13:32 +000064 // clear readback to something non-zero so we can detect readback failures
65 memset(readback, 0x1, X_SIZE * Y_SIZE);
robertphillips@google.com69950682012-04-06 18:06:10 +000066
bsalomon@google.com67b915d2013-02-04 16:13:32 +000067 // read the texture back
68 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
69 readback, 0);
robertphillips@google.com69950682012-04-06 18:06:10 +000070
bsalomon@google.com67b915d2013-02-04 16:13:32 +000071 // make sure the original & read back versions match
72 bool match = true;
robertphillips@google.com69950682012-04-06 18:06:10 +000073
bsalomon@google.com67b915d2013-02-04 16:13:32 +000074 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 }
robertphillips@google.com69950682012-04-06 18:06:10 +000079 }
80 }
robertphillips@google.com69950682012-04-06 18:06:10 +000081
bsalomon@google.com67b915d2013-02-04 16:13:32 +000082 REPORTER_ASSERT(reporter, match);
83
84 // Now try writing on the single channel texture
bsalomonafe30052015-01-16 07:32:33 -080085 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
bsalomon74f681d2015-06-23 14:38:48 -070086 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), &props,
87 SkGpuDevice::kUninit_InitContents));
bsalomon@google.com67b915d2013-02-04 16:13:32 +000088 SkCanvas canvas(device);
89
90 SkPaint paint;
91
92 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
93
94 paint.setColor(SK_ColorWHITE);
95
96 canvas.drawRect(rect, paint);
97
98 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
99 readback, 0);
100
101 match = true;
102
103 for (int y = 0; y < Y_SIZE; ++y) {
104 for (int x = 0; x < X_SIZE; ++x) {
105 if (0xFF != readback[x][y]) {
106 match = false;
107 }
108 }
109 }
110
111 REPORTER_ASSERT(reporter, match);
112 }
robertphillips@google.com69950682012-04-06 18:06:10 +0000113}
114
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000115#endif