blob: d7fced9554e0619d7b5c421b84b7f15fe36c7809 [file] [log] [blame]
piotaixrd2a35222014-08-19 14:29:02 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
9#include "tests/Test.h"
10#include "tools/Resources.h"
reed7c748852014-11-10 08:57:21 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
piotaixrd2a35222014-08-19 14:29:02 -070018
reede8f30622016-03-23 18:59:25 -070019static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
20 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070021 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070022 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
23}
24
piotaixrd2a35222014-08-19 14:29:02 -070025DEF_TEST(ImageIsOpaqueTest, reporter) {
26 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070027 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070028 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070029
30 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070031 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070032 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070033}
34
egdanielab527a52016-06-28 08:07:26 -070035DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070036 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080037 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070038 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080039 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070040
kkinnunen15302832015-12-01 04:35:26 -080041 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070042 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -070043
kkinnunen15302832015-12-01 04:35:26 -080044 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070045}
46
reed9e2ed832016-10-31 05:27:28 -070047///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "include/core/SkPictureRecorder.h"
reed9e2ed832016-10-31 05:27:28 -070049
50static 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
57DEF_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 Canaryc465d132017-12-08 10:21:31 -050066 GetResourceAsImage("images/mandrill_128.png"),
67 GetResourceAsImage("images/color_wheel.jpg"),
Brian Osman138ea972016-12-16 11:55:18 -050068 SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050069 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050070 SkColorSpace::MakeSRGB()),
reed9e2ed832016-10-31 05:27:28 -070071 })
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}