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 "include/core/SkTypes.h"
9 #include "tests/Test.h"
10 #include "tools/Resources.h"
11
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkSurface.h"
15 #include "include/gpu/GrContext.h"
16 #include "src/core/SkReadBuffer.h"
17 #include "src/core/SkWriteBuffer.h"
18
check_isopaque(skiatest::Reporter * reporter,const sk_sp<SkSurface> & surface,bool expectedOpaque)19 static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
20 bool expectedOpaque) {
21 sk_sp<SkImage> image(surface->makeImageSnapshot());
22 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
23 }
24
DEF_TEST(ImageIsOpaqueTest,reporter)25 DEF_TEST(ImageIsOpaqueTest, reporter) {
26 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
27 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
28 check_isopaque(reporter, surfaceTransparent, false);
29
30 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
31 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
32 check_isopaque(reporter, surfaceOpaque, true);
33 }
34
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,reporter,ctxInfo)35 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
36 GrContext* context = ctxInfo.grContext();
37 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
38 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
39 check_isopaque(reporter, surfaceTransparent, false);
40
41 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
42 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
43
44 check_isopaque(reporter, surfaceOpaque, true);
45 }
46
47 ///////////////////////////////////////////////////////////////////////////////////////////////////
48 #include "include/core/SkPictureRecorder.h"
49
make_picture()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
DEF_TEST(Image_isAlphaOnly,reporter)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),
66 GetResourceAsImage("images/mandrill_128.png"),
67 GetResourceAsImage("images/color_wheel.jpg"),
68 SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
69 SkImage::BitDepth::kU8,
70 SkColorSpace::MakeSRGB()),
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 }
79