skia2/gm/image_pict.cpp

373 lines
13 KiB
C++
Raw Normal View History

/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.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/SkImageGenerator.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrRecordingContext.h"
#include "include/gpu/GrTypes.h"
#include "include/private/GrTypesPriv.h"
#include "src/gpu/GrRecordingContextPriv.h"
#include "src/gpu/GrSamplerState.h"
#include "src/gpu/GrSurfaceContext.h"
#include "src/gpu/GrTextureProxy.h"
#include "src/image/SkImage_Base.h"
#include "src/image/SkImage_Gpu.h"
#include <memory>
#include <utility>
class GrRecordingContext;
static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorRED);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(10);
canvas->drawRect(bounds, paint);
paint.setStyle(SkPaint::kFill_Style);
paint.setColor(SK_ColorBLUE);
canvas->drawOval(bounds, paint);
}
/*
* Exercise drawing pictures inside an image, showing that the image version is pixelated
* (correctly) when it is inside an image.
*/
class ImagePictGM : public skiagm::GM {
sk_sp<SkPicture> fPicture;
sk_sp<SkImage> fImage0;
sk_sp<SkImage> fImage1;
public:
ImagePictGM() {}
protected:
SkString onShortName() override {
return SkString("image-picture");
}
SkISize onISize() override {
return SkISize::Make(850, 450);
}
void onOnceBeforeDraw() override {
const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100);
SkPictureRecorder recorder;
draw_something(recorder.beginRecording(bounds), bounds);
fPicture = recorder.finishRecordingAsPicture();
// extract enough just for the oval.
const SkISize size = SkISize::Make(100, 100);
auto srgbColorSpace = SkColorSpace::MakeSRGB();
SkMatrix matrix;
matrix.setTranslate(-100, -100);
fImage0 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
SkImage::BitDepth::kU8, srgbColorSpace);
matrix.postTranslate(-50, -50);
matrix.postRotate(45);
matrix.postTranslate(50, 50);
fImage1 = SkImage::MakeFromPicture(fPicture, size, &matrix, nullptr,
SkImage::BitDepth::kU8, srgbColorSpace);
}
void drawSet(SkCanvas* canvas) const {
SkMatrix matrix = SkMatrix::Translate(-100, -100);
canvas->drawPicture(fPicture, &matrix, nullptr);
canvas->drawImage(fImage0.get(), 150, 0);
canvas->drawImage(fImage1.get(), 300, 0);
}
void onDraw(SkCanvas* canvas) override {
canvas->translate(20, 20);
this->drawSet(canvas);
canvas->save();
canvas->translate(0, 130);
canvas->scale(0.25f, 0.25f);
this->drawSet(canvas);
canvas->restore();
canvas->save();
canvas->translate(0, 200);
canvas->scale(2, 2);
this->drawSet(canvas);
canvas->restore();
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM( return new ImagePictGM; )
///////////////////////////////////////////////////////////////////////////////////////////////////
static std::unique_ptr<SkImageGenerator> make_pic_generator(GrDirectContext*,
sk_sp<SkPicture> pic) {
SkMatrix matrix;
matrix.setTranslate(-100, -100);
return SkImageGenerator::MakeFromPicture({ 100, 100 }, std::move(pic), &matrix, nullptr,
SkImage::BitDepth::kU8,
SkColorSpace::MakeSRGB());
}
class RasterGenerator : public SkImageGenerator {
public:
RasterGenerator(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm)
{}
protected:
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
const Options&) override {
SkASSERT(fBM.width() == info.width());
SkASSERT(fBM.height() == info.height());
return fBM.readPixels(info, pixels, rowBytes, 0, 0);
}
private:
SkBitmap fBM;
};
static std::unique_ptr<SkImageGenerator> make_ras_generator(GrDirectContext*,
sk_sp<SkPicture> pic) {
SkBitmap bm;
bm.allocN32Pixels(100, 100);
SkCanvas canvas(bm);
canvas.clear(0);
canvas.translate(-100, -100);
canvas.drawPicture(pic);
return std::make_unique<RasterGenerator>(bm);
}
class TextureGenerator : public SkImageGenerator {
public:
TextureGenerator(GrRecordingContext* rContext, const SkImageInfo& info, sk_sp<SkPicture> pic)
: SkImageGenerator(info)
, fRContext(SkRef(rContext)) {
sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kYes, info, 0,
kTopLeft_GrSurfaceOrigin, nullptr));
if (surface) {
surface->getCanvas()->clear(0);
surface->getCanvas()->translate(-100, -100);
surface->getCanvas()->drawPicture(pic);
sk_sp<SkImage> image(surface->makeImageSnapshot());
const GrSurfaceProxyView* view = as_IB(image)->view(rContext);
if (view) {
fView = *view;
}
}
}
protected:
GrSurfaceProxyView onGenerateTexture(GrRecordingContext* rContext,
const SkImageInfo& info,
const SkIPoint& origin,
GrMipmapped mipMapped,
GrImageTexGenPolicy policy) override {
SkASSERT(rContext);
SkASSERT(rContext->priv().matches(fRContext.get()));
if (!fView) {
return {};
}
if (origin.fX == 0 && origin.fY == 0 && info.dimensions() == fView.proxy()->dimensions() &&
policy == GrImageTexGenPolicy::kDraw) {
return fView;
}
auto budgeted = policy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted ? SkBudgeted::kNo
: SkBudgeted::kYes;
return GrSurfaceProxyView::Copy(
fRContext.get(), fView, mipMapped,
Reland "Reland "Remove support for copyAsDraw in gpu copySurface."" This reverts commit 4c6f9b767034c6812d868108516c2580dce3cb56. Reason for revert: Landing with neuxs 7 and androind one fixes Original change's description: > Revert "Reland "Remove support for copyAsDraw in gpu copySurface."" > > This reverts commit 84ea04949cabc87a88d06c5c6f6aeb944a745911. > > Reason for revert: nexus 7 and android one broken > > Original change's description: > > Reland "Remove support for copyAsDraw in gpu copySurface." > > > > This reverts commit c5167c053bd58e6afbad83fe493c0231df3f9704. > > > > Reason for revert: fixed > > > > Original change's description: > > > Revert "Remove support for copyAsDraw in gpu copySurface." > > > > > > This reverts commit 6565506463db042d3d543a1707f473cdf1ef4e9e. > > > > > > Reason for revert: seems to break things? > > > > > > Original change's description: > > > > Remove support for copyAsDraw in gpu copySurface. > > > > > > > > The major changes on a higher lever are: > > > > 1) The majority of all copies now go through GrSurfaceProxy::Copy which > > > > takes in a proxy and returns a new one with the data copied to it. This > > > > is the most common use case within Ganesh. > > > > > > > > 2) The backend copy calls no longer do draws, require origins to be the > > > > same, and won't do any swizzling or adjustment of subrects. They are > > > > all implemented to be dumb copy this data to this other spot. > > > > > > > > 3) The GrSurfaceContext copy call has now been moved to priv and renamed > > > > copyNoDraw, and a new priv copyAsDraw was added to GrRenderTargetContext. > > > > > > > > 4) WritePixels and ReplaceRenderTarget both need to specifiy the destination > > > > of copies. They are the only users (besides the GrSurfaceProxy::Copy) which > > > > call the priv methods on GrSurfaceContext. > > > > > > > > Change-Id: Iaf1eb3a73ccaf39a75af77e281dae594f809186f > > > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/217459 > > > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > > > Commit-Queue: Greg Daniel <egdaniel@google.com> > > > > > > TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com > > > > > > Change-Id: Id43aa8aa1451e794342e930441d9975b90e6b59f > > > No-Presubmit: true > > > No-Tree-Checks: true > > > No-Try: true > > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/218549 > > > Reviewed-by: Greg Daniel <egdaniel@google.com> > > > Commit-Queue: Greg Daniel <egdaniel@google.com> > > > > TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com > > > > Change-Id: I1a96f85ae2ff7622a6b57406755d478e7fbcf56e > > No-Presubmit: true > > No-Tree-Checks: true > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/218797 > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > Commit-Queue: Greg Daniel <egdaniel@google.com> > > TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com > > Change-Id: I310930a9df30535f45a065263a40239141e15562 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/219384 > Reviewed-by: Greg Daniel <egdaniel@google.com> > Commit-Queue: Greg Daniel <egdaniel@google.com> TBR=egdaniel@google.com,bsalomon@google.com,robertphillips@google.com Change-Id: I88df4f19aa26ed77b5af4e25d138387cbabd1934 No-Presubmit: true No-Tree-Checks: true Reviewed-on: https://skia-review.googlesource.com/c/skia/+/219386 Commit-Queue: Greg Daniel <egdaniel@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
2019-06-07 15:43:30 +00:00
SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()),
SkBackingFit::kExact, budgeted);
}
private:
sk_sp<GrRecordingContext> fRContext;
GrSurfaceProxyView fView;
};
static std::unique_ptr<SkImageGenerator> make_tex_generator(GrDirectContext* dContext,
sk_sp<SkPicture> pic) {
if (!dContext) {
return nullptr;
}
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
return std::make_unique<TextureGenerator>(dContext, info, pic);
}
class ImageCacheratorGM : public skiagm::GM {
typedef std::unique_ptr<SkImageGenerator> (*FactoryFunc)(GrDirectContext*, sk_sp<SkPicture>);
SkString fName;
FactoryFunc fFactory;
sk_sp<SkPicture> fPicture;
sk_sp<SkImage> fImage;
sk_sp<SkImage> fImageSubset;
public:
ImageCacheratorGM(const char suffix[], FactoryFunc factory) : fFactory(factory) {
fName.printf("image-cacherator-from-%s", suffix);
}
protected:
SkString onShortName() override {
return fName;
}
SkISize onISize() override {
return SkISize::Make(960, 450);
}
void onOnceBeforeDraw() override {
const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100);
SkPictureRecorder recorder;
draw_something(recorder.beginRecording(bounds), bounds);
fPicture = recorder.finishRecordingAsPicture();
}
bool makeCaches(GrDirectContext* dContext) {
{
auto gen = fFactory(dContext, fPicture);
if (!gen) {
return false;
}
fImage = SkImage::MakeFromGenerator(std::move(gen));
if (!fImage) {
return false;
}
SkASSERT(fImage->dimensions() == SkISize::Make(100, 100));
}
{
const SkIRect subset = SkIRect::MakeLTRB(50, 50, 100, 100);
// We re-create the generator here on the off chance that making a subset from
// 'fImage' might perturb its state.
auto gen = fFactory(dContext, fPicture);
if (!gen) {
return false;
}
fImageSubset = SkImage::MakeFromGenerator(std::move(gen))->makeSubset(subset, dContext);
if (!fImageSubset) {
return false;
}
SkASSERT(fImageSubset->dimensions() == SkISize::Make(50, 50));
}
return true;
}
static void draw_placeholder(SkCanvas* canvas, SkScalar x, SkScalar y, int w, int h) {
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
SkRect r = SkRect::MakeXYWH(x, y, SkIntToScalar(w), SkIntToScalar(h));
canvas->drawRect(r, paint);
canvas->drawLine(r.left(), r.top(), r.right(), r.bottom(), paint);
canvas->drawLine(r.left(), r.bottom(), r.right(), r.top(), paint);
}
static void draw_as_bitmap(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) {
SkBitmap bitmap;
if (as_IB(image)->getROPixels(&bitmap)) {
canvas->drawBitmap(bitmap, x, y);
} else {
draw_placeholder(canvas, x, y, image->width(), image->height());
}
}
static void draw_as_tex(SkCanvas* canvas, SkImage* image, SkScalar x, SkScalar y) {
// The gpu-backed images are drawn in this manner bc the generator backed images
// aren't considered texture-backed
GrSurfaceProxyView view = as_IB(image)->refView(canvas->recordingContext(),
GrMipmapped::kNo);
if (!view) {
// show placeholder if we have no texture
draw_placeholder(canvas, x, y, image->width(), image->height());
return;
}
// CONTEXT TODO: remove this use of the 'backdoor' to create an image
GrContext* tmp = canvas->recordingContext()->priv().backdoor();
// No API to draw a GrTexture directly, so we cheat and create a private image subclass
sk_sp<SkImage> texImage(new SkImage_Gpu(sk_ref_sp(tmp),
Reland "Reland "Pass in a SkColorType into SkImage_Gpu ctor."" This reverts commit 9ed88b310117915f61df73720715fc60a711f1f0. Reason for revert: <INSERT REASONING HERE> Original change's description: > Revert "Reland "Pass in a SkColorType into SkImage_Gpu ctor."" > > This reverts commit 938106dcccf4cd31020d767c95ee74d728e42370. > > Reason for revert: Most likely cause of Chrome roll failure: > https://chromium-review.googlesource.com/c/chromium/src/+/2013502 > > Original change's description: > > Reland "Pass in a SkColorType into SkImage_Gpu ctor." > > > > This reverts commit 42032cb88fb70dcc3cda02451b712e747a9b6171. > > > > Reason for revert: Relanding now that pre CLS that fix initial issue landed > > > > Original change's description: > > > Revert "Pass in a SkColorType into SkImage_Gpu ctor." > > > > > > This reverts commit 1d17543133c023da03f1ef4ab0a125ca3782957c. > > > > > > Reason for revert: android bots > > > > > > Original change's description: > > > > Pass in a SkColorType into SkImage_Gpu ctor. > > > > > > > > This allows us to get rid of using the GrPixelConfig on proxy to create > > > > the SkColorType > > > > > > > > Bug: skia:6718 > > > > Change-Id: I1758b79152fa1e8a1796e55d19f5e8ca0b0e8b7b > > > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/264396 > > > > Commit-Queue: Greg Daniel <egdaniel@google.com> > > > > Reviewed-by: Robert Phillips <robertphillips@google.com> > > > > > > TBR=egdaniel@google.com,robertphillips@google.com > > > > > > Change-Id: I449e6b0750d611346791beb1dd1346106ad01741 > > > No-Presubmit: true > > > No-Tree-Checks: true > > > No-Try: true > > > Bug: skia:6718 > > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/264469 > > > Reviewed-by: Mike Klein <mtklein@google.com> > > > Commit-Queue: Mike Klein <mtklein@google.com> > > > > TBR=egdaniel@google.com,mtklein@google.com,robertphillips@google.com > > > > # Not skipping CQ checks because original CL landed > 1 day ago. > > > > Bug: skia:6718 > > Change-Id: I8088b4e96d1f2c73be03d150fb8f8280a21db5de > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265496 > > Reviewed-by: Greg Daniel <egdaniel@google.com> > > Commit-Queue: Greg Daniel <egdaniel@google.com> > > TBR=egdaniel@google.com,robertphillips@google.com > > Change-Id: Idded694199b5c090a727e9b84251ba4700716c1f > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:6718 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265603 > Reviewed-by: Ben Wagner aka dogben <benjaminwagner@google.com> > Commit-Queue: Ben Wagner aka dogben <benjaminwagner@google.com> TBR=egdaniel@google.com,benjaminwagner@google.com,robertphillips@google.com Bug: skia:6718 Change-Id: Iaed2c5de04bfedcc85f75fb581f1c1fd2775bcde Reviewed-on: https://skia-review.googlesource.com/c/skia/+/265762 Reviewed-by: Greg Daniel <egdaniel@google.com> Commit-Queue: Greg Daniel <egdaniel@google.com>
2020-01-22 17:22:36 +00:00
image->uniqueID(), std::move(view),
image->colorType(), image->alphaType(),
image->refColorSpace()));
canvas->drawImage(texImage.get(), x, y);
}
void drawRow(SkCanvas* canvas, float scale) const {
canvas->scale(scale, scale);
SkMatrix matrix = SkMatrix::Translate(-100, -100);
canvas->drawPicture(fPicture, &matrix, nullptr);
// Draw the tex first, so it doesn't hit a lucky cache from the raster version. This
// way we also can force the generateTexture call.
draw_as_tex(canvas, fImage.get(), 150, 0);
draw_as_tex(canvas, fImageSubset.get(), 150+101, 0);
draw_as_bitmap(canvas, fImage.get(), 310, 0);
draw_as_bitmap(canvas, fImageSubset.get(), 310+101, 0);
}
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
if (!this->makeCaches(GrAsDirectContext(canvas->recordingContext()))) {
errorMsg->printf("Could not create cached images");
return DrawResult::kSkip;
}
canvas->save();
canvas->translate(20, 20);
this->drawRow(canvas, 1.0);
canvas->restore();
canvas->save();
canvas->translate(20, 150);
this->drawRow(canvas, 0.25f);
canvas->restore();
canvas->save();
canvas->translate(20, 220);
this->drawRow(canvas, 2.0f);
canvas->restore();
return DrawResult::kOk;
}
private:
typedef skiagm::GM INHERITED;
};
DEF_GM( return new ImageCacheratorGM("picture", make_pic_generator); )
DEF_GM( return new ImageCacheratorGM("raster", make_ras_generator); )
DEF_GM( return new ImageCacheratorGM("texture", make_tex_generator); )