ImageIsOpaqueTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "include/core/SkTypes.h"
  8. #include "tests/Test.h"
  9. #include "tools/Resources.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkSurface.h"
  13. #include "include/gpu/GrContext.h"
  14. #include "src/core/SkReadBuffer.h"
  15. #include "src/core/SkWriteBuffer.h"
  16. static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
  17. bool expectedOpaque) {
  18. sk_sp<SkImage> image(surface->makeImageSnapshot());
  19. REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
  20. }
  21. DEF_TEST(ImageIsOpaqueTest, reporter) {
  22. SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
  23. auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
  24. check_isopaque(reporter, surfaceTransparent, false);
  25. SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
  26. auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
  27. check_isopaque(reporter, surfaceOpaque, true);
  28. }
  29. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
  30. GrContext* context = ctxInfo.grContext();
  31. SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
  32. auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
  33. check_isopaque(reporter, surfaceTransparent, false);
  34. SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
  35. auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
  36. check_isopaque(reporter, surfaceOpaque, true);
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////////////////////////
  39. #include "include/core/SkPictureRecorder.h"
  40. static sk_sp<SkPicture> make_picture() {
  41. SkPictureRecorder recorder;
  42. SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 });
  43. canvas->drawColor(SK_ColorRED);
  44. return recorder.finishRecordingAsPicture();
  45. }
  46. DEF_TEST(Image_isAlphaOnly, reporter) {
  47. SkPMColor pmColors = 0;
  48. SkPixmap pmap = {
  49. SkImageInfo::MakeN32Premul(1, 1),
  50. &pmColors,
  51. sizeof(pmColors)
  52. };
  53. for (auto& image : {
  54. SkImage::MakeRasterCopy(pmap),
  55. GetResourceAsImage("images/mandrill_128.png"),
  56. GetResourceAsImage("images/color_wheel.jpg"),
  57. SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
  58. SkImage::BitDepth::kU8,
  59. SkColorSpace::MakeSRGB()),
  60. })
  61. {
  62. REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
  63. }
  64. REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({
  65. SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true);
  66. }