blob: 080849691daaf1cd80ca07b66b2b36bbfef46344 [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"
Adlai Holler4caa9352020-07-16 10:58:58 -040011#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Robert Phillipsd095b9f2020-02-03 16:12:51 -050013#include "src/core/SkCompressedDataUtils.h"
14#include "src/gpu/GrCaps.h"
Adlai Holler302e8fb2020-09-14 11:58:06 -040015#include "src/gpu/GrImageContextPriv.h"
Robert Phillipsd095b9f2020-02-03 16:12:51 -050016#include "src/image/SkImage_Base.h"
Robert Phillipsa84caa32020-07-28 09:57:26 -040017#include "src/image/SkImage_GpuBase.h"
Robert Phillipsd095b9f2020-02-03 16:12:51 -050018
19constexpr int kImgWidth = 16;
20constexpr int kImgHeight = 8;
21constexpr int kPad = 4;
22
23struct BC1Block {
24 uint16_t fColor0;
25 uint16_t fColor1;
26 uint32_t fIndices;
27};
28
29static int num_4x4_blocks(int size) {
30 return ((size + 3) & ~3) >> 2;
31}
32
33static uint16_t to565(SkColor col) {
34 int r5 = SkMulDiv255Round(31, SkColorGetR(col));
35 int g6 = SkMulDiv255Round(63, SkColorGetG(col));
36 int b5 = SkMulDiv255Round(31, SkColorGetB(col));
37
38 return (r5 << 11) | (g6 << 5) | b5;
39}
40
41// BC1 has per-block transparency. If, taken as ints,
42// fColor0 < fColor1 -> the block has transparency (& it is in color3)
43// fColor1 > fColor0 -> the block is opaque
44//
45// This method can create two blocks to test out BC1's behavior. If BC1
46// behaves as expected (i.e., w/ per-block transparency) then, for RGBA textures,
47// the transparent block(s) should appear as:
48// opaque black, medium grey, transparent black, white.
49// and the opaque block(s) should appear as:
50// opaque black, dark grey, light grey, white
51//
52// For RGB textures, however, the transparent block(s) should appear as:
53// opaque black, medium grey, _opaque_ black, white
54// and the opaque block(s) should appear as:
55// opaque black, dark grey, light grey, white.
56static void create_BC1_block(BC1Block* block, bool transparent) {
57 unsigned int byte;
58
59 if (transparent) {
60 block->fColor0 = to565(SK_ColorBLACK);
61 block->fColor1 = to565(SK_ColorWHITE);
62 SkASSERT(block->fColor0 <= block->fColor1); // this signals a transparent block
63 // opaque black (col0), medium grey (col2), transparent black (col3), white (col1).
64 byte = (0x0 << 0) | (0x2 << 2) | (0x3 << 4) | (0x1 << 6);
65 } else {
66 block->fColor0 = to565(SK_ColorWHITE);
67 block->fColor1 = to565(SK_ColorBLACK);
68 SkASSERT(block->fColor0 > block->fColor1); // this signals an opaque block
69 // opaque black (col1), dark grey (col3), light grey (col2), white (col0)
70 byte = (0x1 << 0) | (0x3 << 2) | (0x2 << 4) | (0x0 << 6);
71 }
72
73 block->fIndices = (byte << 24) | (byte << 16) | (byte << 8) | byte;
74}
75
76// This makes a 16x8 BC1 texture which has the top 4 rows be officially transparent
77// and the bottom 4 rows be officially opaque.
78static sk_sp<SkData> make_compressed_data() {
79 SkISize dim{ kImgWidth, kImgHeight };
80
81 size_t totalSize = SkCompressedDataSize(SkImage::CompressionType::kBC1_RGB8_UNORM, dim,
82 nullptr, false);
83
84 sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize);
85 BC1Block* dstBlocks = reinterpret_cast<BC1Block*>(tmp->writable_data());
86
87 BC1Block transBlock, opaqueBlock;
88 create_BC1_block(&transBlock, true);
89 create_BC1_block(&opaqueBlock, false);
90
91 int numXBlocks = num_4x4_blocks(kImgWidth);
92 int numYBlocks = num_4x4_blocks(kImgHeight);
93
94 for (int y = 0; y < numYBlocks; ++y) {
95 for (int x = 0; x < numXBlocks; ++x) {
96 dstBlocks[y*numXBlocks + x] = (y < numYBlocks/2) ? transBlock : opaqueBlock;
97 }
98 }
99
100 return tmp;
101}
102
Adlai Holler4caa9352020-07-16 10:58:58 -0400103static sk_sp<SkImage> data_to_img(GrDirectContext *direct, sk_sp<SkData> data,
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500104 SkImage::CompressionType compression) {
Adlai Holler4caa9352020-07-16 10:58:58 -0400105 if (direct) {
106 return SkImage::MakeTextureFromCompressed(direct, std::move(data),
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500107 kImgWidth,
108 kImgHeight,
109 compression,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400110 GrMipmapped::kNo);
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500111 } else {
112 return SkImage::MakeRasterFromCompressed(std::move(data),
113 kImgWidth,
114 kImgHeight,
115 compression);
116 }
117}
118
Robert Phillipsa84caa32020-07-28 09:57:26 -0400119static void draw_image(SkCanvas* canvas, sk_sp<SkImage> image, int x, int y) {
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500120
121 bool isCompressed = false;
122 if (image && image->isTextureBacked()) {
Adlai Holler302e8fb2020-09-14 11:58:06 -0400123 const GrCaps* caps = as_IB(image)->context()->priv().caps();
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500124
125 GrTextureProxy* proxy = as_IB(image)->peekProxy();
126 isCompressed = caps->isFormatCompressed(proxy->backendFormat());
127 }
128
129 canvas->drawImage(image, x, y);
130
131 if (!isCompressed) {
132 SkRect r = SkRect::MakeXYWH(x, y, kImgWidth, kImgHeight);
133 r.outset(1.0f, 1.0f);
134
135 SkPaint redStroke;
136 redStroke.setColor(SK_ColorRED);
137 redStroke.setStyle(SkPaint::kStroke_Style);
138 redStroke.setStrokeWidth(2.0f);
139
140 canvas->drawRect(r, redStroke);
141 }
142}
143
144namespace skiagm {
145
146// This GM draws the BC1 compressed texture filled with "make_compressed_data"s data twice.
147//
148// It is drawn once (on the top) as a kBC1_RGB8_UNORM texture and then again (on the bottom)
149// as a kBC1_RGBA8_UNORM texture.
150//
151// If BC1 behaves as expected we should see:
152//
153// RGB8 Black MidGrey Black* White ...
154// Black DrkGrey LtGrey White ...
155//
156// RGBA8 Black MidGrey Green+ White ...
157// Black DrkGrey LtGrey White ...
158//
159// * We expect this to be black bc the transparent black will be forced to opaque. If BC1 were
160// treating it as an opaque block then it would be LtGrey - not black.
161// + This is just the background showing through the transparent black
162class BC1TransparencyGM : public GM {
163public:
164 BC1TransparencyGM() {
165 this->setBGColor(SK_ColorGREEN);
166 }
167
168protected:
169
170 SkString onShortName() override {
171 return SkString("bc1_transparency");
172 }
173
174 SkISize onISize() override {
175 return SkISize::Make(kImgWidth + 2 * kPad, 2 * kImgHeight + 3 * kPad);
176 }
177
Robert Phillipsa84caa32020-07-28 09:57:26 -0400178 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
179 if (dContext && dContext->abandoned()) {
180 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
181 // if forbidden.
182 return DrawResult::kSkip;
183 }
184
185 sk_sp<SkData> bc1Data = make_compressed_data();
186
187 fRGBImage = data_to_img(dContext, bc1Data, SkImage::CompressionType::kBC1_RGB8_UNORM);
188 fRGBAImage = data_to_img(dContext, std::move(bc1Data),
189 SkImage::CompressionType::kBC1_RGBA8_UNORM);
190 if (!fRGBImage || !fRGBAImage) {
191 *errorMsg = "Failed to create BC1 images.";
192 return DrawResult::kFail;
193 }
194
195 return DrawResult::kOk;
196 }
197
198 void onGpuTeardown() override {
199 fRGBImage = nullptr;
200 fRGBAImage = nullptr;
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500201 }
202
203 void onDraw(SkCanvas* canvas) override {
Robert Phillipsa84caa32020-07-28 09:57:26 -0400204 draw_image(canvas, fRGBImage, kPad, kPad);
205 draw_image(canvas, fRGBAImage, kPad, 2 * kPad + kImgHeight);
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500206 }
207
208private:
Robert Phillipsa84caa32020-07-28 09:57:26 -0400209 sk_sp<SkImage> fRGBImage;
210 sk_sp<SkImage> fRGBAImage;
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500211
John Stiles7571f9e2020-09-02 22:42:33 -0400212 using INHERITED = GM;
Robert Phillipsd095b9f2020-02-03 16:12:51 -0500213};
214
215//////////////////////////////////////////////////////////////////////////////
216
217DEF_GM(return new BC1TransparencyGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400218} // namespace skiagm