Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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.h" |
| 9 | #include "sk_tool_utils.h" |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 10 | #include "SkImage.h" |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 11 | #include "SkRandom.h" |
| 12 | |
Jim Van Verth | cb7149e | 2019-01-15 16:29:28 -0500 | [diff] [blame] | 13 | #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_GOOGLE3) |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 14 | #include "etc1.h" |
| 15 | |
| 16 | #include "GrContext.h" |
| 17 | #include "GrGpu.h" |
| 18 | #include "GrRenderTargetContext.h" |
| 19 | #include "GrRenderTargetContextPriv.h" |
| 20 | #include "GrTextureProxy.h" |
| 21 | #include "effects/GrSimpleTextureEffect.h" |
| 22 | #include "ops/GrFillRectOp.h" |
| 23 | |
| 24 | // Basic test of Ganesh's ETC1 support |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 25 | class ETC1GM : public skiagm::GpuGM { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 26 | public: |
| 27 | ETC1GM() { |
| 28 | this->setBGColor(0xFFCCCCCC); |
| 29 | } |
| 30 | |
| 31 | protected: |
| 32 | SkString onShortName() override { |
| 33 | return SkString("etc1"); |
| 34 | } |
| 35 | |
| 36 | SkISize onISize() override { |
| 37 | return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad); |
| 38 | } |
| 39 | |
| 40 | void onOnceBeforeDraw() override { |
| 41 | SkBitmap bm; |
| 42 | SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType, |
| 43 | kOpaque_SkAlphaType); |
| 44 | bm.allocPixels(ii); |
| 45 | |
| 46 | bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight)); |
| 47 | |
| 48 | for (int y = 0; y < kTexHeight; y += 4) { |
| 49 | for (int x = 0; x < kTexWidth; x += 4) { |
| 50 | bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int size = etc1_get_encoded_data_size(bm.width(), bm.height()); |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 55 | fETC1Data = SkData::MakeUninitialized(size); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 56 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 57 | unsigned char* pixels = (unsigned char*) fETC1Data->writable_data(); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 58 | |
| 59 | if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0), |
| 60 | bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) { |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 61 | fETC1Data = nullptr; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame^] | 65 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 66 | sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data, |
| 67 | kTexWidth, kTexHeight, |
| 68 | SkImage::kETC1_CompressionType); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 69 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 70 | canvas->drawImage(image, 0, 0); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private: |
| 74 | static const int kPad = 8; |
| 75 | static const int kTexWidth = 16; |
| 76 | static const int kTexHeight = 20; |
| 77 | |
Jim Van Verth | ee06b33 | 2019-01-18 10:36:32 -0500 | [diff] [blame] | 78 | sk_sp<SkData> fETC1Data; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 79 | |
| 80 | typedef GM INHERITED; |
| 81 | }; |
| 82 | |
| 83 | ////////////////////////////////////////////////////////////////////////////// |
| 84 | |
| 85 | DEF_GM(return new ETC1GM;) |
| 86 | |
| 87 | #endif |