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 "include/core/SkTypes.h" // IWYU pragma: keep 9 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" 23 #include "third_party/etc1/etc1.h" 24 25 class GrContext; 26 class GrRenderTargetContext; 27 28 // Basic test of Ganesh's ETC1 support 29 class ETC1GM : public skiagm::GpuGM { 30 public: ETC1GM()31 ETC1GM() { 32 this->setBGColor(0xFFCCCCCC); 33 } 34 35 protected: onShortName()36 SkString onShortName() override { 37 return SkString("etc1"); 38 } 39 onISize()40 SkISize onISize() override { 41 return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad); 42 } 43 onOnceBeforeDraw()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()); 59 fETC1Data = SkData::MakeUninitialized(size); 60 61 unsigned char* pixels = (unsigned char*) fETC1Data->writable_data(); 62 63 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0), 64 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) { 65 fETC1Data = nullptr; 66 } 67 } 68 onDraw(GrContext * context,GrRenderTargetContext *,SkCanvas * canvas)69 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { 70 sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data, 71 kTexWidth, kTexHeight, 72 SkImage::kETC1_CompressionType); 73 74 canvas->drawImage(image, 0, 0); 75 } 76 77 private: 78 static const int kPad = 8; 79 static const int kTexWidth = 16; 80 static const int kTexHeight = 20; 81 82 sk_sp<SkData> fETC1Data; 83 84 typedef GM INHERITED; 85 }; 86 87 ////////////////////////////////////////////////////////////////////////////// 88 89 DEF_GM(return new ETC1GM;) 90 91 #endif 92