piotaixr | d2a3522 | 2014-08-19 14:29:02 -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 "SkTypes.h" |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 10 | #include "Test.h" |
| 11 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 12 | #include "GrContext.h" |
Mike Reed | c090c64 | 2017-05-16 10:39:06 -0400 | [diff] [blame] | 13 | #include "SkCanvas.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 14 | #include "SkImage.h" |
| 15 | #include "SkSurface.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 16 | #include "SkReadBuffer.h" |
| 17 | #include "SkWriteBuffer.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 18 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 19 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 20 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 21 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 22 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 23 | } |
| 24 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 25 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 26 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 27 | auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 28 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 29 | |
| 30 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 31 | auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 32 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 33 | } |
| 34 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 35 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 36 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 37 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 38 | auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 39 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 40 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 41 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 42 | auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 43 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 44 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 45 | } |
| 46 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 48 | #include "SkPictureRecorder.h" |
| 49 | |
| 50 | static sk_sp<SkPicture> make_picture() { |
| 51 | SkPictureRecorder recorder; |
| 52 | SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 }); |
| 53 | canvas->drawColor(SK_ColorRED); |
| 54 | return recorder.finishRecordingAsPicture(); |
| 55 | } |
| 56 | |
| 57 | DEF_TEST(Image_isAlphaOnly, reporter) { |
| 58 | SkPMColor pmColors = 0; |
| 59 | SkPixmap pmap = { |
| 60 | SkImageInfo::MakeN32Premul(1, 1), |
| 61 | &pmColors, |
| 62 | sizeof(pmColors) |
| 63 | }; |
| 64 | for (auto& image : { |
| 65 | SkImage::MakeRasterCopy(pmap), |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 66 | GetResourceAsImage("images/mandrill_128.png"), |
| 67 | GetResourceAsImage("images/color_wheel.jpg"), |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 68 | SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 69 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 70 | SkColorSpace::MakeSRGB()), |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 71 | }) |
| 72 | { |
| 73 | REPORTER_ASSERT(reporter, image->isAlphaOnly() == false); |
| 74 | } |
| 75 | |
| 76 | REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({ |
| 77 | SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true); |
| 78 | } |