robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [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 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 10 | #include "SkData.h" |
| 11 | #include "SkDecodingImageGenerator.h" |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkOSFile.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /** |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 18 | * Test decoding an image from a PKM or KTX file and then |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 19 | * from compressed ETC1 data. |
| 20 | */ |
| 21 | class ETC1BitmapGM : public GM { |
| 22 | public: |
| 23 | ETC1BitmapGM() { } |
| 24 | virtual ~ETC1BitmapGM() { } |
| 25 | |
| 26 | protected: |
| 27 | virtual SkString onShortName() SK_OVERRIDE { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 28 | SkString str = SkString("etc1bitmap_"); |
| 29 | str.append(this->fileExtension()); |
| 30 | return str; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | virtual SkISize onISize() SK_OVERRIDE { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 34 | return make_isize(128, 128); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 35 | } |
| 36 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 37 | virtual SkString fileExtension() const = 0; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 38 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 39 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 40 | SkBitmap bm; |
| 41 | SkString filename = SkOSPath::SkPathJoin( |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 42 | INHERITED::gResourcePath.c_str(), "mandrill_128."); |
| 43 | filename.append(this->fileExtension()); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 44 | |
mtklein | 04cd368 | 2014-06-03 09:04:33 -0700 | [diff] [blame] | 45 | SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str())); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 46 | if (NULL == fileData) { |
| 47 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 48 | return; |
| 49 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 50 | |
| 51 | if (!SkInstallDiscardablePixelRef( |
| 52 | SkDecodingImageGenerator::Create( |
| 53 | fileData, SkDecodingImageGenerator::Options()), &bm)) { |
| 54 | SkDebugf("Could not install discardable pixel ref.\n"); |
| 55 | return; |
| 56 | } |
| 57 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 58 | canvas->drawBitmap(bm, 0, 0); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | typedef GM INHERITED; |
| 63 | }; |
| 64 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 65 | // This class specializes ETC1BitmapGM to load the mandrill_128.pkm file. |
| 66 | class ETC1Bitmap_PKM_GM : public ETC1BitmapGM { |
| 67 | public: |
| 68 | ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { } |
| 69 | virtual ~ETC1Bitmap_PKM_GM() { } |
| 70 | |
| 71 | protected: |
| 72 | |
| 73 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("pkm"); } |
| 74 | |
| 75 | private: |
| 76 | typedef ETC1BitmapGM INHERITED; |
| 77 | }; |
| 78 | |
| 79 | // This class specializes ETC1BitmapGM to load the mandrill_128.ktx file. |
| 80 | class ETC1Bitmap_KTX_GM : public ETC1BitmapGM { |
| 81 | public: |
| 82 | ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { } |
| 83 | virtual ~ETC1Bitmap_KTX_GM() { } |
| 84 | |
| 85 | protected: |
| 86 | |
| 87 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("ktx"); } |
| 88 | |
| 89 | private: |
| 90 | typedef ETC1BitmapGM INHERITED; |
| 91 | }; |
| 92 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 93 | } // namespace skiagm |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 97 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); ) |
| 98 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); ) |