blob: 42c9f99d38572eee8537988cbf942fc89d747a06 [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"
reed7c748852014-11-10 08:57:21 -08009#include "Test.h"
10
piotaixrd2a35222014-08-19 14:29:02 -070011#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080012#include "GrContext.h"
piotaixrd2a35222014-08-19 14:29:02 -070013#endif
14#include "SkImage.h"
15#include "SkSurface.h"
reed7c748852014-11-10 08:57:21 -080016#include "SkReadBuffer.h"
17#include "SkWriteBuffer.h"
piotaixrd2a35222014-08-19 14:29:02 -070018
reed7c748852014-11-10 08:57:21 -080019static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
reed7534cf12014-12-15 06:41:02 -080020 // just need a safe amount of storage, but ensure that it is 4-byte aligned.
21 int32_t storage[(sizeof(SkImageInfo)*2) / sizeof(int32_t)];
brianosmanfad98562016-05-04 11:06:28 -070022 SkBinaryWriteBuffer wb(storage, sizeof(storage));
reed7c748852014-11-10 08:57:21 -080023 info.flatten(wb);
24 SkASSERT(wb.bytesWritten() < sizeof(storage));
25
26 SkReadBuffer rb(storage, wb.bytesWritten());
reed7c748852014-11-10 08:57:21 -080027
28 // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
reeddabe5d32016-06-21 10:28:14 -070029 SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8);
reed7c748852014-11-10 08:57:21 -080030
31 info2.unflatten(rb);
32 REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());
msarettded0f262016-06-16 05:33:31 -070033
34 // FIXME (msarett):
35 // Support flatten/unflatten of SkColorSpace objects.
36 REPORTER_ASSERT(reporter, info.makeColorSpace(nullptr) == info2.makeColorSpace(nullptr));
reed7c748852014-11-10 08:57:21 -080037}
38
39DEF_TEST(ImageInfo_flattening, reporter) {
reeddabe5d32016-06-21 10:28:14 -070040 sk_sp<SkColorSpace> spaces[] = {
41 nullptr,
42 SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named),
43 SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named),
44 };
45
reed7c748852014-11-10 08:57:21 -080046 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
47 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
reeddabe5d32016-06-21 10:28:14 -070048 for (auto& cs : spaces) {
reed7c748852014-11-10 08:57:21 -080049 SkImageInfo info = SkImageInfo::Make(100, 200,
50 static_cast<SkColorType>(ct),
51 static_cast<SkAlphaType>(at),
reeddabe5d32016-06-21 10:28:14 -070052 cs);
reed7c748852014-11-10 08:57:21 -080053 test_flatten(reporter, info);
54 }
55 }
56 }
57}
piotaixrd2a35222014-08-19 14:29:02 -070058
reede8f30622016-03-23 18:59:25 -070059static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
60 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070061 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070062 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
63}
64
piotaixrd2a35222014-08-19 14:29:02 -070065DEF_TEST(ImageIsOpaqueTest, reporter) {
66 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070067 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070068 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070069
70 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070071 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070072 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070073}
74
75#if SK_SUPPORT_GPU
76
bsalomon758586c2016-04-06 14:02:39 -070077DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070078 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080079 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070080 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080081 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070082
kkinnunen15302832015-12-01 04:35:26 -080083 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070084 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -070085
kkinnunen15302832015-12-01 04:35:26 -080086 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070087}
88
89#endif