| 1 |
/* |
| 2 |
* Copyright 2015 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageGenerator.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkMatrix.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkPicture.h" |
| 19 |
#include "include/core/SkPictureRecorder.h" |
| 20 |
#include "include/core/SkPoint.h" |
| 21 |
#include "include/core/SkRect.h" |
| 22 |
#include "include/core/SkRefCnt.h" |
| 23 |
#include "include/core/SkScalar.h" |
| 24 |
#include "include/core/SkSize.h" |
| 25 |
#include "include/core/SkString.h" |
| 26 |
#include "include/core/SkSurface.h" |
| 27 |
#include "include/core/SkTypes.h" |
| 28 |
#include "src/image/SkImageGeneratorPriv.h" |
| 29 |
#include "src/image/SkImage_Base.h" |
| 30 |
|
| 31 |
#if defined(SK_GANESH) |
| 32 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 33 |
#include "include/gpu/ganesh/GrExternalTextureGenerator.h" |
| 34 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 35 |
#include "include/gpu/ganesh/GrTypes.h" |
| 36 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 37 |
#include "include/private/gpu/ganesh/GrTextureGenerator.h" |
| 38 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 39 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 40 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 41 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 42 |
#include "src/gpu/ganesh/SurfaceContext.h" |
| 43 |
#include "src/gpu/ganesh/image/GrImageUtils.h" |
| 44 |
#include "src/gpu/ganesh/image/SkImage_Ganesh.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
#if defined(SK_GRAPHITE) |
| 48 |
#include "include/gpu/graphite/Surface.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#include <memory> |
| 52 |
#include <utility> |
| 53 |
|
| 54 |
class GrRecordingContext; |
| 55 |
|
| 56 |
static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
| 57 |
SkPaint paint; |
| 58 |
paint.setAntiAlias(true); |
| 59 |
paint.setColor(SK_ColorRED); |
| 60 |
paint.setStyle(SkPaint::kStroke_Style); |
| 61 |
paint.setStrokeWidth(10); |
| 62 |
canvas->drawRect(bounds, paint); |
| 63 |
paint.setStyle(SkPaint::kFill_Style); |
| 64 |
paint.setColor(SK_ColorBLUE); |
| 65 |
canvas->drawOval(bounds, paint); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Exercise drawing pictures inside an image, showing that the image version is pixelated |
| 70 |
* (correctly) when it is inside an image. |
| 71 |
*/ |
| 72 |
class ImagePictGM : public skiagm::GM { |
| 73 |
sk_sp<SkPicture> fPicture; |
| 74 |
sk_sp<SkImage> fImage0; |
| 75 |
sk_sp<SkImage> fImage1; |
| 76 |
public: |
| 77 |
ImagePictGM() {} |
| 78 |
|
| 79 |
protected: |
| 80 |
SkString getName() const override { return SkString("image-picture"); } |
| 81 |
|
| 82 |
SkISize getISize() override { return SkISize::Make(850, 450); } |
| 83 |
|
| 84 |
void onOnceBeforeDraw() override { |
| 85 |
const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 86 |
SkPictureRecorder recorder; |
| 87 |
draw_something(recorder.beginRecording(bounds), bounds); |
| 88 |
fPicture = recorder.finishRecordingAsPicture(); |
| 89 |
|
| 90 |
// extract enough just for the oval. |
| 91 |
const SkISize size = SkISize::Make(100, 100); |
| 92 |
auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
| 93 |
|
| 94 |
SkMatrix matrix; |
| 95 |
matrix.setTranslate(-100, -100); |
| 96 |
fImage0 = SkImages::DeferredFromPicture( |
| 97 |
fPicture, size, &matrix, nullptr, SkImages::BitDepth::kU8, srgbColorSpace); |
| 98 |
matrix.postTranslate(-50, -50); |
| 99 |
matrix.postRotate(45); |
| 100 |
matrix.postTranslate(50, 50); |
| 101 |
fImage1 = SkImages::DeferredFromPicture( |
| 102 |
fPicture, size, &matrix, nullptr, SkImages::BitDepth::kU8, srgbColorSpace); |
| 103 |
} |
| 104 |
|
| 105 |
void drawSet(SkCanvas* canvas) const { |
| 106 |
SkMatrix matrix = SkMatrix::Translate(-100, -100); |
| 107 |
canvas->drawPicture(fPicture, &matrix, nullptr); |
| 108 |
canvas->drawImage(fImage0.get(), 150, 0); |
| 109 |
canvas->drawImage(fImage1.get(), 300, 0); |
| 110 |
} |
| 111 |
|
| 112 |
void onDraw(SkCanvas* canvas) override { |
| 113 |
canvas->translate(20, 20); |
| 114 |
|
| 115 |
this->drawSet(canvas); |
| 116 |
|
| 117 |
canvas->save(); |
| 118 |
canvas->translate(0, 130); |
| 119 |
canvas->scale(0.25f, 0.25f); |
| 120 |
this->drawSet(canvas); |
| 121 |
canvas->restore(); |
| 122 |
|
| 123 |
canvas->save(); |
| 124 |
canvas->translate(0, 200); |
| 125 |
canvas->scale(2, 2); |
| 126 |
this->drawSet(canvas); |
| 127 |
canvas->restore(); |
| 128 |
} |
| 129 |
|
| 130 |
private: |
| 131 |
using INHERITED = skiagm::GM; |
| 132 |
}; |
| 133 |
DEF_GM( return new ImagePictGM; ) |
| 134 |
|
| 135 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 136 |
|
| 137 |
static std::unique_ptr<SkImageGenerator> make_pic_generator(SkCanvas*, |
| 138 |
sk_sp<SkPicture> pic) { |
| 139 |
SkMatrix matrix; |
| 140 |
matrix.setTranslate(-100, -100); |
| 141 |
return SkImageGenerators::MakeFromPicture({100, 100}, |
| 142 |
std::move(pic), |
| 143 |
&matrix, |
| 144 |
nullptr, |
| 145 |
SkImages::BitDepth::kU8, |
| 146 |
SkColorSpace::MakeSRGB()); |
| 147 |
} |
| 148 |
|
| 149 |
class RasterGenerator : public SkImageGenerator { |
| 150 |
public: |
| 151 |
RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) |
| 152 |
{} |
| 153 |
|
| 154 |
protected: |
| 155 |
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 156 |
const Options&) override { |
| 157 |
SkASSERT(fBM.width() == info.width()); |
| 158 |
SkASSERT(fBM.height() == info.height()); |
| 159 |
return fBM.readPixels(info, pixels, rowBytes, 0, 0); |
| 160 |
} |
| 161 |
private: |
| 162 |
SkBitmap fBM; |
| 163 |
}; |
| 164 |
static std::unique_ptr<SkImageGenerator> make_ras_generator(SkCanvas*, |
| 165 |
sk_sp<SkPicture> pic) { |
| 166 |
SkBitmap bm; |
| 167 |
bm.allocN32Pixels(100, 100); |
| 168 |
SkCanvas canvas(bm); |
| 169 |
canvas.clear(0); |
| 170 |
canvas.translate(-100, -100); |
| 171 |
canvas.drawPicture(pic); |
| 172 |
return std::make_unique<RasterGenerator>(bm); |
| 173 |
} |
| 174 |
|
| 175 |
#if defined(SK_GANESH) |
| 176 |
class TextureGenerator : public GrTextureGenerator { |
| 177 |
public: |
| 178 |
TextureGenerator(SkCanvas* canvas, const SkImageInfo& info, sk_sp<SkPicture> pic) |
| 179 |
: GrTextureGenerator(info) { |
| 180 |
|
| 181 |
fRContext = sk_ref_sp(canvas->recordingContext()); |
| 182 |
|
| 183 |
sk_sp<SkSurface> surface; |
| 184 |
|
| 185 |
if (fRContext) { |
| 186 |
surface = SkSurfaces::RenderTarget(fRContext.get(), |
| 187 |
skgpu::Budgeted::kYes, |
| 188 |
info, |
| 189 |
0, |
| 190 |
kTopLeft_GrSurfaceOrigin, |
| 191 |
nullptr); |
| 192 |
} |
| 193 |
#if defined(SK_GRAPHITE) |
| 194 |
if (skgpu::graphite::Recorder* recorder = canvas->recorder()) { |
| 195 |
surface = SkSurfaces::RenderTarget(recorder, info); |
| 196 |
} |
| 197 |
#endif |
| 198 |
|
| 199 |
if (surface) { |
| 200 |
surface->getCanvas()->clear(0); |
| 201 |
surface->getCanvas()->translate(-100, -100); |
| 202 |
surface->getCanvas()->drawPicture(pic); |
| 203 |
fImage = surface->makeImageSnapshot(); |
| 204 |
} |
| 205 |
} |
| 206 |
protected: |
| 207 |
GrSurfaceProxyView onGenerateTexture(GrRecordingContext* rContext, |
| 208 |
const SkImageInfo& info, |
| 209 |
skgpu::Mipmapped mipmapped, |
| 210 |
GrImageTexGenPolicy policy) override { |
| 211 |
SkASSERT(rContext); |
| 212 |
SkASSERT(rContext->priv().matches(fRContext.get())); |
| 213 |
|
| 214 |
// We are immediately copying the view so no need to pass in targetSurface |
| 215 |
auto [view, _] = skgpu::ganesh::AsView(rContext, fImage, skgpu::Mipmapped::kNo, |
| 216 |
/*targetSurface=*/nullptr); |
| 217 |
if (!view) { |
| 218 |
return {}; |
| 219 |
} |
| 220 |
|
| 221 |
SkASSERT_RELEASE(info.dimensions() == view.proxy()->dimensions()); |
| 222 |
|
| 223 |
if (policy == GrImageTexGenPolicy::kDraw) { |
| 224 |
return view; |
| 225 |
} |
| 226 |
auto budgeted = policy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 227 |
? skgpu::Budgeted::kNo |
| 228 |
: skgpu::Budgeted::kYes; |
| 229 |
return GrSurfaceProxyView::Copy( |
| 230 |
fRContext.get(), |
| 231 |
view, |
| 232 |
mipmapped, |
| 233 |
SkIRect::MakeWH(info.width(), info.height()), |
| 234 |
SkBackingFit::kExact, |
| 235 |
budgeted, |
| 236 |
/*label=*/"SurfaceProxyView_GenerateTexture"); |
| 237 |
} |
| 238 |
|
| 239 |
private: |
| 240 |
sk_sp<GrRecordingContext> fRContext; |
| 241 |
sk_sp<SkImage> fImage; |
| 242 |
}; |
| 243 |
|
| 244 |
static std::unique_ptr<SkImageGenerator> make_tex_generator(SkCanvas* canvas, |
| 245 |
sk_sp<SkPicture> pic) { |
| 246 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 247 |
if (!dContext && !canvas->recorder()) { |
| 248 |
return nullptr; |
| 249 |
} |
| 250 |
|
| 251 |
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 252 |
|
| 253 |
return std::make_unique<TextureGenerator>(canvas, info, pic); |
| 254 |
} |
| 255 |
#endif // SK_GANESH |
| 256 |
|
| 257 |
class ImageCacheratorGM : public skiagm::GM { |
| 258 |
typedef std::unique_ptr<SkImageGenerator> (*FactoryFunc)(SkCanvas*, sk_sp<SkPicture>); |
| 259 |
|
| 260 |
SkString fName; |
| 261 |
FactoryFunc fFactory; |
| 262 |
sk_sp<SkPicture> fPicture; |
| 263 |
sk_sp<SkImage> fImage; |
| 264 |
sk_sp<SkImage> fImageSubset; |
| 265 |
bool fUseTexture; |
| 266 |
|
| 267 |
public: |
| 268 |
ImageCacheratorGM(const char suffix[], FactoryFunc factory, bool useTexture) : |
| 269 |
fFactory(factory), fUseTexture(useTexture) { |
| 270 |
fName.printf("image-cacherator-from-%s", suffix); |
| 271 |
} |
| 272 |
|
| 273 |
protected: |
| 274 |
SkString getName() const override { return fName; } |
| 275 |
|
| 276 |
SkISize getISize() override { return SkISize::Make(960, 450); } |
| 277 |
|
| 278 |
void onOnceBeforeDraw() override { |
| 279 |
const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 280 |
SkPictureRecorder recorder; |
| 281 |
draw_something(recorder.beginRecording(bounds), bounds); |
| 282 |
fPicture = recorder.finishRecordingAsPicture(); |
| 283 |
} |
| 284 |
|
| 285 |
bool makeCaches(SkCanvas* canvas) { |
| 286 |
{ |
| 287 |
auto gen = fFactory(canvas, fPicture); |
| 288 |
if (!gen) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
#if defined(SK_GANESH) |
| 292 |
if (fUseTexture) { |
| 293 |
auto textureGen = std::unique_ptr<GrTextureGenerator>( |
| 294 |
static_cast<GrTextureGenerator*>(gen.release())); |
| 295 |
fImage = SkImages::DeferredFromTextureGenerator(std::move(textureGen)); |
| 296 |
} else |
| 297 |
#endif |
| 298 |
{ |
| 299 |
SkASSERT(!fUseTexture); |
| 300 |
fImage = SkImages::DeferredFromGenerator(std::move(gen)); |
| 301 |
} |
| 302 |
if (!fImage) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
SkASSERT(fImage->dimensions() == SkISize::Make(100, 100)); |
| 306 |
} |
| 307 |
|
| 308 |
{ |
| 309 |
const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100); |
| 310 |
|
| 311 |
// We re-create the generator here on the off chance that making a subset from |
| 312 |
// 'fImage' might perturb its state. |
| 313 |
auto gen = fFactory(canvas, fPicture); |
| 314 |
if (!gen) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
auto recorder = canvas->baseRecorder(); |
| 319 |
#if defined(SK_GANESH) |
| 320 |
if (fUseTexture) { |
| 321 |
auto textureGen = std::unique_ptr<GrTextureGenerator>( |
| 322 |
static_cast<GrTextureGenerator*>(gen.release())); |
| 323 |
fImageSubset = SkImages::DeferredFromTextureGenerator(std::move(textureGen)) |
| 324 |
->makeSubset(recorder, subset, {}); |
| 325 |
} else |
| 326 |
#endif |
| 327 |
{ |
| 328 |
SkASSERT(!fUseTexture); |
| 329 |
fImageSubset = SkImages::DeferredFromGenerator(std::move(gen)) |
| 330 |
->makeSubset(recorder, subset, {}); |
| 331 |
} |
| 332 |
if (!fImageSubset) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50)); |
| 336 |
} |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
static void draw_placeholder(SkCanvas* canvas, SkScalar x, SkScalar y, int w, int h) { |
| 342 |
SkPaint paint; |
| 343 |
paint.setStyle(SkPaint::kStroke_Style); |
| 344 |
SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(w), SkIntToScalar(h)); |
| 345 |
canvas->drawRect(r, paint); |
| 346 |
canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), paint); |
| 347 |
canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), paint); |
| 348 |
} |
| 349 |
|
| 350 |
static void draw_as_bitmap(GrDirectContext* dContext, SkCanvas* canvas, SkImage* image, |
| 351 |
SkScalar x, SkScalar y) { |
| 352 |
SkBitmap bitmap; |
| 353 |
if (as_IB(image)->getROPixels(dContext, &bitmap)) { |
| 354 |
canvas->drawImage(bitmap.asImage(), x, y); |
| 355 |
} else { |
| 356 |
draw_placeholder(canvas, x, y, image->width(), image->height()); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
static void draw_as_tex(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) { |
| 361 |
#if defined(SK_GANESH) |
| 362 |
if (as_IB(image)->isGaneshBacked()) { |
| 363 |
// The gpu-backed images are drawn in this manner bc the generator backed images |
| 364 |
// aren't considered texture-backed |
| 365 |
// We know for this test the targetSurface proxy is different from the image so we can |
| 366 |
// just pass in nullptr. |
| 367 |
auto [view, ct] = |
| 368 |
skgpu::ganesh::AsView(canvas->recordingContext(), image, skgpu::Mipmapped::kNo, |
| 369 |
/*targetSurface=*/nullptr); |
| 370 |
if (!view) { |
| 371 |
// show placeholder if we have no texture |
| 372 |
draw_placeholder(canvas, x, y, image->width(), image->height()); |
| 373 |
return; |
| 374 |
} |
| 375 |
SkColorInfo colorInfo(GrColorTypeToSkColorType(ct), |
| 376 |
image->alphaType(), |
| 377 |
image->refColorSpace()); |
| 378 |
// No API to draw a GrTexture directly, so we cheat and create a private image subclass |
| 379 |
sk_sp<SkImage> texImage(new SkImage_Ganesh(sk_ref_sp(canvas->recordingContext()), |
| 380 |
image->uniqueID(), |
| 381 |
std::move(view), |
| 382 |
std::move(colorInfo))); |
| 383 |
canvas->drawImage(texImage.get(), x, y); |
| 384 |
} else |
| 385 |
#endif |
| 386 |
{ |
| 387 |
canvas->drawImage(image, x, y); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
void drawRow(GrDirectContext* dContext, SkCanvas* canvas, float scale) const { |
| 392 |
canvas->scale(scale, scale); |
| 393 |
|
| 394 |
SkMatrix matrix = SkMatrix::Translate(-100, -100); |
| 395 |
canvas->drawPicture(fPicture, &matrix, nullptr); |
| 396 |
|
| 397 |
// Draw the tex first, so it doesn't hit a lucky cache from the raster version. This |
| 398 |
// way we also can force the generateTexture call. |
| 399 |
|
| 400 |
draw_as_tex(canvas, fImage.get(), 150, 0); |
| 401 |
draw_as_tex(canvas, fImageSubset.get(), 150+101, 0); |
| 402 |
|
| 403 |
draw_as_bitmap(dContext, canvas, fImage.get(), 310, 0); |
| 404 |
draw_as_bitmap(dContext, canvas, fImageSubset.get(), 310+101, 0); |
| 405 |
} |
| 406 |
|
| 407 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 408 |
#if defined(SK_GANESH) |
| 409 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 410 |
#else |
| 411 |
constexpr GrDirectContext* dContext = nullptr; |
| 412 |
#endif |
| 413 |
if (!this->makeCaches(canvas)) { |
| 414 |
errorMsg->printf("Could not create cached images"); |
| 415 |
return DrawResult::kSkip; |
| 416 |
} |
| 417 |
|
| 418 |
canvas->save(); |
| 419 |
canvas->translate(20, 20); |
| 420 |
this->drawRow(dContext, canvas, 1.0); |
| 421 |
canvas->restore(); |
| 422 |
|
| 423 |
canvas->save(); |
| 424 |
canvas->translate(20, 150); |
| 425 |
this->drawRow(dContext, canvas, 0.25f); |
| 426 |
canvas->restore(); |
| 427 |
|
| 428 |
canvas->save(); |
| 429 |
canvas->translate(20, 220); |
| 430 |
this->drawRow(dContext, canvas, 2.0f); |
| 431 |
canvas->restore(); |
| 432 |
|
| 433 |
return DrawResult::kOk; |
| 434 |
} |
| 435 |
|
| 436 |
private: |
| 437 |
using INHERITED = skiagm::GM; |
| 438 |
}; |
| 439 |
|
| 440 |
DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator, false); ) |
| 441 |
DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator, false); ) |
| 442 |
#if defined(SK_GANESH) |
| 443 |
DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator, true); ) |
| 444 |
#endif |