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