krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 10 | #include "Resources.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkData.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 13 | #include "SkImageGenerator.h" |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 14 | #include "SkImageDecoder.h" |
| 15 | #include "SkOSFile.h" |
| 16 | #include "SkTextureCompressor.h" |
| 17 | |
| 18 | static const char *kASTCFilenames[] = { |
| 19 | "mandrill_128x128_4x4.astc", // kASTC_4x4_Format |
| 20 | "mandrill_130x128_5x4.astc", // kASTC_5x4_Format |
| 21 | "mandrill_130x130_5x5.astc", // kASTC_5x5_Format |
| 22 | "mandrill_132x130_6x5.astc", // kASTC_6x5_Format |
| 23 | "mandrill_132x132_6x6.astc", // kASTC_6x6_Format |
| 24 | "mandrill_128x130_8x5.astc", // kASTC_8x5_Format |
| 25 | "mandrill_128x132_8x6.astc", // kASTC_8x6_Format |
| 26 | "mandrill_128x128_8x8.astc", // kASTC_8x8_Format |
| 27 | "mandrill_130x130_10x5.astc", // kASTC_10x5_Format |
| 28 | "mandrill_130x132_10x6.astc", // kASTC_10x6_Format |
| 29 | "mandrill_130x128_10x8.astc", // kASTC_10x8_Format |
| 30 | "mandrill_130x130_10x10.astc", // kASTC_10x10_Format |
| 31 | "mandrill_132x130_12x10.astc", // kASTC_12x10_Format |
| 32 | "mandrill_132x132_12x12.astc", // kASTC_12x12_Format |
| 33 | }; |
| 34 | |
| 35 | static const int kNumASTCFilenames = SK_ARRAY_COUNT(kASTCFilenames); |
| 36 | |
| 37 | static inline const char *get_astc_filename(int idx) { |
| 38 | if (idx < 0 || kNumASTCFilenames <= idx) { |
| 39 | return ""; |
| 40 | } |
| 41 | |
| 42 | return kASTCFilenames[idx]; |
| 43 | } |
| 44 | |
| 45 | namespace skiagm { |
| 46 | |
| 47 | /** |
| 48 | * Test decoding an image from an ASTC file and then from compressed ASTC data. |
| 49 | */ |
| 50 | class ASTCBitmapGM : public GM { |
| 51 | public: |
| 52 | ASTCBitmapGM() { } |
| 53 | virtual ~ASTCBitmapGM() { } |
| 54 | |
| 55 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 56 | SkString onShortName() SK_OVERRIDE { |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 57 | return SkString("astcbitmap"); |
| 58 | } |
| 59 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 60 | SkISize onISize() SK_OVERRIDE { |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 61 | return SkISize::Make(kGMDimension, kGMDimension); |
| 62 | } |
| 63 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 65 | for (int j = 0; j < 4; ++j) { |
| 66 | for (int i = 0; i < 4; ++i) { |
| 67 | SkString filename = GetResourcePath(get_astc_filename(j*4+i)); |
| 68 | if (filename == GetResourcePath("")) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str())); |
| 73 | if (NULL == fileData) { |
| 74 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | SkBitmap bm; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 79 | if (!SkInstallDiscardablePixelRef(fileData, &bm)) { |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 80 | SkDebugf("Could not install discardable pixel ref.\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | const SkScalar bmX = static_cast<SkScalar>(i*kBitmapDimension); |
| 85 | const SkScalar bmY = static_cast<SkScalar>(j*kBitmapDimension); |
| 86 | canvas->drawBitmap(bm, bmX, bmY); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | static const int kGMDimension = 600; |
| 93 | static const int kBitmapDimension = kGMDimension/4; |
| 94 | |
| 95 | typedef GM INHERITED; |
| 96 | }; |
| 97 | |
| 98 | } // namespace skiagm |
| 99 | |
| 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | DEF_GM( return SkNEW(skiagm::ASTCBitmapGM); ) |