blob: b26ee5b78058516d07e650cb50fcdcddc32d0cca [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
Mike Reedac9f0c92020-12-23 10:11:33 -05008#include "include/core/SkBitmap.h"
Adlai Holler872a32c2020-07-10 14:33:22 -04009#include "include/core/SkImage.h"
10#include "include/core/SkSurface.h"
11#include "include/core/SkSurfaceCharacterization.h"
12#include "tests/Test.h"
13
14DEF_GPUTEST(GrDDLImage_MakeSubset, reporter, options) {
15 sk_gpu_test::GrContextFactory factory(options);
16 for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
17 auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
Adlai Holler14dc7912020-08-11 15:48:49 +000018 auto dContext = factory.get(contextType);
19 if (!dContext) {
Adlai Holler872a32c2020-07-10 14:33:22 -040020 continue;
21 }
22 SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
23 SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
24
25 // Raster image:
26 SkBitmap bm;
27 bm.setInfo(ii);
28 bm.allocPixels();
29 bm.eraseColor(SK_ColorBLACK);
30 bm.setImmutable();
Mike Reeddc607e32020-12-23 11:50:36 -050031 auto rasterImg = bm.asImage();
Adlai Holler872a32c2020-07-10 14:33:22 -040032 REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
33
34 // raster + context:
Adlai Holler14dc7912020-08-11 15:48:49 +000035 auto subImg1 = rasterImg->makeSubset(subsetBounds, dContext);
36 REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040037
38 // raster + no context:
39 auto subImg2 = rasterImg->makeSubset(subsetBounds, nullptr);
40 REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
41
42 // Texture image:
Adlai Holler14dc7912020-08-11 15:48:49 +000043 auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii);
Adlai Holler872a32c2020-07-10 14:33:22 -040044 SkSurfaceCharacterization sc;
45 REPORTER_ASSERT(reporter, surf->characterize(&sc));
Greg Daniel398ecf12020-08-26 16:33:54 -040046 GrBackendTexture tex =
47 dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
48 GrMipmapped(sc.isMipMapped()), GrRenderable::kYes);
Adlai Holler14dc7912020-08-11 15:48:49 +000049 auto gpuImage = SkImage::MakeFromTexture(dContext, tex, kTopLeft_GrSurfaceOrigin,
Adlai Holler872a32c2020-07-10 14:33:22 -040050 ii.colorType(), ii.alphaType(),
51 ii.refColorSpace());
Adlai Holler14dc7912020-08-11 15:48:49 +000052 REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040053
54 // gpu image + context:
Adlai Holler14dc7912020-08-11 15:48:49 +000055 auto subImg5 = gpuImage->makeSubset(subsetBounds, dContext);
56 REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
Adlai Holler872a32c2020-07-10 14:33:22 -040057
58 // gpu image + nullptr:
59 REPORTER_ASSERT(reporter, !gpuImage->makeSubset(subsetBounds, nullptr));
60
Adlai Holler14dc7912020-08-11 15:48:49 +000061 dContext->flush();
62 dContext->submit(true);
63 dContext->deleteBackendTexture(tex);
Adlai Holler872a32c2020-07-10 14:33:22 -040064 }
65}