blob: 1ee6b09a995d29ce5514dd571a315ceec1332f9b [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
kkinnunen15302832015-12-01 04:35:26 -080013#include "GrContext.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000014#include "SkGpuDevice.h"
robertphillips@google.com69950682012-04-06 18:06:10 +000015
bsalomonf46a1242015-12-15 12:37:38 -080016// This was made indivisible by 4 to ensure we test setting GL_PACK_ALIGNMENT properly.
17static const int X_SIZE = 13;
18static const int Y_SIZE = 13;
robertphillips@google.com69950682012-04-06 18:06:10 +000019
kkinnunen15302832015-12-01 04:35:26 -080020DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) {
21 unsigned char textureData[X_SIZE][Y_SIZE];
robertphillips@google.com69950682012-04-06 18:06:10 +000022
kkinnunen15302832015-12-01 04:35:26 -080023 memset(textureData, 0, X_SIZE * Y_SIZE);
robertphillips@google.com69950682012-04-06 18:06:10 +000024
kkinnunen15302832015-12-01 04:35:26 -080025 GrSurfaceDesc desc;
robertphillips@google.com69950682012-04-06 18:06:10 +000026
kkinnunen15302832015-12-01 04:35:26 -080027 // 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;
robertphillips@google.com69950682012-04-06 18:06:10 +000033
kkinnunen15302832015-12-01 04:35:26 -080034 // We are initializing the texture with zeros here
35 GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
36 if (!texture) {
37 return;
bsalomon@google.com67b915d2013-02-04 16:13:32 +000038 }
kkinnunen15302832015-12-01 04:35:26 -080039
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
53 unsigned char readback[X_SIZE][Y_SIZE];
54
55 // clear readback to something non-zero so we can detect readback failures
56 memset(readback, 0x1, X_SIZE * Y_SIZE);
57
58 // read the texture back
59 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
60 readback, 0);
61
62 // make sure the original & read back versions match
63 bool match = true;
64
65 for (int y = 0; y < Y_SIZE; ++y) {
66 for (int x = 0; x < X_SIZE; ++x) {
67 if (textureData[x][y] != readback[x][y]) {
68 match = false;
69 }
70 }
71 }
72
73 REPORTER_ASSERT(reporter, match);
74
75 // Now try writing on the single channel texture
76 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
77 SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), &props,
78 SkGpuDevice::kUninit_InitContents));
79 SkCanvas canvas(device);
80
81 SkPaint paint;
82
83 const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
84
85 paint.setColor(SK_ColorWHITE);
86
87 canvas.drawRect(rect, paint);
88
89 texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
90 readback, 0);
91
92 match = true;
93
94 for (int y = 0; y < Y_SIZE; ++y) {
95 for (int x = 0; x < X_SIZE; ++x) {
96 if (0xFF != readback[x][y]) {
97 match = false;
98 }
99 }
100 }
101
102 REPORTER_ASSERT(reporter, match);
robertphillips@google.com69950682012-04-06 18:06:10 +0000103}
104
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000105#endif