2012-07-29 20:38:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-01-30 18:01:40 +00:00
|
|
|
#include <functional>
|
2012-07-29 20:38:16 +00:00
|
|
|
#include "gm.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkAutoPixmapStorage.h"
|
2015-01-23 13:58:07 +00:00
|
|
|
#include "SkData.h"
|
2012-07-29 20:38:16 +00:00
|
|
|
#include "SkCanvas.h"
|
2015-01-23 13:58:07 +00:00
|
|
|
#include "SkRandom.h"
|
2012-07-31 03:57:11 +00:00
|
|
|
#include "SkStream.h"
|
2015-01-23 13:58:07 +00:00
|
|
|
#include "SkSurface.h"
|
2012-07-31 03:57:11 +00:00
|
|
|
|
2012-10-31 14:48:39 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#endif
|
2012-08-28 12:19:02 +00:00
|
|
|
|
2012-07-31 03:57:11 +00:00
|
|
|
static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
|
2013-05-28 16:45:07 +00:00
|
|
|
// TODO: Make this draw a file that is checked in, so it can
|
|
|
|
// be exercised on machines other than mike's. Will require a
|
|
|
|
// rebaseline.
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
|
|
|
|
if (nullptr == data) {
|
2014-09-18 20:57:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(data));
|
2012-07-31 03:57:11 +00:00
|
|
|
if (image) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->scale(size.width() * 1.0f / image->width(),
|
|
|
|
size.height() * 1.0f / image->height());
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImage(image, 0, 0, nullptr);
|
2012-07-31 03:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-29 20:38:16 +00:00
|
|
|
|
|
|
|
static void drawContents(SkSurface* surface, SkColor fillC) {
|
2012-09-07 02:01:30 +00:00
|
|
|
SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
|
2012-09-06 18:43:21 +00:00
|
|
|
SkIntToScalar(surface->height()));
|
2012-07-31 02:45:15 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
|
|
|
SkScalar stroke = size.fWidth / 10;
|
|
|
|
SkScalar radius = (size.fWidth - stroke) / 2;
|
|
|
|
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(fillC);
|
|
|
|
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(stroke);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:52:16 +00:00
|
|
|
static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
|
2012-07-29 20:38:16 +00:00
|
|
|
drawContents(surf, SK_ColorRED);
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> imgR = surf->makeImageSnapshot();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
if (true) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> imgR2 = surf->makeImageSnapshot();
|
2012-08-28 12:19:02 +00:00
|
|
|
SkASSERT(imgR == imgR2);
|
|
|
|
}
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
drawContents(surf, SK_ColorGREEN);
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> imgG = surf->makeImageSnapshot();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
// since we've drawn after we snapped imgR, imgG will be a different obj
|
|
|
|
SkASSERT(imgR != imgG);
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
drawContents(surf, SK_ColorBLUE);
|
|
|
|
|
2012-07-31 02:45:15 +00:00
|
|
|
SkPaint paint;
|
|
|
|
// paint.setFilterBitmap(true);
|
|
|
|
// paint.setAlpha(0x80);
|
|
|
|
|
2016-03-25 16:08:00 +00:00
|
|
|
canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr);
|
|
|
|
canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr);
|
2015-08-27 14:41:13 +00:00
|
|
|
surf->draw(canvas, 0, 160, usePaint ? &paint : nullptr);
|
2013-07-23 15:52:16 +00:00
|
|
|
|
|
|
|
SkRect src1, src2, src3;
|
|
|
|
src1.iset(0, 0, surf->width(), surf->height());
|
2013-07-24 07:01:12 +00:00
|
|
|
src2.iset(-surf->width() / 2, -surf->height() / 2,
|
2013-07-23 15:52:16 +00:00
|
|
|
surf->width(), surf->height());
|
|
|
|
src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
|
|
|
|
|
|
|
|
SkRect dst1, dst2, dst3, dst4;
|
|
|
|
dst1.set(0, 240, 65, 305);
|
|
|
|
dst2.set(0, 320, 65, 385);
|
|
|
|
dst3.set(0, 400, 65, 465);
|
|
|
|
dst4.set(0, 480, 65, 545);
|
|
|
|
|
2016-03-25 16:08:00 +00:00
|
|
|
canvas->drawImageRect(imgR, src1, dst1, usePaint ? &paint : nullptr);
|
|
|
|
canvas->drawImageRect(imgG, src2, dst2, usePaint ? &paint : nullptr);
|
|
|
|
canvas->drawImageRect(imgR, src3, dst3, usePaint ? &paint : nullptr);
|
|
|
|
canvas->drawImageRect(imgG, dst4, usePaint ? &paint : nullptr);
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ImageGM : public skiagm::GM {
|
|
|
|
void* fBuffer;
|
2012-07-30 18:20:12 +00:00
|
|
|
size_t fBufferSize;
|
2012-07-29 20:38:16 +00:00
|
|
|
SkSize fSize;
|
|
|
|
enum {
|
|
|
|
W = 64,
|
|
|
|
H = 64,
|
|
|
|
RB = W * 4 + 8,
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
ImageGM() {
|
2012-07-30 18:20:12 +00:00
|
|
|
fBufferSize = RB * H;
|
|
|
|
fBuffer = sk_malloc_throw(fBufferSize);
|
2012-07-29 20:38:16 +00:00
|
|
|
fSize.set(SkIntToScalar(W), SkIntToScalar(H));
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
virtual ~ImageGM() {
|
|
|
|
sk_free(fBuffer);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-10-31 14:48:39 +00:00
|
|
|
return SkString("image-surface");
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-07-23 15:52:16 +00:00
|
|
|
return SkISize::Make(960, 1200);
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-07-31 03:57:11 +00:00
|
|
|
drawJpeg(canvas, this->getISize());
|
|
|
|
|
2012-07-31 02:45:15 +00:00
|
|
|
canvas->scale(2, 2);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const char* kLabel1 = "Original Img";
|
|
|
|
const char* kLabel2 = "Modified Img";
|
|
|
|
const char* kLabel3 = "Cur Surface";
|
|
|
|
const char* kLabel4 = "Full Crop";
|
|
|
|
const char* kLabel5 = "Over-crop";
|
|
|
|
const char* kLabel6 = "Upper-left";
|
|
|
|
const char* kLabel7 = "No Crop";
|
|
|
|
|
|
|
|
const char* kLabel8 = "Pre-Alloc Img";
|
|
|
|
const char* kLabel9 = "New Alloc Img";
|
|
|
|
const char* kLabel10 = "GPU";
|
2012-10-31 14:48:39 +00:00
|
|
|
|
|
|
|
SkPaint textPaint;
|
2014-04-11 18:34:35 +00:00
|
|
|
textPaint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&textPaint);
|
2014-04-11 18:34:35 +00:00
|
|
|
textPaint.setTextSize(8);
|
2012-10-31 14:48:39 +00:00
|
|
|
|
|
|
|
canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
|
|
|
|
canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
|
|
|
|
canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
|
2013-07-23 15:52:16 +00:00
|
|
|
canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
|
|
|
|
canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
|
|
|
|
canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
|
|
|
|
canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
|
2012-10-31 14:48:39 +00:00
|
|
|
|
2013-07-23 15:52:16 +00:00
|
|
|
canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
|
|
|
|
canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
|
2015-01-16 22:21:40 +00:00
|
|
|
canvas->drawText(kLabel10, strlen(kLabel10), 265, 10, textPaint);
|
2012-10-31 14:48:39 +00:00
|
|
|
|
|
|
|
canvas->translate(80, 20);
|
|
|
|
|
2012-07-30 18:20:12 +00:00
|
|
|
// since we draw into this directly, we need to start fresh
|
|
|
|
sk_bzero(fBuffer, fBufferSize);
|
|
|
|
|
2014-01-15 02:38:22 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB));
|
|
|
|
sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info));
|
|
|
|
sk_sp<SkSurface> surf2; // gpu
|
2012-10-31 14:48:39 +00:00
|
|
|
|
2015-01-16 22:21:40 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-03-24 01:59:25 +00:00
|
|
|
surf2 = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kNo, info);
|
2012-10-31 14:48:39 +00:00
|
|
|
#endif
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf0.get(), true);
|
2013-07-23 15:52:16 +00:00
|
|
|
canvas->translate(80, 0);
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf1.get(), true);
|
2015-01-16 22:21:40 +00:00
|
|
|
if (surf2) {
|
2012-10-31 14:48:39 +00:00
|
|
|
canvas->translate(80, 0);
|
2016-03-24 01:59:25 +00:00
|
|
|
test_surface(canvas, surf2.get(), true);
|
2012-10-31 14:48:39 +00:00
|
|
|
}
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
2015-01-16 22:21:40 +00:00
|
|
|
DEF_GM( return new ImageGM; )
|
2015-11-23 20:32:16 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
|
|
|
|
static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
|
|
|
|
SkBitmap bitmap;
|
2015-12-09 19:36:59 +00:00
|
|
|
bitmap.installPixels(pmap);
|
2015-11-23 20:32:16 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->drawImage(image, 0, 0, nullptr);
|
|
|
|
canvas->translate(110, 10);
|
|
|
|
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
|
|
|
|
SkAutoPixmapStorage storage;
|
|
|
|
storage.alloc(info);
|
|
|
|
|
|
|
|
const SkImage::CachingHint chints[] = {
|
2015-11-24 19:44:47 +00:00
|
|
|
SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint,
|
2015-11-23 20:32:16 +00:00
|
|
|
};
|
|
|
|
const SkFilterQuality qualities[] = {
|
|
|
|
kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto ch : chints) {
|
|
|
|
canvas->save();
|
|
|
|
for (auto q : qualities) {
|
|
|
|
if (image->scalePixels(storage, q, ch)) {
|
|
|
|
draw_pixmap(canvas, storage);
|
|
|
|
}
|
|
|
|
canvas->translate(70, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, 45);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_contents(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(20);
|
|
|
|
canvas->drawCircle(50, 50, 35, paint);
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
|
2015-11-23 20:32:16 +00:00
|
|
|
SkPictureRecorder recorder;
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
|
|
|
info.dimensions(), nullptr, nullptr);
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
|
|
|
|
sk_sp<SkImage> image(make_raster(info, nullptr, draw));
|
|
|
|
sk_sp<SkData> data(image->encode());
|
|
|
|
return SkImage::MakeFromEncoded(data);
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) {
|
2015-11-23 20:32:16 +00:00
|
|
|
if (!ctx) { return nullptr; }
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
|
2016-05-13 14:25:44 +00:00
|
|
|
if (!surface) { return nullptr; }
|
2015-12-02 22:19:47 +00:00
|
|
|
draw(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*));
|
2015-11-23 20:32:16 +00:00
|
|
|
|
|
|
|
class ScalePixelsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ScalePixelsGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("scale-pixels");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(960, 1200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
|
|
|
|
const ImageMakerProc procs[] = {
|
2015-11-24 19:44:47 +00:00
|
|
|
make_codec, make_raster, make_picture, make_codec, make_gpu,
|
2015-11-23 20:32:16 +00:00
|
|
|
};
|
|
|
|
for (auto& proc : procs) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(proc(info, canvas->getGrContext(), draw_contents));
|
2015-11-23 20:32:16 +00:00
|
|
|
if (image) {
|
2016-03-17 17:51:11 +00:00
|
|
|
show_scaled_pixels(canvas, image.get());
|
2015-11-23 20:32:16 +00:00
|
|
|
}
|
|
|
|
canvas->translate(0, 120);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-11-23 20:32:16 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ScalePixelsGM; )
|
2015-12-02 22:19:47 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "SkImageGenerator.h"
|
|
|
|
|
|
|
|
static SkImageInfo make_info(SkImage* img) {
|
2016-08-17 21:01:05 +00:00
|
|
|
return SkImageInfo::MakeN32(img->width(), img->height(), img->alphaType());
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Its simple, but I wonder if we should expose this formally?
|
|
|
|
//
|
|
|
|
class ImageGeneratorFromImage : public SkImageGenerator {
|
|
|
|
public:
|
|
|
|
ImageGeneratorFromImage(SkImage* img) : INHERITED(make_info(img)), fImg(SkRef(img)) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[],
|
|
|
|
int* ctableCount) override {
|
|
|
|
return fImg->readPixels(info, pixels, rowBytes, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<SkImage> fImg;
|
2015-12-02 22:19:47 +00:00
|
|
|
|
|
|
|
typedef SkImageGenerator INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void draw_opaque_contents(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(0xFFFF8844);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(20);
|
|
|
|
canvas->drawCircle(50, 50, 35, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkImageGenerator* gen_raster(const SkImageInfo& info) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-12-02 22:19:47 +00:00
|
|
|
draw_opaque_contents(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return new ImageGeneratorFromImage(surface->makeImageSnapshot().get());
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkImageGenerator* gen_picture(const SkImageInfo& info) {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
draw_opaque_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
|
|
|
|
return SkImageGenerator::NewFromPicture(info.dimensions(), pict.get(), nullptr, nullptr);
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkImageGenerator* gen_png(const SkImageInfo& info) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(make_raster(info, nullptr, draw_opaque_contents));
|
2016-11-08 14:29:46 +00:00
|
|
|
sk_sp<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
|
2016-08-03 20:32:32 +00:00
|
|
|
return SkImageGenerator::NewFromEncoded(data.get());
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkImageGenerator* gen_jpg(const SkImageInfo& info) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(make_raster(info, nullptr, draw_opaque_contents));
|
2016-11-08 14:29:46 +00:00
|
|
|
sk_sp<SkData> data(image->encode(SkImageEncoder::kJPEG_Type, 100));
|
2016-08-03 20:32:32 +00:00
|
|
|
return SkImageGenerator::NewFromEncoded(data.get());
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef SkImageGenerator* (*GeneratorMakerProc)(const SkImageInfo&);
|
|
|
|
|
|
|
|
static void show_scaled_generator(SkCanvas* canvas, SkImageGenerator* gen) {
|
|
|
|
const SkImageInfo genInfo = gen->getInfo();
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(genInfo);
|
|
|
|
if (gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())) {
|
|
|
|
canvas->drawBitmap(bm, 0, 0, nullptr);
|
|
|
|
}
|
|
|
|
canvas->translate(110, 0);
|
|
|
|
|
|
|
|
const float scales[] = { 0.75f, 0.5f, 0.25f };
|
|
|
|
for (auto scale : scales) {
|
|
|
|
SkImageGenerator::SupportedSizes sizes;
|
|
|
|
if (gen->computeScaledDimensions(scale, &sizes)) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(sizes.fSizes[0].width(),
|
|
|
|
sizes.fSizes[0].height());
|
|
|
|
bm.allocPixels(info);
|
|
|
|
SkPixmap pmap;
|
|
|
|
bm.peekPixels(&pmap);
|
|
|
|
if (gen->generateScaledPixels(pmap)) {
|
|
|
|
canvas->drawBitmap(bm, 0, SkIntToScalar(genInfo.height() - info.height())/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->translate(100, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScaleGeneratorGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ScaleGeneratorGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("scale-generator");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(500, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
|
|
|
|
// explicitly make it opaque, so we can test JPEG (which is only ever opaque)
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(100, 100, kOpaque_SkAlphaType);
|
|
|
|
|
|
|
|
const GeneratorMakerProc procs[] = {
|
|
|
|
gen_raster, gen_picture, gen_png, gen_jpg,
|
|
|
|
};
|
|
|
|
for (auto& proc : procs) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkImageGenerator> gen(proc(info));
|
2015-12-02 22:19:47 +00:00
|
|
|
if (gen) {
|
2016-11-03 18:40:50 +00:00
|
|
|
show_scaled_generator(canvas, gen.get());
|
2015-12-02 22:19:47 +00:00
|
|
|
}
|
|
|
|
canvas->translate(0, 120);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ScaleGeneratorGM; )
|
2016-01-30 18:01:40 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContextFactory.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) {
|
|
|
|
GrContext* context = nullptr;
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
context = canvas->getGrContext();
|
2016-03-31 01:56:19 +00:00
|
|
|
sk_gpu_test::GrContextFactory factory;
|
2016-01-30 18:01:40 +00:00
|
|
|
#endif
|
|
|
|
if (!context) {
|
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto render_image = [](SkCanvas* canvas) {
|
|
|
|
canvas->clear(SK_ColorBLUE);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kSize = 50;
|
2016-01-30 18:01:40 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocN32Pixels(kSize, kSize);
|
|
|
|
SkCanvas bmpCanvas(bmp);
|
|
|
|
render_image(&bmpCanvas);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
std::function<sk_sp<SkImage>()> imageFactories[] = {
|
2016-01-30 18:01:40 +00:00
|
|
|
// Create sw raster image.
|
|
|
|
[bmp] {
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromBitmap(bmp);
|
2016-01-30 18:01:40 +00:00
|
|
|
},
|
|
|
|
// Create encoded image.
|
|
|
|
[bmp] {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkData> src(
|
2016-01-30 18:01:40 +00:00
|
|
|
SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100));
|
2016-03-17 17:51:11 +00:00
|
|
|
return SkImage::MakeFromEncoded(std::move(src));
|
2016-01-30 18:01:40 +00:00
|
|
|
},
|
|
|
|
// Create a picture image.
|
|
|
|
[render_image] {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
|
|
|
|
render_image(canvas);
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
|
|
|
SkISize::Make(kSize, kSize), nullptr, nullptr);
|
2016-01-30 18:01:40 +00:00
|
|
|
},
|
|
|
|
// Create a texture image
|
2016-03-17 17:51:11 +00:00
|
|
|
[context, render_image]() -> sk_sp<SkImage> {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(
|
|
|
|
SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
|
|
|
|
SkImageInfo::MakeN32Premul(kSize, kSize)));
|
2016-01-30 18:01:40 +00:00
|
|
|
if (!surface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
render_image(surface->getCanvas());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2016-01-30 18:01:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kPad = 5.f;
|
2016-01-30 18:01:40 +00:00
|
|
|
canvas->translate(kPad, kPad);
|
|
|
|
for (auto factory : imageFactories) {
|
2016-03-17 17:51:11 +00:00
|
|
|
auto image(factory());
|
2016-01-30 18:01:40 +00:00
|
|
|
if (!image) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (context) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> texImage(image->makeTextureImage(context));
|
2016-01-30 18:01:40 +00:00
|
|
|
if (texImage) {
|
|
|
|
canvas->drawImage(texImage, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->translate(image->width() + kPad, 0);
|
|
|
|
}
|
|
|
|
}
|