blob: 8fedfb0b0b3cdcaac58ddabc97bb1e2e35e70028 [file] [log] [blame]
Robert Phillipsd095b9f2020-02-03 16:12:51 -05001/*
2 * Copyright 2020 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
8#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkImage.h"
11#include "src/core/SkCompressedDataUtils.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/image/SkImage_Base.h"
15
16constexpr int kImgWidth = 16;
17constexpr int kImgHeight = 8;
18constexpr int kPad = 4;
19
20struct BC1Block {
21 uint16_t fColor0;
22 uint16_t fColor1;
23 uint32_t fIndices;
24};
25
26static int num_4x4_blocks(int size) {
27 return ((size + 3) & ~3) >> 2;
28}
29
30static uint16_t to565(SkColor col) {
31 int r5 = SkMulDiv255Round(31, SkColorGetR(col));
32 int g6 = SkMulDiv255Round(63, SkColorGetG(col));
33 int b5 = SkMulDiv255Round(31, SkColorGetB(col));
34
35 return (r5 << 11) | (g6 << 5) | b5;
36}
37
38// BC1 has per-block transparency. If, taken as ints,
39// fColor0 < fColor1 -> the block has transparency (& it is in color3)
40// fColor1 > fColor0 -> the block is opaque
41//
42// This method can create two blocks to test out BC1's behavior. If BC1
43// behaves as expected (i.e., w/ per-block transparency) then, for RGBA textures,
44// the transparent block(s) should appear as:
45// opaque black, medium grey, transparent black, white.
46// and the opaque block(s) should appear as:
47// opaque black, dark grey, light grey, white
48//
49// For RGB textures, however, the transparent block(s) should appear as:
50// opaque black, medium grey, _opaque_ black, white
51// and the opaque block(s) should appear as:
52// opaque black, dark grey, light grey, white.
53static void create_BC1_block(BC1Block* block, bool transparent) {
54 unsigned int byte;
55
56 if (transparent) {
57 block->fColor0 = to565(SK_ColorBLACK);
58 block->fColor1 = to565(SK_ColorWHITE);
59 SkASSERT(block->fColor0 <= block->fColor1); // this signals a transparent block
60 // opaque black (col0), medium grey (col2), transparent black (col3), white (col1).
61 byte = (0x0 << 0) | (0x2 << 2) | (0x3 << 4) | (0x1 << 6);
62 } else {
63 block->fColor0 = to565(SK_ColorWHITE);
64 block->fColor1 = to565(SK_ColorBLACK);
65 SkASSERT(block->fColor0 > block->fColor1); // this signals an opaque block
66 // opaque black (col1), dark grey (col3), light grey (col2), white (col0)
67 byte = (0x1 << 0) | (0x3 << 2) | (0x2 << 4) | (0x0 << 6);
68 }
69
70 block->fIndices = (byte << 24) | (byte << 16) | (byte << 8) | byte;
71}
72
73// This makes a 16x8 BC1 texture which has the top 4 rows be officially transparent
74// and the bottom 4 rows be officially opaque.
75static sk_sp<SkData> make_compressed_data() {
76 SkISize dim{ kImgWidth, kImgHeight };
77
78 size_t totalSize = SkCompressedDataSize(SkImage::CompressionType::kBC1_RGB8_UNORM, dim,
79 nullptr, false);
80
81 sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize);
82 BC1Block* dstBlocks = reinterpret_cast<BC1Block*>(tmp->writable_data());
83
84 BC1Block transBlock, opaqueBlock;
85 create_BC1_block(&transBlock, true);
86 create_BC1_block(&opaqueBlock, false);
87
88 int numXBlocks = num_4x4_blocks(kImgWidth);
89 int numYBlocks = num_4x4_blocks(kImgHeight);
90
91 for (int y = 0; y < numYBlocks; ++y) {
92 for (int x = 0; x < numXBlocks; ++x) {
93 dstBlocks[y*numXBlocks + x] = (y < numYBlocks/2) ? transBlock : opaqueBlock;
94 }
95 }
96
97 return tmp;
98}
99
100static sk_sp<SkImage> data_to_img(GrContext *context, sk_sp<SkData> data,
101 SkImage::CompressionType compression) {
102 if (context) {
103 return SkImage::MakeTextureFromCompressed(context, std::move(data),
104 kImgWidth,
105 kImgHeight,
106 compression,
107 GrMipMapped::kNo);
108 } else {
109 return SkImage::MakeRasterFromCompressed(std::move(data),
110 kImgWidth,
111 kImgHeight,
112 compression);
113 }
114}
115
116static void draw_image(GrContext* context, SkCanvas* canvas, sk_sp<SkImage> image, int x, int y) {
117
118 bool isCompressed = false;
119 if (image && image->isTextureBacked()) {
120 const GrCaps* caps = context->priv().caps();
121
122 GrTextureProxy* proxy = as_IB(image)->peekProxy();
123 isCompressed = caps->isFormatCompressed(proxy->backendFormat());
124 }
125
126 canvas->drawImage(image, x, y);
127
128 if (!isCompressed) {
129 SkRect r = SkRect::MakeXYWH(x, y, kImgWidth, kImgHeight);
130 r.outset(1.0f, 1.0f);
131
132 SkPaint redStroke;
133 redStroke.setColor(SK_ColorRED);
134 redStroke.setStyle(SkPaint::kStroke_Style);
135 redStroke.setStrokeWidth(2.0f);
136
137 canvas->drawRect(r, redStroke);
138 }
139}
140
141namespace skiagm {
142
143// This GM draws the BC1 compressed texture filled with "make_compressed_data"s data twice.
144//
145// It is drawn once (on the top) as a kBC1_RGB8_UNORM texture and then again (on the bottom)
146// as a kBC1_RGBA8_UNORM texture.
147//
148// If BC1 behaves as expected we should see:
149//
150// RGB8 Black MidGrey Black* White ...
151// Black DrkGrey LtGrey White ...
152//
153// RGBA8 Black MidGrey Green+ White ...
154// Black DrkGrey LtGrey White ...
155//
156// * We expect this to be black bc the transparent black will be forced to opaque. If BC1 were
157// treating it as an opaque block then it would be LtGrey - not black.
158// + This is just the background showing through the transparent black
159class BC1TransparencyGM : public GM {
160public:
161 BC1TransparencyGM() {
162 this->setBGColor(SK_ColorGREEN);
163 }
164
165protected:
166
167 SkString onShortName() override {
168 return SkString("bc1_transparency");
169 }
170
171 SkISize onISize() override {
172 return SkISize::Make(kImgWidth + 2 * kPad, 2 * kImgHeight + 3 * kPad);
173 }
174
175 void onOnceBeforeDraw() override {
176 fBC1Data = make_compressed_data();
177 }
178
179 void onDraw(SkCanvas* canvas) override {
180 GrContext* context = canvas->getGrContext();
181
182 sk_sp<SkImage> rgbImg = data_to_img(context, fBC1Data,
183 SkImage::CompressionType::kBC1_RGB8_UNORM);
184
185 sk_sp<SkImage> rgbaImg = data_to_img(context, fBC1Data,
186 SkImage::CompressionType::kBC1_RGBA8_UNORM);
187
188 draw_image(context, canvas, rgbImg, kPad, kPad);
189 draw_image(context, canvas, rgbaImg, kPad, 2 * kPad + kImgHeight);
190 }
191
192private:
193 sk_sp<SkData> fBC1Data;
194
195 typedef GM INHERITED;
196};
197
198//////////////////////////////////////////////////////////////////////////////
199
200DEF_GM(return new BC1TransparencyGM;)
201}