Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4caa935

Browse files
Adlai-HollerSkia Commit-Bot
authored andcommitted
Migrate MakeTextureImage to take GrDirectContext
This is part of a larger effort to force SkImage users to specify the direct context they want to use when manipulating GPU images. Chrome CL 2299194 (landed) enables the staging flag. bug: skia:10466 Change-Id: I959db57dd8dca5c2622eb5ffaa7de161c4d6d8f0 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/302643 Commit-Queue: Brian Salomon <bsalomon@google.com> Auto-Submit: Adlai Holler <adlai@google.com> Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com>
1 parent 7479eda commit 4caa935

14 files changed

+137
-106
lines changed

bench/BulkRectBench.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,14 @@ class BulkRectBench : public Benchmark {
215215
// Push the skimages to the GPU when using the GPU backend so that the texture creation is
216216
// not part of the bench measurements. Always remake the images since they are so simple,
217217
// and since they are context-specific, this works when the bench runs multiple GPU backends
218-
auto context = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
219-
: nullptr;
218+
auto direct = GrAsDirectContext(canvas->recordingContext());
220219
for (int i = 0; i < kImageCount; ++i) {
221220
SkBitmap bm;
222221
bm.allocN32Pixels(256, 256);
223222
bm.eraseColor(fColors[i].toSkColor());
224223
auto image = SkImage::MakeFromBitmap(bm);
225224

226-
fImages[i] = context ? image->makeTextureImage(context) : std::move(image);
225+
fImages[i] = direct ? image->makeTextureImage(direct) : std::move(image);
227226
}
228227
}
229228

bench/ImageFilterDAGBench.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ class ImageMakeWithFilterDAGBench : public Benchmark {
7373
SkIPoint offset = SkIPoint::Make(0, 0);
7474
SkIRect discardSubset;
7575

76-
auto context = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
77-
: nullptr;
76+
auto direct = GrAsDirectContext(canvas->recordingContext());
7877
// makeWithFilter will only use the GPU backend if the image is already a texture
79-
sk_sp<SkImage> image = fImage->makeTextureImage(context);
78+
sk_sp<SkImage> image = fImage->makeTextureImage(direct);
8079
if (!image) {
8180
image = fImage;
8281
}

gm/bc1_transparency.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "gm/gm.h"
99
#include "include/core/SkCanvas.h"
1010
#include "include/core/SkImage.h"
11-
#include "include/gpu/GrContext.h"
11+
#include "include/gpu/GrDirectContext.h"
1212
#include "include/gpu/GrRecordingContext.h"
1313
#include "src/core/SkCompressedDataUtils.h"
1414
#include "src/gpu/GrCaps.h"
@@ -99,10 +99,10 @@ static sk_sp<SkData> make_compressed_data() {
9999
return tmp;
100100
}
101101

102-
static sk_sp<SkImage> data_to_img(GrContext *context, sk_sp<SkData> data,
102+
static sk_sp<SkImage> data_to_img(GrDirectContext *direct, sk_sp<SkData> data,
103103
SkImage::CompressionType compression) {
104-
if (context) {
105-
return SkImage::MakeTextureFromCompressed(context, std::move(data),
104+
if (direct) {
105+
return SkImage::MakeTextureFromCompressed(direct, std::move(data),
106106
kImgWidth,
107107
kImgHeight,
108108
compression,
@@ -180,16 +180,16 @@ class BC1TransparencyGM : public GM {
180180
}
181181

182182
void onDraw(SkCanvas* canvas) override {
183-
auto context = canvas->getGrContext();
183+
auto direct = GrAsDirectContext(canvas->recordingContext());
184184

185-
sk_sp<SkImage> rgbImg = data_to_img(context, fBC1Data,
185+
sk_sp<SkImage> rgbImg = data_to_img(direct, fBC1Data,
186186
SkImage::CompressionType::kBC1_RGB8_UNORM);
187187

188-
sk_sp<SkImage> rgbaImg = data_to_img(context, fBC1Data,
188+
sk_sp<SkImage> rgbaImg = data_to_img(direct, fBC1Data,
189189
SkImage::CompressionType::kBC1_RGBA8_UNORM);
190190

191-
draw_image(context, canvas, rgbImg, kPad, kPad);
192-
draw_image(context, canvas, rgbaImg, kPad, 2 * kPad + kImgHeight);
191+
draw_image(direct, canvas, rgbImg, kPad, kPad);
192+
draw_image(direct, canvas, rgbaImg, kPad, 2 * kPad + kImgHeight);
193193
}
194194

195195
private:

gm/compressed_textures.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "include/core/SkRefCnt.h"
2222
#include "include/core/SkSize.h"
2323
#include "include/core/SkString.h"
24-
#include "include/gpu/GrContext.h"
24+
#include "include/gpu/GrDirectContext.h"
2525
#include "include/gpu/GrRecordingContext.h"
2626
#include "src/core/SkCompressedDataUtils.h"
2727
#include "src/core/SkMipmap.h"
@@ -31,7 +31,6 @@
3131
#include "src/image/SkImage_Base.h"
3232
#include "third_party/etc1/etc1.h"
3333

34-
class GrContext;
3534
class GrRenderTargetContext;
3635

3736
static SkPoint gen_pt(float angle, const SkVector& scale) {
@@ -213,26 +212,26 @@ class CompressedTexturesGM : public skiagm::GM {
213212
}
214213

215214
void onDraw(SkCanvas* canvas) override {
216-
GrContext* context = canvas->getGrContext();
215+
auto direct = GrAsDirectContext(canvas->recordingContext());
217216

218-
this->drawCell(context, canvas, fOpaqueETC2Data,
217+
this->drawCell(direct, canvas, fOpaqueETC2Data,
219218
SkImage::CompressionType::kETC2_RGB8_UNORM, { kPad, kPad });
220219

221-
this->drawCell(context, canvas, fOpaqueBC1Data,
220+
this->drawCell(direct, canvas, fOpaqueBC1Data,
222221
SkImage::CompressionType::kBC1_RGB8_UNORM, { 2*kPad + kCellWidth, kPad });
223222

224-
this->drawCell(context, canvas, fTransparentBC1Data,
223+
this->drawCell(direct, canvas, fTransparentBC1Data,
225224
SkImage::CompressionType::kBC1_RGBA8_UNORM,
226225
{ 2*kPad + kCellWidth, 2*kPad + kBaseTexHeight });
227226
}
228227

229228
private:
230-
void drawCell(GrContext* context, SkCanvas* canvas, sk_sp<SkData> data,
229+
void drawCell(GrDirectContext* direct, SkCanvas* canvas, sk_sp<SkData> data,
231230
SkImage::CompressionType compression, SkIVector offset) {
232231

233232
sk_sp<SkImage> image;
234-
if (context) {
235-
image = SkImage::MakeTextureFromCompressed(context, std::move(data),
233+
if (direct) {
234+
image = SkImage::MakeTextureFromCompressed(direct, std::move(data),
236235
fImgDimensions.width(),
237236
fImgDimensions.height(),
238237
compression, GrMipMapped::kYes);
@@ -255,7 +254,7 @@ class CompressedTexturesGM : public skiagm::GM {
255254

256255
bool isCompressed = false;
257256
if (image->isTextureBacked()) {
258-
const GrCaps* caps = context->priv().caps();
257+
const GrCaps* caps = direct->priv().caps();
259258

260259
GrTextureProxy* proxy = as_IB(image)->peekProxy();
261260
isCompressed = caps->isFormatCompressed(proxy->backendFormat());

gm/exoticformats.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "include/core/SkCanvas.h"
1010
#include "include/core/SkImage.h"
1111
#include "include/core/SkStream.h"
12-
#include "include/gpu/GrContext.h"
12+
#include "include/gpu/GrDirectContext.h"
1313
#include "include/gpu/GrRecordingContext.h"
1414
#include "src/core/SkCompressedDataUtils.h"
1515
#include "src/core/SkMipmap.h"
@@ -301,9 +301,10 @@ static sk_sp<SkData> load_dds(const char* filename, ImageInfo* imageInfo) {
301301
}
302302

303303
//-------------------------------------------------------------------------------------------------
304-
static sk_sp<SkImage> data_to_img(GrContext *context, sk_sp<SkData> data, const ImageInfo& info) {
305-
if (context) {
306-
return SkImage::MakeTextureFromCompressed(context, std::move(data),
304+
static sk_sp<SkImage> data_to_img(GrDirectContext *direct, sk_sp<SkData> data,
305+
const ImageInfo& info) {
306+
if (direct) {
307+
return SkImage::MakeTextureFromCompressed(direct, std::move(data),
307308
info.fDim.fWidth,
308309
info.fDim.fHeight,
309310
info.fCompressionType,
@@ -335,7 +336,7 @@ class ExoticFormatsGM : public GM {
335336
return SkISize::Make(2*kImgWidthHeight + 3 * kPad, kImgWidthHeight + 2 * kPad);
336337
}
337338

338-
void loadImages(GrContext *context) {
339+
void loadImages(GrDirectContext *direct) {
339340

340341
if (!fETC1Image) {
341342
ImageInfo info;
@@ -345,7 +346,7 @@ class ExoticFormatsGM : public GM {
345346
SkASSERT(info.fMipMapped == GrMipMapped::kNo);
346347
SkASSERT(info.fCompressionType == SkImage::CompressionType::kETC2_RGB8_UNORM);
347348

348-
fETC1Image = data_to_img(context, std::move(data), info);
349+
fETC1Image = data_to_img(direct, std::move(data), info);
349350
} else {
350351
SkDebugf("failed to load flower-etc1.ktx\n");
351352
}
@@ -359,7 +360,7 @@ class ExoticFormatsGM : public GM {
359360
SkASSERT(info.fMipMapped == GrMipMapped::kNo);
360361
SkASSERT(info.fCompressionType == SkImage::CompressionType::kBC1_RGB8_UNORM);
361362

362-
fBC1Image = data_to_img(context, std::move(data), info);
363+
fBC1Image = data_to_img(direct, std::move(data), info);
363364
} else {
364365
SkDebugf("failed to load flower-bc1.dds\n");
365366
}
@@ -394,12 +395,18 @@ class ExoticFormatsGM : public GM {
394395
}
395396

396397
void onDraw(SkCanvas* canvas) override {
397-
GrContext* context = canvas->getGrContext();
398+
auto recording = canvas->recordingContext();
399+
auto direct = GrAsDirectContext(recording);
398400

399-
this->loadImages(context);
401+
// In DDL mode, these draws will be dropped.
402+
if (recording && !direct) {
403+
return;
404+
}
405+
406+
this->loadImages(direct);
400407

401-
this->drawImage(context, canvas, fETC1Image.get(), kPad, kPad);
402-
this->drawImage(context, canvas, fBC1Image.get(), kImgWidthHeight + 2 * kPad, kPad);
408+
this->drawImage(direct, canvas, fETC1Image.get(), kPad, kPad);
409+
this->drawImage(direct, canvas, fBC1Image.get(), kImgWidthHeight + 2 * kPad, kPad);
403410
}
404411

405412
private:

gm/flippity.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static const SkMatrix kUVMatrices[kNumMatrices] = {
8181

8282

8383
// Create a fixed size text label like "LL" or "LR".
84-
static sk_sp<SkImage> make_text_image(GrContext* context, const char* text, SkColor color) {
84+
static sk_sp<SkImage> make_text_image(GrDirectContext* direct, const char* text, SkColor color) {
8585
SkPaint paint;
8686
paint.setAntiAlias(true);
8787
paint.setColor(color);
@@ -107,7 +107,7 @@ static sk_sp<SkImage> make_text_image(GrContext* context, const char* text, SkCo
107107

108108
sk_sp<SkImage> image = surf->makeImageSnapshot();
109109

110-
return image->makeTextureImage(context);
110+
return image->makeTextureImage(direct);
111111
}
112112

113113
// Create an image with each corner marked w/ "LL", "LR", etc., with the origin either bottom-left
@@ -231,7 +231,7 @@ class FlippityGM : public skiagm::GpuGM {
231231
canvas->restore();
232232
}
233233

234-
void makeLabels(GrDirectContext* context) {
234+
void makeLabels(GrDirectContext* direct) {
235235
if (fLabels.count()) {
236236
return;
237237
}
@@ -246,7 +246,7 @@ class FlippityGM : public skiagm::GpuGM {
246246
};
247247

248248
for (int i = 0; i < kNumLabels; ++i) {
249-
fLabels.push_back(make_text_image(context, kLabelText[i], kLabelColors[i]));
249+
fLabels.push_back(make_text_image(direct, kLabelText[i], kLabelColors[i]));
250250
}
251251
SkASSERT(kNumLabels == fLabels.count());
252252
}

gm/imagefilters.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "include/effects/SkHighContrastFilter.h"
2929
#include "include/effects/SkImageFilters.h"
3030
#include "include/effects/SkShaderMaskFilter.h"
31+
#include "include/gpu/GrDirectContext.h"
3132
#include "tools/Resources.h"
3233
#include "tools/ToolUtils.h"
3334

@@ -200,9 +201,9 @@ DEF_GM(return new SaveLayerWithBackdropGM();)
200201
// normally be a sprite draw that could avoid an auto-saveLayer.
201202
DEF_SIMPLE_GM(imagefilters_effect_order, canvas, 512, 512) {
202203
sk_sp<SkImage> image(GetResourceAsImage("images/mandrill_256.png"));
203-
if (canvas->getGrContext()) {
204-
sk_sp<SkImage> gpuImage = image->makeTextureImage(canvas->getGrContext());
205-
if (gpuImage) {
204+
auto direct = GrAsDirectContext(canvas->recordingContext());
205+
if (direct) {
206+
if (sk_sp<SkImage> gpuImage = image->makeTextureImage(direct)) {
206207
image = std::move(gpuImage);
207208
}
208209
}

gm/imagemakewithfilter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "include/effects/SkImageFilters.h"
3030

31-
#include "include/gpu/GrContext.h"
31+
#include "include/gpu/GrDirectContext.h"
3232

3333
#include "tools/Resources.h"
3434
#include "tools/ToolUtils.h"
@@ -270,12 +270,12 @@ class ImageMakeWithFilterGM : public skiagm::GM {
270270
// These need to be GPU-backed when on the GPU to ensure that the image filters use the GPU
271271
// code paths (otherwise they may choose to do CPU filtering then upload)
272272
sk_sp<SkImage> mainImage, auxImage;
273-
if (canvas->recordingContext()) {
274-
if (canvas->recordingContext()->abandoned()) {
273+
if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
274+
if (direct->abandoned()) {
275275
return;
276276
}
277-
mainImage = fMainImage->makeTextureImage(canvas->getGrContext());
278-
auxImage = fAuxImage->makeTextureImage(canvas->getGrContext());
277+
mainImage = fMainImage->makeTextureImage(direct);
278+
auxImage = fAuxImage->makeTextureImage(direct);
279279
} else {
280280
mainImage = fMainImage;
281281
auxImage = fAuxImage;

gm/readpixels.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "include/core/SkStream.h"
2323
#include "include/core/SkString.h"
2424
#include "include/core/SkTypes.h"
25+
#include "include/gpu/GrDirectContext.h"
2526
#include "include/third_party/skcms/skcms.h"
2627
#include "tools/Resources.h"
2728

@@ -164,8 +165,8 @@ class ReadPixelsGM : public skiagm::GM {
164165
if (!image) {
165166
continue;
166167
}
167-
if (GrContext* context = canvas->getGrContext()) {
168-
image = image->makeTextureImage(context);
168+
if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
169+
image = image->makeTextureImage(direct);
169170
}
170171
if (image) {
171172
for (SkColorType dstColorType : colorTypes) {

include/core/SkImage.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,17 @@ class SK_API SkImage : public SkRefCnt {
252252
* If the encoded format is not supported, or subset is outside of the bounds of the decoded
253253
* image, nullptr is returned.
254254
*
255+
* @param direct the GrDirectContext in play
255256
* @param encoded the encoded data
256257
* @param length the number of bytes of encoded data
257258
* @param subset the bounds of the pixels within the decoded image to return. may be null.
258259
* @return created SkImage, or nullptr
259260
*/
260-
static sk_sp<SkImage> DecodeToTexture(GrContext* ctx, const void* encoded, size_t length,
261-
const SkIRect* subset = nullptr);
262-
static sk_sp<SkImage> DecodeToTexture(GrContext* ctx, const sk_sp<SkData>& data,
261+
static sk_sp<SkImage> DecodeToTexture(GrDirectContext* direct, const void* encoded,
262+
size_t length, const SkIRect* subset = nullptr);
263+
static sk_sp<SkImage> DecodeToTexture(GrDirectContext* direct, const sk_sp<SkData>& data,
263264
const SkIRect* subset = nullptr) {
264-
return DecodeToTexture(ctx, data->data(), data->size(), subset);
265+
return DecodeToTexture(direct, data->data(), data->size(), subset);
265266
}
266267

267268
/*
@@ -306,7 +307,7 @@ class SK_API SkImage : public SkRefCnt {
306307
@param isProtected do the contents of 'data' require DRM protection (on Vulkan)?
307308
@return created SkImage, or nullptr
308309
*/
309-
static sk_sp<SkImage> MakeTextureFromCompressed(GrContext* context,
310+
static sk_sp<SkImage> MakeTextureFromCompressed(GrDirectContext* direct,
310311
sk_sp<SkData> data,
311312
int width, int height,
312313
CompressionType type,
@@ -319,11 +320,7 @@ class SK_API SkImage : public SkRefCnt {
319320
int width, int height,
320321
CompressionType type,
321322
GrMipMapped mipMapped = GrMipMapped::kNo,
322-
GrProtected isProtected = GrProtected::kNo) {
323-
return MakeTextureFromCompressed(context, data, width, height, type,
324-
mipMapped, isProtected);
325-
326-
}
323+
GrProtected isProtected = GrProtected::kNo);
327324

328325
/** Creates a CPU-backed SkImage from compressed data.
329326
@@ -1224,17 +1221,23 @@ class SK_API SkImage : public SkRefCnt {
12241221
mipMapped is compatible with the backing GPU texture. SkBudgeted is ignored in this case.
12251222
12261223
Returns nullptr if context is nullptr, or if SkImage was created with another
1227-
GrContext.
1224+
GrDirectContext.
12281225
1229-
@param GrContext GPU context
1230-
@param GrMipMapped whether created SkImage texture must allocate mip map levels
1231-
@param SkBudgeted whether to count a newly created texture for the returned image
1232-
counts against the GrContext's budget.
1233-
@return created SkImage, or nullptr
1226+
@param GrDirectContext the GrDirectContext in play, if it exists
1227+
@param GrMipMapped whether created SkImage texture must allocate mip map levels
1228+
@param SkBudgeted whether to count a newly created texture for the returned image
1229+
counts against the GrContext's budget.
1230+
@return created SkImage, or nullptr
12341231
*/
1232+
#ifndef SK_IMAGE_MAKE_TEXTURE_IMAGE_ALLOW_GR_CONTEXT
1233+
sk_sp<SkImage> makeTextureImage(GrDirectContext*,
1234+
GrMipMapped = GrMipMapped::kNo,
1235+
SkBudgeted = SkBudgeted::kYes) const;
1236+
#else
12351237
sk_sp<SkImage> makeTextureImage(GrContext*,
12361238
GrMipMapped = GrMipMapped::kNo,
12371239
SkBudgeted = SkBudgeted::kYes) const;
1240+
#endif
12381241

12391242
/** Returns raster image or lazy image. Copies SkImage backed by GPU texture into
12401243
CPU memory if needed. Returns original SkImage if decoded in raster bitmap,

0 commit comments

Comments
 (0)