Skip to content

Commit

Permalink
Move Ganesh Skp texture code into GrImageUtils
Browse files Browse the repository at this point in the history
This is a follow-up to http://review.skia.org/673717 which
makes SkPictureImageGenerator Ganesh-agnostic (and moves it
to src/image/ so it can be closer to its friends).

Change-Id: I61e0e8d5e026fd782da9651c4d90be51daee0734
Bug: skia:13983
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/678656
Commit-Queue: Kevin Lubick <[email protected]>
Reviewed-by: Brian Osman <[email protected]>
  • Loading branch information
kjlubick authored and SkCQ committed Apr 27, 2023
1 parent f90ca5d commit e72842a
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 97 deletions.
5 changes: 4 additions & 1 deletion gn/core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,13 @@ skia_core_sources = [
"$_src/image/SkImage_Lazy.cpp",
"$_src/image/SkImage_Lazy.h",
"$_src/image/SkImage_LazyFactories.cpp",
"$_src/image/SkImage_Picture.cpp",
"$_src/image/SkImage_Picture.h",
"$_src/image/SkImage_Raster.cpp",
"$_src/image/SkImage_Raster.h",
"$_src/image/SkImage_RasterFactories.cpp",
"$_src/image/SkPictureImageGenerator.cpp",
"$_src/image/SkPictureImageGenerator.h",
"$_src/image/SkRescaleAndReadPixels.cpp",
"$_src/image/SkRescaleAndReadPixels.h",
"$_src/image/SkSurface.cpp",
Expand Down Expand Up @@ -726,7 +730,6 @@ skia_skpicture_sources = [
"$_src/core/SkPictureData.h",
"$_src/core/SkPictureFlat.cpp",
"$_src/core/SkPictureFlat.h",
"$_src/core/SkPictureImageGenerator.cpp",
"$_src/core/SkPicturePlayback.cpp",
"$_src/core/SkPicturePlayback.h",
"$_src/core/SkPictureRecord.cpp",
Expand Down
2 changes: 1 addition & 1 deletion include/core/SkImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ class SK_API SkImage : public SkRefCnt {
example: https://fiddle.skia.org/c/@Image_isLazyGenerated_a
example: https://fiddle.skia.org/c/@Image_isLazyGenerated_b
*/
bool isLazyGenerated() const;
virtual bool isLazyGenerated() const = 0;

/** Creates SkImage in target SkColorSpace.
Returns nullptr if SkImage could not be created.
Expand Down
5 changes: 4 additions & 1 deletion public.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ BASE_SRCS_ALL = [
"src/core/SkPictureData.h",
"src/core/SkPictureFlat.cpp",
"src/core/SkPictureFlat.h",
"src/core/SkPictureImageGenerator.cpp",
"src/core/SkPicturePlayback.cpp",
"src/core/SkPicturePlayback.h",
"src/core/SkPicturePriv.h",
Expand Down Expand Up @@ -1232,9 +1231,13 @@ BASE_SRCS_ALL = [
"src/image/SkImage_Lazy.cpp",
"src/image/SkImage_Lazy.h",
"src/image/SkImage_LazyFactories.cpp",
"src/image/SkImage_Picture.cpp",
"src/image/SkImage_Picture.h",
"src/image/SkImage_Raster.cpp",
"src/image/SkImage_Raster.h",
"src/image/SkImage_RasterFactories.cpp",
"src/image/SkPictureImageGenerator.cpp",
"src/image/SkPictureImageGenerator.h",
"src/image/SkRescaleAndReadPixels.cpp",
"src/image/SkRescaleAndReadPixels.h",
"src/image/SkSurface.cpp",
Expand Down
1 change: 0 additions & 1 deletion src/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ SKPICTURE_FILES = [
"SkPictureData.h",
"SkPictureFlat.cpp",
"SkPictureFlat.h",
"SkPictureImageGenerator.cpp",
"SkPicturePlayback.cpp",
"SkPicturePlayback.h",
"SkPictureRecord.cpp",
Expand Down
49 changes: 48 additions & 1 deletion src/gpu/ganesh/image/GrImageUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkRect.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkSize.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/core/SkYUVAInfo.h"
#include "include/core/SkYUVAPixmaps.h"
Expand Down Expand Up @@ -53,6 +56,7 @@
#include "src/gpu/ganesh/image/SkImage_RasterPinnable.h"
#include "src/image/SkImage_Base.h"
#include "src/image/SkImage_Lazy.h"
#include "src/image/SkImage_Picture.h"
#include "src/image/SkImage_Raster.h"

#include <string_view>
Expand Down Expand Up @@ -219,6 +223,41 @@ static GrSurfaceProxyView texture_proxy_view_from_planes(GrRecordingContext* ctx
return sfc->readSurfaceView();
}

static GrSurfaceProxyView generate_picture_texture(GrRecordingContext* ctx,
const SkImage_Picture* img,
GrMipmapped mipmapped,
GrImageTexGenPolicy texGenPolicy) {
SkASSERT(ctx);
SkASSERT(img);

auto sharedGenerator = img->generator();
SkAutoMutexExclusive mutex(sharedGenerator->fMutex);

skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
? skgpu::Budgeted::kNo
: skgpu::Budgeted::kYes;
auto surface = SkSurface::MakeRenderTarget(ctx, budgeted, img->imageInfo(), 0,
kTopLeft_GrSurfaceOrigin,
img->props(), mipmapped == GrMipmapped::kYes);
if (!surface) {
return {};
}

surface->getCanvas()->clear(SkColors::kTransparent);
surface->getCanvas()->drawPicture(img->picture(), img->matrix(), img->paint());
sk_sp<SkImage> image(surface->makeImageSnapshot());
if (!image) {
return {};
}

auto [view, ct] = AsView(ctx, image, mipmapped);
SkASSERT(view);
SkASSERT(mipmapped == GrMipmapped::kNo ||
view.asTextureProxy()->mipmapped() == GrMipmapped::kYes);
return view;
}


// Returns the texture proxy. We will always cache the generated texture on success.
// We have 4 ways to try to return a texture (in sorted order)
//
Expand Down Expand Up @@ -292,7 +331,15 @@ GrSurfaceProxyView LockTextureProxyView(GrRecordingContext* rContext,

// 2. Ask the generator to natively create one (if it knows how)
{
if (img->generator()->isTextureGenerator()) {
if (img->type() == SkImage_Base::Type::kLazyPicture) {
if (auto view = generate_picture_texture(rContext,
static_cast<const SkImage_Picture*>(img),
mipmapped,
texGenPolicy)) {
installKey(view);
return view;
}
} else if (img->generator()->isTextureGenerator()) {
auto sharedGenerator = img->generator();
SkAutoMutexExclusive mutex(sharedGenerator->fMutex);
auto textureGen = static_cast<GrTextureGenerator*>(sharedGenerator->fGenerator.get());
Expand Down
4 changes: 4 additions & 0 deletions src/image/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ CORE_FILES = [
"SkImage_Lazy.cpp",
"SkImage_Lazy.h",
"SkImage_LazyFactories.cpp",
"SkImage_Picture.cpp",
"SkImage_Picture.h",
"SkImage_Raster.cpp",
"SkImage_Raster.h",
"SkImage_RasterFactories.cpp",
"SkPictureImageGenerator.cpp",
"SkPictureImageGenerator.h",
"SkRescaleAndReadPixels.cpp",
"SkRescaleAndReadPixels.h",
"SkSurface.cpp",
Expand Down
4 changes: 0 additions & 4 deletions src/image/SkImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,6 @@ sk_sp<SkImage> SkImage::makeWithFilter(GrRecordingContext* rContext, const SkIma
return result->asImage();
}

bool SkImage::isLazyGenerated() const {
return as_IB(this)->onIsLazyGenerated();
}

bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); }

sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target, GrDirectContext* direct) const {
Expand Down
5 changes: 4 additions & 1 deletion src/image/SkImage_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class SkImage_Base : public SkImage {
kRaster,
kRasterPinnable,
kLazy,
kLazyPicture,
kGanesh,
kGaneshYUVA,
kGraphite,
Expand All @@ -138,7 +139,9 @@ class SkImage_Base : public SkImage {
virtual Type type() const = 0;

// True for picture-backed and codec-backed
bool onIsLazyGenerated() const { return this->type() == Type::kLazy; }
bool isLazyGenerated() const override {
return this->type() == Type::kLazy || this->type() == Type::kLazyPicture;
}

bool isRasterBacked() const {
return this->type() == Type::kRaster || this->type() == Type::kRasterPinnable;
Expand Down
1 change: 0 additions & 1 deletion src/image/SkImage_Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class SkImage_Lazy : public SkImage_Base {

// Be careful with this. You need to acquire the mutex, as the generator might be shared
// among several images.
//std::unique_ptr<SkImageGenerator> generator() const;
sk_sp<SharedGenerator> generator() const;
protected:
virtual bool readPixelsProxy(GrDirectContext*, const SkPixmap&) const { return false; }
Expand Down
11 changes: 5 additions & 6 deletions src/image/SkImage_LazyFactories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include "include/core/SkPicture.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSurfaceProps.h"

#include "src/image/SkImageGeneratorPriv.h"
#include "src/image/SkImage_Picture.h"

#include <optional>
#include <utility>
Expand All @@ -39,8 +38,8 @@ sk_sp<SkImage> DeferredFromPicture(sk_sp<SkPicture> picture,
const SkPaint* paint,
BitDepth bitDepth,
sk_sp<SkColorSpace> colorSpace) {
return DeferredFromPicture(std::move(picture), dimensions, matrix, paint, bitDepth,
std::move(colorSpace), {});
return SkImage_Picture::Make(std::move(picture), dimensions, matrix, paint, bitDepth,
std::move(colorSpace), {});
}

sk_sp<SkImage> DeferredFromPicture(sk_sp<SkPicture> picture,
Expand All @@ -50,8 +49,8 @@ sk_sp<SkImage> DeferredFromPicture(sk_sp<SkPicture> picture,
BitDepth bitDepth,
sk_sp<SkColorSpace> colorSpace,
SkSurfaceProps props) {
return DeferredFromGenerator(SkImageGenerators::MakeFromPicture(
dimensions, std::move(picture), matrix, paint, bitDepth, std::move(colorSpace), props));
return SkImage_Picture::Make(std::move(picture), dimensions, matrix, paint, bitDepth,
std::move(colorSpace), props);
}

} // namespace SkImages
58 changes: 58 additions & 0 deletions src/image/SkImage_Picture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#include "src/image/SkImage_Picture.h"

#include "include/core/SkColorSpace.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageGenerator.h"
#include "include/core/SkPicture.h"
#include "include/core/SkSurfaceProps.h"
#include "src/base/SkTLazy.h"
#include "src/image/SkImageGeneratorPriv.h"
#include "src/image/SkImage_Lazy.h"
#include "src/image/SkPictureImageGenerator.h"

#include <memory>
#include <utility>

class SkMatrix;
class SkPaint;
struct SkISize;

sk_sp<SkImage> SkImage_Picture::Make(sk_sp<SkPicture> picture, const SkISize& dimensions,
const SkMatrix* matrix, const SkPaint* paint,
SkImages::BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace,
SkSurfaceProps props) {
auto gen = SkImageGenerators::MakeFromPicture(dimensions, std::move(picture), matrix, paint,
bitDepth, std::move(colorSpace), props);

SkImage_Lazy::Validator validator(
SharedGenerator::Make(std::move(gen)), nullptr, nullptr);

return validator ? sk_make_sp<SkImage_Picture>(&validator) : nullptr;
}

SkPictureImageGenerator* SkImage_Picture::gen() const {
return static_cast<SkPictureImageGenerator*>(this->generator()->fGenerator.get());
}

SkPicture* SkImage_Picture::picture() const {
return this->gen()->fPicture.get();
}

SkMatrix* SkImage_Picture::matrix() const {
return &this->gen()->fMatrix;
}

SkPaint* SkImage_Picture::paint() const {
return this->gen()->fPaint.getMaybeNull();
}

SkSurfaceProps* SkImage_Picture::props() const {
return &this->gen()->fProps;
}
47 changes: 47 additions & 0 deletions src/image/SkImage_Picture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkImage_Picture_DEFINED
#define SkImage_Picture_DEFINED

#include "include/core/SkRefCnt.h"
#include "src/image/SkImage_Base.h"
#include "src/image/SkImage_Lazy.h"

class SkColorSpace;
class SkImage;
class SkMatrix;
class SkPaint;
class SkPicture;
class SkPictureImageGenerator;
class SkSurfaceProps;
struct SkISize;

namespace SkImages { enum class BitDepth; }

class SkImage_Picture : public SkImage_Lazy {
public:
static sk_sp<SkImage> Make(sk_sp<SkPicture> picture, const SkISize& dimensions,
const SkMatrix* matrix, const SkPaint* paint,
SkImages::BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace,
SkSurfaceProps props);

SkImage_Picture(Validator* validator) : SkImage_Lazy(validator) {}

SkImage_Base::Type type() const override { return SkImage_Base::Type::kLazyPicture; }

// These are not necessarily thread-safe. Be sure to grab the mutex from the shared
// generator before accessing them.
SkPicture* picture() const;
SkMatrix* matrix() const;
SkPaint* paint() const;
SkSurfaceProps* props() const;

private:
SkPictureImageGenerator* gen() const;
};

#endif
Loading

0 comments on commit e72842a

Please sign in to comment.