etc1.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "include/core/SkTypes.h" // IWYU pragma: keep
  8. #if !defined(SK_BUILD_FOR_GOOGLE3)
  9. #include "gm/gm.h"
  10. #include "include/core/SkBitmap.h"
  11. #include "include/core/SkCanvas.h"
  12. #include "include/core/SkColor.h"
  13. #include "include/core/SkData.h"
  14. #include "include/core/SkImage.h"
  15. #include "include/core/SkImageInfo.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "third_party/etc1/etc1.h"
  21. class GrContext;
  22. class GrRenderTargetContext;
  23. // Basic test of Ganesh's ETC1 support
  24. class ETC1GM : public skiagm::GpuGM {
  25. public:
  26. ETC1GM() {
  27. this->setBGColor(0xFFCCCCCC);
  28. }
  29. protected:
  30. SkString onShortName() override {
  31. return SkString("etc1");
  32. }
  33. SkISize onISize() override {
  34. return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
  35. }
  36. void onOnceBeforeDraw() override {
  37. SkBitmap bm;
  38. SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
  39. kOpaque_SkAlphaType);
  40. bm.allocPixels(ii);
  41. bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
  42. for (int y = 0; y < kTexHeight; y += 4) {
  43. for (int x = 0; x < kTexWidth; x += 4) {
  44. bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
  45. }
  46. }
  47. int size = etc1_get_encoded_data_size(bm.width(), bm.height());
  48. fETC1Data = SkData::MakeUninitialized(size);
  49. unsigned char* pixels = (unsigned char*) fETC1Data->writable_data();
  50. if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
  51. bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
  52. fETC1Data = nullptr;
  53. }
  54. }
  55. void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
  56. sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data,
  57. kTexWidth, kTexHeight,
  58. SkImage::kETC1_CompressionType);
  59. canvas->drawImage(image, 0, 0);
  60. }
  61. private:
  62. static const int kPad = 8;
  63. static const int kTexWidth = 16;
  64. static const int kTexHeight = 20;
  65. sk_sp<SkData> fETC1Data;
  66. typedef GM INHERITED;
  67. };
  68. //////////////////////////////////////////////////////////////////////////////
  69. DEF_GM(return new ETC1GM;)
  70. #endif