Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 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" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 13 | #include "src/core/SkCompressedDataUtils.h" |
| 14 | #include "src/gpu/GrCaps.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 16 | #include "src/image/SkImage_Base.h" |
| 17 | |
| 18 | constexpr int kImgWidth = 16; |
| 19 | constexpr int kImgHeight = 8; |
| 20 | constexpr int kPad = 4; |
| 21 | |
| 22 | struct BC1Block { |
| 23 | uint16_t fColor0; |
| 24 | uint16_t fColor1; |
| 25 | uint32_t fIndices; |
| 26 | }; |
| 27 | |
| 28 | static int num_4x4_blocks(int size) { |
| 29 | return ((size + 3) & ~3) >> 2; |
| 30 | } |
| 31 | |
| 32 | static uint16_t to565(SkColor col) { |
| 33 | int r5 = SkMulDiv255Round(31, SkColorGetR(col)); |
| 34 | int g6 = SkMulDiv255Round(63, SkColorGetG(col)); |
| 35 | int b5 = SkMulDiv255Round(31, SkColorGetB(col)); |
| 36 | |
| 37 | return (r5 << 11) | (g6 << 5) | b5; |
| 38 | } |
| 39 | |
| 40 | // BC1 has per-block transparency. If, taken as ints, |
| 41 | // fColor0 < fColor1 -> the block has transparency (& it is in color3) |
| 42 | // fColor1 > fColor0 -> the block is opaque |
| 43 | // |
| 44 | // This method can create two blocks to test out BC1's behavior. If BC1 |
| 45 | // behaves as expected (i.e., w/ per-block transparency) then, for RGBA textures, |
| 46 | // the transparent block(s) should appear as: |
| 47 | // opaque black, medium grey, transparent black, white. |
| 48 | // and the opaque block(s) should appear as: |
| 49 | // opaque black, dark grey, light grey, white |
| 50 | // |
| 51 | // For RGB textures, however, the transparent block(s) should appear as: |
| 52 | // opaque black, medium grey, _opaque_ black, white |
| 53 | // and the opaque block(s) should appear as: |
| 54 | // opaque black, dark grey, light grey, white. |
| 55 | static void create_BC1_block(BC1Block* block, bool transparent) { |
| 56 | unsigned int byte; |
| 57 | |
| 58 | if (transparent) { |
| 59 | block->fColor0 = to565(SK_ColorBLACK); |
| 60 | block->fColor1 = to565(SK_ColorWHITE); |
| 61 | SkASSERT(block->fColor0 <= block->fColor1); // this signals a transparent block |
| 62 | // opaque black (col0), medium grey (col2), transparent black (col3), white (col1). |
| 63 | byte = (0x0 << 0) | (0x2 << 2) | (0x3 << 4) | (0x1 << 6); |
| 64 | } else { |
| 65 | block->fColor0 = to565(SK_ColorWHITE); |
| 66 | block->fColor1 = to565(SK_ColorBLACK); |
| 67 | SkASSERT(block->fColor0 > block->fColor1); // this signals an opaque block |
| 68 | // opaque black (col1), dark grey (col3), light grey (col2), white (col0) |
| 69 | byte = (0x1 << 0) | (0x3 << 2) | (0x2 << 4) | (0x0 << 6); |
| 70 | } |
| 71 | |
| 72 | block->fIndices = (byte << 24) | (byte << 16) | (byte << 8) | byte; |
| 73 | } |
| 74 | |
| 75 | // This makes a 16x8 BC1 texture which has the top 4 rows be officially transparent |
| 76 | // and the bottom 4 rows be officially opaque. |
| 77 | static sk_sp<SkData> make_compressed_data() { |
| 78 | SkISize dim{ kImgWidth, kImgHeight }; |
| 79 | |
| 80 | size_t totalSize = SkCompressedDataSize(SkImage::CompressionType::kBC1_RGB8_UNORM, dim, |
| 81 | nullptr, false); |
| 82 | |
| 83 | sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize); |
| 84 | BC1Block* dstBlocks = reinterpret_cast<BC1Block*>(tmp->writable_data()); |
| 85 | |
| 86 | BC1Block transBlock, opaqueBlock; |
| 87 | create_BC1_block(&transBlock, true); |
| 88 | create_BC1_block(&opaqueBlock, false); |
| 89 | |
| 90 | int numXBlocks = num_4x4_blocks(kImgWidth); |
| 91 | int numYBlocks = num_4x4_blocks(kImgHeight); |
| 92 | |
| 93 | for (int y = 0; y < numYBlocks; ++y) { |
| 94 | for (int x = 0; x < numXBlocks; ++x) { |
| 95 | dstBlocks[y*numXBlocks + x] = (y < numYBlocks/2) ? transBlock : opaqueBlock; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return tmp; |
| 100 | } |
| 101 | |
| 102 | static sk_sp<SkImage> data_to_img(GrContext *context, sk_sp<SkData> data, |
| 103 | SkImage::CompressionType compression) { |
| 104 | if (context) { |
| 105 | return SkImage::MakeTextureFromCompressed(context, std::move(data), |
| 106 | kImgWidth, |
| 107 | kImgHeight, |
| 108 | compression, |
| 109 | GrMipMapped::kNo); |
| 110 | } else { |
| 111 | return SkImage::MakeRasterFromCompressed(std::move(data), |
| 112 | kImgWidth, |
| 113 | kImgHeight, |
| 114 | compression); |
| 115 | } |
| 116 | } |
| 117 | |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 118 | static void draw_image(GrRecordingContext* context, SkCanvas* canvas, |
| 119 | sk_sp<SkImage> image, int x, int y) { |
Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 120 | |
| 121 | bool isCompressed = false; |
| 122 | if (image && image->isTextureBacked()) { |
| 123 | const GrCaps* caps = context->priv().caps(); |
| 124 | |
| 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 | |
| 144 | namespace 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 |
| 162 | class BC1TransparencyGM : public GM { |
| 163 | public: |
| 164 | BC1TransparencyGM() { |
| 165 | this->setBGColor(SK_ColorGREEN); |
| 166 | } |
| 167 | |
| 168 | protected: |
| 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 | |
| 178 | void onOnceBeforeDraw() override { |
| 179 | fBC1Data = make_compressed_data(); |
| 180 | } |
| 181 | |
| 182 | void onDraw(SkCanvas* canvas) override { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 183 | auto context = canvas->getGrContext(); |
Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 184 | |
| 185 | sk_sp<SkImage> rgbImg = data_to_img(context, fBC1Data, |
| 186 | SkImage::CompressionType::kBC1_RGB8_UNORM); |
| 187 | |
| 188 | sk_sp<SkImage> rgbaImg = data_to_img(context, fBC1Data, |
| 189 | SkImage::CompressionType::kBC1_RGBA8_UNORM); |
| 190 | |
| 191 | draw_image(context, canvas, rgbImg, kPad, kPad); |
| 192 | draw_image(context, canvas, rgbaImg, kPad, 2 * kPad + kImgHeight); |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | sk_sp<SkData> fBC1Data; |
| 197 | |
| 198 | typedef GM INHERITED; |
| 199 | }; |
| 200 | |
| 201 | ////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | DEF_GM(return new BC1TransparencyGM;) |
| 204 | } |