xref: /aosp_15_r20/external/skia/tests/DeviceTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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 "include/core/SkBitmap.h"
9 #include "include/core/SkImage.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSurfaceProps.h"
14 #include "include/core/SkTypes.h"
15 #include "include/gpu/GpuTypes.h"
16 #include "include/gpu/ganesh/GrDirectContext.h"
17 #include "include/gpu/ganesh/GrTypes.h"
18 #include "include/gpu/ganesh/SkImageGanesh.h"
19 #include "src/core/SkDevice.h"
20 #include "src/core/SkSpecialImage.h"
21 #include "src/gpu/SkBackingFit.h"
22 #include "src/gpu/ganesh/Device.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 
27 struct GrContextOptions;
28 
29 class DeviceTestingAccess {
30 public:
MakeSpecial(SkDevice * dev,const SkBitmap & bm)31     static sk_sp<SkSpecialImage> MakeSpecial(SkDevice* dev, const SkBitmap& bm) {
32         return dev->makeSpecial(bm);
33     }
34 
MakeSpecial(SkDevice * dev,SkImage * img)35     static sk_sp<SkSpecialImage> MakeSpecial(SkDevice* dev, SkImage* img) {
36         return dev->makeSpecial(img);
37     }
38 
SnapSpecial(SkDevice * dev)39     static sk_sp<SkSpecialImage> SnapSpecial(SkDevice* dev) {
40         return dev->snapSpecial();
41     }
42 };
43 
44 // TODO: re-enable this when Raster methods are implemented
45 #if 0
46 DEF_TEST(SpecialImage_BitmapDevice, reporter) {
47     static const int kWidth = 100;
48     static const int kHeight = 90;
49 
50     SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
51 
52     sk_sp<SkDevice> bmDev = SkBitmapDevice::Create(ii);
53 
54     SkBitmap bm;
55     bm.tryAllocN32Pixels(kWidth, kHeight);
56 
57     // Create a raster-backed special image from a raster-backed SkBitmap
58     sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(bmDev.get(), bm);
59     SkASSERT(!special->isTextureBacked());
60     SkASSERT(kWidth == special->width());
61     SkASSERT(kHeight == special->height());
62     SkASSERT(bm.getGenerationID() == special->uniqueID());
63     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
64 
65     // Create a raster-backed special image from a raster-backed SkImage
66     sk_sp<SkImage> image(SkImages::RasterFromBitmap(bm));
67     special = DeviceTestingAccess::MakeSpecial(bmDev.get(), image.get());
68     SkASSERT(!special->isTextureBacked());
69     SkASSERT(kWidth == special->width());
70     SkASSERT(kHeight == special->height());
71     SkASSERT(bm.getGenerationID() == special->uniqueID());
72     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
73 
74     // Snap the device as a raster-backed special image
75     special = DeviceTestingAccess::SnapSpecial(bmDev.get());
76     SkASSERT(!special->isTextureBacked());
77     SkASSERT(2*kWidth == special->width());
78     SkASSERT(2*kHeight == special->height());
79     SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
80 }
81 #endif
82 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)83 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SpecialImage_GPUDevice,
84                                        reporter,
85                                        ctxInfo,
86                                        CtsEnforcement::kApiLevel_T) {
87     auto dContext = ctxInfo.directContext();
88 
89     static const int kWidth = 100;
90     static const int kHeight = 90;
91 
92     SkImageInfo ii = SkImageInfo::MakeN32Premul(2*kWidth, 2*kHeight);
93 
94     auto device = dContext->priv().createDevice(skgpu::Budgeted::kNo,
95                                                 ii,
96                                                 SkBackingFit::kExact,
97                                                 1,
98                                                 skgpu::Mipmapped::kNo,
99                                                 GrProtected::kNo,
100                                                 kBottomLeft_GrSurfaceOrigin,
101                                                 SkSurfaceProps(),
102                                                 skgpu::ganesh::Device::InitContents::kClear);
103 
104     SkBitmap bm;
105     SkAssertResult(bm.tryAllocN32Pixels(kWidth, kHeight));
106 
107     // Create a gpu-backed special image from a raster-backed SkBitmap
108     sk_sp<SkSpecialImage> special = DeviceTestingAccess::MakeSpecial(device.get(), bm);
109     SkASSERT(special->isGaneshBacked());
110     SkASSERT(kWidth == special->width());
111     SkASSERT(kHeight == special->height());
112     SkASSERT(bm.getGenerationID() == special->uniqueID());
113     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
114 
115     // Create a gpu-backed special image from a raster-backed SkImage
116     sk_sp<SkImage> image(bm.asImage());
117     special = DeviceTestingAccess::MakeSpecial(device.get(), image.get());
118     SkASSERT(special->isGaneshBacked());
119     SkASSERT(kWidth == special->width());
120     SkASSERT(kHeight == special->height());
121     // TODO: Hmmm, this is a bit unexpected
122     SkASSERT(image->uniqueID() != special->uniqueID());
123     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
124 
125     // Create a gpu-backed special image from a gpu-backed SkImage
126     image = SkImages::TextureFromImage(dContext, image);
127     special = DeviceTestingAccess::MakeSpecial(device.get(), image.get());
128     SkASSERT(special->isGaneshBacked());
129     SkASSERT(kWidth == special->width());
130     SkASSERT(kHeight == special->height());
131     SkASSERT(image->uniqueID() == special->uniqueID());
132     SkASSERT(SkIRect::MakeWH(kWidth, kHeight) == special->subset());
133 
134     // Snap the device as a gpu-backed special image
135     special = DeviceTestingAccess::SnapSpecial(device.get());
136     SkASSERT(special->isGaneshBacked());
137     SkASSERT(2*kWidth == special->width());
138     SkASSERT(2*kHeight == special->height());
139     SkASSERT(SkIRect::MakeWH(2*kWidth, 2*kHeight) == special->subset());
140 }
141