1 /*
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/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSurface.h"
17 #include "include/core/SkTypes.h"
18 #include "include/gpu/GpuTypes.h"
19 #include "include/gpu/ganesh/GrBackendSurface.h"
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/GrTypes.h"
22 #include "include/gpu/ganesh/SkImageGanesh.h"
23 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
24 #include "include/private/chromium/GrSurfaceCharacterization.h"
25 #include "src/gpu/ganesh/GrDirectContextPriv.h"
26 #include "tests/CtsEnforcement.h"
27 #include "tests/Test.h"
28 #include "tools/gpu/ContextType.h"
29
30 class GrRecordingContext;
31 struct GrContextOptions;
32
DEF_GANESH_TEST(GrDDLImage_MakeSubset,reporter,options,CtsEnforcement::kApiLevel_T)33 DEF_GANESH_TEST(GrDDLImage_MakeSubset, reporter, options, CtsEnforcement::kApiLevel_T) {
34 using namespace skgpu;
35
36 sk_gpu_test::GrContextFactory factory(options);
37 for (int ct = 0; ct < skgpu::kContextTypeCount; ++ct) {
38 auto contextType = static_cast<skgpu::ContextType>(ct);
39 auto dContext = factory.get(contextType);
40 if (!dContext) {
41 continue;
42 }
43
44 Protected isProtected = Protected(dContext->priv().caps()->supportsProtectedContent());
45
46 SkIRect subsetBounds = SkIRect::MakeLTRB(4,4,8,8);
47 SkImageInfo ii = SkImageInfo::Make(16, 16, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
48
49 // Raster image:
50 SkBitmap bm;
51 bm.setInfo(ii);
52 bm.allocPixels();
53 bm.eraseColor(SK_ColorBLACK);
54 bm.setImmutable();
55 auto rasterImg = bm.asImage();
56 REPORTER_ASSERT(reporter, rasterImg->isValid(static_cast<GrRecordingContext*>(nullptr)));
57
58 // raster + context:
59 auto subImg1 = rasterImg->makeSubset(dContext, subsetBounds);
60 REPORTER_ASSERT(reporter, subImg1->isValid(dContext));
61
62 // raster + no context:
63 auto subImg2 = rasterImg->makeSubset(nullptr, subsetBounds);
64 REPORTER_ASSERT(reporter, subImg2->isValid(static_cast<GrRecordingContext*>(nullptr)));
65
66 // Texture image:
67 auto surf = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, ii);
68 GrSurfaceCharacterization sc;
69 REPORTER_ASSERT(reporter, surf->characterize(&sc));
70 GrBackendTexture tex = dContext->createBackendTexture(ii.width(),
71 ii.height(),
72 ii.colorType(),
73 skgpu::Mipmapped(sc.isMipMapped()),
74 GrRenderable::kYes,
75 isProtected);
76 auto gpuImage = SkImages::BorrowTextureFrom(dContext,
77 tex,
78 kTopLeft_GrSurfaceOrigin,
79 ii.colorType(),
80 ii.alphaType(),
81 ii.refColorSpace());
82 REPORTER_ASSERT(reporter, gpuImage->isValid(dContext));
83
84 // gpu image + context:
85 auto subImg5 = gpuImage->makeSubset(dContext, subsetBounds);
86 REPORTER_ASSERT(reporter, subImg5->isValid(dContext));
87
88 // gpu image + nullptr:
89 REPORTER_ASSERT(reporter, !gpuImage->makeSubset(nullptr, subsetBounds));
90
91 dContext->flush();
92 dContext->submit(GrSyncCpu::kYes);
93 dContext->deleteBackendTexture(tex);
94 }
95 }
96