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