diff options
author | reed <reed@google.com> | 2015-08-20 12:30:20 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-20 12:30:20 -0700 |
commit | 499ababa52f3ac4fccf957979713abe58be1584a (patch) | |
tree | dad2072803e0b999f596fa12e1ac602075319a6b | |
parent | d028180f0310cf2b23f9744256a41697b0683e67 (diff) |
add gm for image->newShader
BUG=skia:
TBR=
Review URL: https://codereview.chromium.org/1302943004
-rw-r--r-- | gm/image_shader.cpp | 129 | ||||
-rw-r--r-- | gyp/utils.gyp | 1 | ||||
-rw-r--r-- | gyp/utils.gypi | 1 | ||||
-rw-r--r-- | src/utils/SkImageGeneratorUtils.cpp | 134 | ||||
-rw-r--r-- | src/utils/SkImageGeneratorUtils.h | 29 |
5 files changed, 294 insertions, 0 deletions
diff --git a/gm/image_shader.cpp b/gm/image_shader.cpp new file mode 100644 index 0000000000..0552c2431c --- /dev/null +++ b/gm/image_shader.cpp @@ -0,0 +1,129 @@ +/* + * 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.h" +#include "SkCanvas.h" +#include "SkData.h" +#include "SkImage.h" +#include "SkPictureRecorder.h" +#include "SkSurface.h" + +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); +} + +typedef SkImage* (*ImageMakerProc)(GrContext*, const SkPicture*, const SkImageInfo&); + +static SkImage* make_raster(GrContext*, const SkPicture* pic, const SkImageInfo& info) { + SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); + surface->getCanvas()->clear(0); + surface->getCanvas()->drawPicture(pic); + return surface->newImageSnapshot(); +} + +static SkImage* make_texture(GrContext* ctx, const SkPicture* pic, const SkImageInfo& info) { + if (!ctx) { + return nullptr; + } + SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, + info, 0)); + surface->getCanvas()->clear(0); + surface->getCanvas()->drawPicture(pic); + return surface->newImageSnapshot(); +} + +static SkImage* make_pict_gen(GrContext*, const SkPicture* pic, const SkImageInfo& info) { + return SkImage::NewFromPicture(pic, info.dimensions(), nullptr, nullptr); +} + +static SkImage* make_encode_gen(GrContext* ctx, const SkPicture* pic, const SkImageInfo& info) { + SkAutoTUnref<SkImage> src(make_raster(ctx, pic, info)); + if (!src) { + return nullptr; + } + SkAutoTUnref<SkData> encoded(src->encode(SkImageEncoder::kPNG_Type, 100)); + if (!encoded) { + return nullptr; + } + return SkImage::NewFromEncoded(encoded); +} + +const ImageMakerProc gProcs[] = { + make_raster, + make_texture, + make_pict_gen, + make_encode_gen, +}; + +/* + * Exercise drawing pictures inside an image, showing that the image version is pixelated + * (correctly) when it is inside an image. + */ +class ImageShaderGM : public skiagm::GM { + SkAutoTUnref<SkPicture> fPicture; + +public: + ImageShaderGM() {} + +protected: + SkString onShortName() override { + return SkString("image-shader"); + } + + SkISize onISize() override { + return SkISize::Make(850, 450); + } + + void onOnceBeforeDraw() override { + const SkRect bounds = SkRect::MakeWH(100, 100); + SkPictureRecorder recorder; + draw_something(recorder.beginRecording(bounds), bounds); + fPicture.reset(recorder.endRecording()); + } + + void testImage(SkCanvas* canvas, SkImage* image) { + SkAutoCanvasRestore acr(canvas, true); + + canvas->drawImage(image, 0, 0); + canvas->translate(0, 120); + + const SkShader::TileMode tile = SkShader::kRepeat_TileMode; + const SkMatrix localM = SkMatrix::MakeTrans(-50, -50); + SkAutoTUnref<SkShader> shader(image->newShader(tile, tile, &localM)); + SkPaint paint; + paint.setAntiAlias(true); + paint.setShader(shader); + canvas->drawCircle(50, 50, 50, paint); + } + + void onDraw(SkCanvas* canvas) override { + canvas->translate(20, 20); + + const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); + + for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) { + SkAutoTUnref<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture, info)); + if (image) { + this->testImage(canvas, image); + } + canvas->translate(120, 0); + } + } + +private: + typedef skiagm::GM INHERITED; +}; +DEF_GM( return new ImageShaderGM; ) + diff --git a/gyp/utils.gyp b/gyp/utils.gyp index f3c071009f..9881aa10c1 100644 --- a/gyp/utils.gyp +++ b/gyp/utils.gyp @@ -19,6 +19,7 @@ ], 'include_dirs': [ '../include/effects', + '../include/gpu', '../include/images', '../include/pathops', '../include/pipe', diff --git a/gyp/utils.gypi b/gyp/utils.gypi index 347cc24fef..53ea7f0dfc 100644 --- a/gyp/utils.gypi +++ b/gyp/utils.gypi @@ -56,6 +56,7 @@ '<(skia_src_path)/utils/SkDumpCanvas.cpp', '<(skia_src_path)/utils/SkEventTracer.cpp', '<(skia_src_path)/utils/SkFloatUtils.h', + '<(skia_src_path)/utils/SkImageGeneratorUtils.cpp', '<(skia_src_path)/utils/SkInterpolator.cpp', '<(skia_src_path)/utils/SkLayer.cpp', '<(skia_src_path)/utils/SkMatrix22.cpp', diff --git a/src/utils/SkImageGeneratorUtils.cpp b/src/utils/SkImageGeneratorUtils.cpp new file mode 100644 index 0000000000..f5198c05fe --- /dev/null +++ b/src/utils/SkImageGeneratorUtils.cpp @@ -0,0 +1,134 @@ +/* + * 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 "SkImageGeneratorUtils.h" +#include "SkBitmap.h" + +class GeneratorFromEmpty : public SkImageGenerator { +public: + GeneratorFromEmpty(const SkImageInfo& info) : SkImageGenerator(info) {} +}; + +SkImageGenerator* SkImageGeneratorUtils::NewEmpty(const SkImageInfo& info) { + return SkNEW_ARGS(GeneratorFromEmpty, (info)); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +class GeneratorFromBitmap : public SkImageGenerator { +public: + GeneratorFromBitmap(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) { + if (!bm.isImmutable()) { + bm.copyTo(&fBM); + } + } + +protected: + bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, + SkPMColor*, int*) override { + return fBM.readPixels(info, pixels, rowBytes, 0, 0); + } + +private: + SkBitmap fBM; +}; + +SkImageGenerator* SkImageGeneratorUtils::NewFromBitmap(const SkBitmap& bm) { + return SkNEW_ARGS(GeneratorFromBitmap, (bm)); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#if SK_SUPPORT_GPU + +#include "GrContext.h" +#include "GrTexture.h" +#include "SkGr.h" + +class GeneratorFromTexture : public SkImageGenerator { +public: + GeneratorFromTexture(GrContext* ctx, GrTexture* tex, const SkImageInfo& info) + : SkImageGenerator(info), fCtx(ctx), fTexture(tex) + {} + +protected: + GrTexture* onGenerateTexture(GrContext* ctx, SkImageUsageType, const SkIRect* subset) override { + if (ctx) { + SkASSERT(ctx == fCtx.get()); + } + + if (!subset) { + return SkRef(fTexture.get()); + } + // need to copy the subset into a new texture + GrSurfaceDesc desc = fTexture->desc(); + desc.fWidth = subset->width(); + desc.fHeight = subset->height(); + + GrTexture* dst = fCtx->textureProvider()->createTexture(desc, false); + fCtx->copySurface(dst, fTexture, *subset, SkIPoint::Make(0, 0)); + return dst; + } +private: + SkAutoTUnref<GrContext> fCtx; + SkAutoTUnref<GrTexture> fTexture; +}; +#endif + +SkImageGenerator* SkImageGeneratorUtils::NewFromTexture(GrContext* ctx, GrTexture* tex) { +#if SK_SUPPORT_GPU + if (ctx && tex) { + const GrSurfaceDesc desc = tex->desc(); + + SkColorType ct; + SkColorProfileType cpt; + if (!GrPixelConfig2ColorAndProfileType(desc.fConfig, &ct, &cpt)) { + return nullptr; + } + const SkAlphaType at = kPremul_SkAlphaType; // take isOpaque from caller? + SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, ct, at, cpt); + return SkNEW_ARGS(GeneratorFromTexture, (ctx, tex, info)); + } +#endif + return nullptr; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "SkImage.h" + +class GeneratorFromImage : public SkImageGenerator { +public: + GeneratorFromImage(const SkImage* image, const SkImageInfo& info) + : SkImageGenerator(info), fImage(image) {} + +protected: + bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, + SkPMColor*, int*) override { + return fImage->readPixels(info, pixels, rowBytes, 0, 0); + } + + GrTexture* onGenerateTexture(GrContext* ctx, SkImageUsageType, const SkIRect* subset) override { + // waiting on https://code.google.com/p/skia/issues/detail?id=4233 + return nullptr; + } + +private: + SkAutoTUnref<const SkImage> fImage; +}; + +SkImageGenerator* SkImageGeneratorUtils::NewFromImage(const SkImage* image) { + if (image) { + const SkColorType ct = kN32_SkColorType; + const SkAlphaType at = image->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; + const SkImageInfo info = SkImageInfo::Make(image->width(), image->height(), ct, at); + return SkNEW_ARGS(GeneratorFromImage, (image, info)); + } + return nullptr; +} + + diff --git a/src/utils/SkImageGeneratorUtils.h b/src/utils/SkImageGeneratorUtils.h new file mode 100644 index 0000000000..940d12da0f --- /dev/null +++ b/src/utils/SkImageGeneratorUtils.h @@ -0,0 +1,29 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkImageGeneratorUtils_DEFINED +#define SkImageGeneratorUtils_DEFINED + +#include "SkImageGenerator.h" + +class SkImage; + +class SkImageGeneratorUtils { +public: + // Returns a generator of the specified dimensions, but will always fail to return anything + static SkImageGenerator* NewEmpty(const SkImageInfo&); + + // If the bitmap is mutable, it will make a copy first + static SkImageGenerator* NewFromBitmap(const SkBitmap&); + + // Ref's the provided texture, so it had better be const! + static SkImageGenerator* NewFromTexture(GrContext*, GrTexture*); + + static SkImageGenerator* NewFromImage(const SkImage*); +}; + +#endif |