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" 10 #include "SkImage.h" 11 #include "SkRandom.h" 12 13 #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_GOOGLE3) 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 25 class ETC1GM : public skiagm::GpuGM { 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()); 55 fETC1Data = SkData::MakeUninitialized(size); 56 57 unsigned char* pixels = (unsigned char*) fETC1Data->writable_data(); 58 59 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0), 60 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) { 61 fETC1Data = nullptr; 62 } 63 } 64 65 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { 66 sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data, 67 kTexWidth, kTexHeight, 68 SkImage::kETC1_CompressionType); 69 70 canvas->drawImage(image, 0, 0); 71 } 72 73 private: 74 static const int kPad = 8; 75 static const int kTexWidth = 16; 76 static const int kTexHeight = 20; 77 78 sk_sp<SkData> fETC1Data; 79 80 typedef GM INHERITED; 81 }; 82 83 ////////////////////////////////////////////////////////////////////////////// 84 85 DEF_GM(return new ETC1GM;) 86 87 #endif 88