blob: 9a9cce5f78094733c9ab2ed4a31de77141aba0c1 [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);
Adlai Holler14dc7912020-08-11 15:48:49 +000017 auto dContext = factory.get(contextType);
18 if (!dContext) {
Adlai Holler872a32c2020-07-10 14:33:22 -040019 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:
Adlai Holler14dc7912020-08-11 15:48:49 +000034 auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext);
35 REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040036
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:
Adlai Holler14dc7912020-08-11 15:48:49 +000042 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii);
Adlai Holler872a32c2020-07-10 14:33:22 -040043 SkSurfaceCharacterization sc;
44 REPORTER_ASSERT(reporter, surf->characterize(&sc));
Adlai Holler14dc7912020-08-11 15:48:49 +000045 GrBackendTexture tex = dContext->createBackendTexture(sc);
46 auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin,
Adlai Holler872a32c2020-07-10 14:33:22 -040047 ii.colorType(), ii.alphaType(),
48 ii.refColorSpace());
Adlai Holler14dc7912020-08-11 15:48:49 +000049 REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040050
51 // gpu image + context:
Adlai Holler14dc7912020-08-11 15:48:49 +000052 auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext);
53 REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040054
55 // gpu image + nullptr:
56 REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr));
57
Adlai Holler14dc7912020-08-11 15:48:49 +000058 dContext->flush();
59 dContext->submit(true);
60 dContext->deleteBackendTexture(tex);
Adlai Holler872a32c2020-07-10 14:33:22 -040061 }
62}