blob: 5bec2b059008d160a7c1a4c55703b79080d57792 [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
8#include "SkTypes.h"
msarettabbd6d52016-08-01 09:43:08 -07009#include "Resources.h"
reed7c748852014-11-10 08:57:21 -080010#include "Test.h"
11
piotaixrd2a35222014-08-19 14:29:02 -070012#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080013#include "GrContext.h"
piotaixrd2a35222014-08-19 14:29:02 -070014#endif
Mike Reedc090c642017-05-16 10:39:06 -040015#include "SkCanvas.h"
piotaixrd2a35222014-08-19 14:29:02 -070016#include "SkImage.h"
17#include "SkSurface.h"
reed7c748852014-11-10 08:57:21 -080018#include "SkReadBuffer.h"
19#include "SkWriteBuffer.h"
piotaixrd2a35222014-08-19 14:29:02 -070020
reede8f30622016-03-23 18:59:25 -070021static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
22 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070023 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070024 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
25}
26
piotaixrd2a35222014-08-19 14:29:02 -070027DEF_TEST(ImageIsOpaqueTest, reporter) {
28 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070029 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070030 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070031
32 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070033 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070034 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070035}
36
37#if SK_SUPPORT_GPU
38
egdanielab527a52016-06-28 08:07:26 -070039DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070040 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080041 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070042 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080043 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070044
kkinnunen15302832015-12-01 04:35:26 -080045 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070046 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -070047
kkinnunen15302832015-12-01 04:35:26 -080048 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070049}
50
51#endif
reed9e2ed832016-10-31 05:27:28 -070052
53///////////////////////////////////////////////////////////////////////////////////////////////////
54#include "SkPictureRecorder.h"
55
56static sk_sp<SkPicture> make_picture() {
57 SkPictureRecorder recorder;
58 SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 });
59 canvas->drawColor(SK_ColorRED);
60 return recorder.finishRecordingAsPicture();
61}
62
63DEF_TEST(Image_isAlphaOnly, reporter) {
64 SkPMColor pmColors = 0;
65 SkPixmap pmap = {
66 SkImageInfo::MakeN32Premul(1, 1),
67 &pmColors,
68 sizeof(pmColors)
69 };
70 for (auto& image : {
71 SkImage::MakeRasterCopy(pmap),
Hal Canaryc465d132017-12-08 10:21:31 -050072 GetResourceAsImage("images/mandrill_128.png"),
73 GetResourceAsImage("images/color_wheel.jpg"),
Brian Osman138ea972016-12-16 11:55:18 -050074 SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050075 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050076 SkColorSpace::MakeSRGB()),
reed9e2ed832016-10-31 05:27:28 -070077 })
78 {
79 REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
80 }
81
82 REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({
83 SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true);
84}