blob: 55da002efec2939273fee89793ec7201288ca405 [file] [log] [blame]
Adlai Holler872a32c2020-07-10 14:33:22 -04001/*
2 * Copyright 2020 Google LLC
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/SkImage.h"
9#include "include/core/SkSurface.h"
10#include "include/core/SkSurfaceCharacterization.h"
11#include "tests/Test.h"
12
13DEF_GPUTEST(GrDDLImage_MakeSubset, reporter, options) {
14 sk_gpu_test::GrContextFactory factory(options);
15 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
16 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
17 auto direct = factory.get(contextType);
18 if (!direct) {
19 continue;
20 }
21 SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
22 SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
23
24 // Raster image:
25 SkBitmap bm;
26 bm.setInfo(ii);
27 bm.allocPixels();
28 bm.eraseColor(SK_ColorBLACK);
29 bm.setImmutable();
30 auto rasterImg = SkImage::MakeFromBitmap(bm);
31 REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
32
33 // raster + context:
34 auto subImg1 = rasterImg->makeSubset(subsetBounds, direct);
35 REPORTER_ASSERT(reporter, subImg1->isValid(direct));
36
37 // raster + no context:
38 auto subImg2 = rasterImg->makeSubset(subsetBounds, nullptr);
39 REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
40
41 // Texture image:
42 auto surf = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, ii);
43 SkSurfaceCharacterization sc;
44 REPORTER_ASSERT(reporter, surf->characterize(&sc));
45 GrBackendTexture tex = direct->createBackendTexture(sc);
46 auto gpuImage = SkImage::MakeFromTexture(direct, tex, kTopLeft_GrSurfaceOrigin,
47 ii.colorType(), ii.alphaType(),
48 ii.refColorSpace());
49 REPORTER_ASSERT(reporter, gpuImage->isValid(direct));
50
51 // gpu image + context:
52 auto subImg5 = gpuImage->makeSubset(subsetBounds, direct);
53 REPORTER_ASSERT(reporter, subImg5->isValid(direct));
54
55 // gpu image + nullptr:
56 REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr));
57
58 direct->flush();
59 direct->submit(true);
60 direct->deleteBackendTexture(tex);
61 }
62}