2012-08-07 14:05:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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"
|
2013-10-24 17:52:07 +00:00
|
|
|
#include "SkGradientShader.h"
|
2012-08-07 14:05:14 +00:00
|
|
|
#include "SkPath.h"
|
2015-09-09 18:29:09 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2015-09-09 17:45:36 +00:00
|
|
|
#include "SkSurface.h"
|
2012-08-07 14:05:14 +00:00
|
|
|
|
2015-09-09 18:29:09 +00:00
|
|
|
static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
|
2015-09-09 17:45:36 +00:00
|
|
|
const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
|
|
|
|
const SkScalar radius = 40;
|
2013-10-24 17:52:07 +00:00
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2015-09-09 18:29:09 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(width, height));
|
2015-09-09 18:29:09 +00:00
|
|
|
draw(surface->getCanvas(), width, height, colors);
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2012-08-07 14:05:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
|
2015-09-09 18:29:09 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
|
2016-03-18 14:25:55 +00:00
|
|
|
return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
|
2016-12-16 16:55:18 +00:00
|
|
|
SkISize::Make(width, height), nullptr, nullptr,
|
2017-01-09 17:38:59 +00:00
|
|
|
SkImage::BitDepth::kU8,
|
2017-02-07 18:56:11 +00:00
|
|
|
SkColorSpace::MakeSRGB());
|
2015-09-09 18:29:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
|
2015-09-09 18:29:09 +00:00
|
|
|
|
|
|
|
static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
|
|
|
|
ImageMakerProc proc) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(proc(width, height, colors));
|
2012-08-07 14:05:14 +00:00
|
|
|
|
2016-11-14 19:41:58 +00:00
|
|
|
SkPaint borderPaint;
|
2012-08-07 14:05:14 +00:00
|
|
|
|
2016-11-14 19:41:58 +00:00
|
|
|
borderPaint.setStyle(SkPaint::kStroke_Style);
|
2012-08-07 14:05:14 +00:00
|
|
|
|
2016-11-14 19:41:58 +00:00
|
|
|
SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
|
2012-08-07 14:05:14 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2016-11-14 19:41:58 +00:00
|
|
|
canvas->clipRect(dstRect);
|
2015-09-09 17:45:36 +00:00
|
|
|
canvas->drawImage(image, 0, 0, nullptr);
|
2012-08-07 14:05:14 +00:00
|
|
|
canvas->restore();
|
2016-11-14 19:41:58 +00:00
|
|
|
canvas->drawRect(dstRect, borderPaint);
|
|
|
|
|
|
|
|
dstRect.offset(SkIntToScalar(150), 0);
|
|
|
|
int hw = width / 2;
|
|
|
|
int hh = height / 2;
|
|
|
|
SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
|
|
|
|
canvas->drawImageRect(image, subset, dstRect, nullptr);
|
|
|
|
canvas->drawRect(dstRect, borderPaint);
|
|
|
|
|
|
|
|
dstRect.offset(SkIntToScalar(150), 0);
|
|
|
|
canvas->drawImageRect(image, dstRect, nullptr);
|
|
|
|
canvas->drawRect(dstRect, borderPaint);
|
2012-08-07 14:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class VeryLargeBitmapGM : public skiagm::GM {
|
2015-09-09 18:29:09 +00:00
|
|
|
ImageMakerProc fProc;
|
|
|
|
SkString fName;
|
|
|
|
|
2012-08-07 14:05:14 +00:00
|
|
|
public:
|
2015-09-09 18:29:09 +00:00
|
|
|
VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
|
|
|
|
fName.printf("verylarge%s", suffix);
|
|
|
|
}
|
2012-08-07 14:05:14 +00:00
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-09-09 18:29:09 +00:00
|
|
|
return fName;
|
2012-08-07 14:05:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-10-24 17:52:07 +00:00
|
|
|
return SkISize::Make(500, 600);
|
2012-08-07 14:05:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-10-24 20:55:14 +00:00
|
|
|
int veryBig = 65*1024; // 64K < size
|
|
|
|
int big = 33*1024; // 32K < size < 64K
|
2013-10-24 17:52:07 +00:00
|
|
|
// smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
|
2013-10-24 20:55:14 +00:00
|
|
|
int medium = 5*1024;
|
2013-07-15 15:48:48 +00:00
|
|
|
int small = 150;
|
2012-08-07 14:05:14 +00:00
|
|
|
|
2013-10-24 17:52:07 +00:00
|
|
|
SkColor colors[2];
|
|
|
|
|
2012-08-07 14:05:14 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
2013-10-24 17:52:07 +00:00
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorGREEN;
|
2015-09-09 18:29:09 +00:00
|
|
|
show_image(canvas, small, small, colors, fProc);
|
2013-10-24 17:52:07 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(150));
|
|
|
|
|
|
|
|
colors[0] = SK_ColorBLUE;
|
|
|
|
colors[1] = SK_ColorMAGENTA;
|
2015-09-09 18:29:09 +00:00
|
|
|
show_image(canvas, big, small, colors, fProc);
|
2012-08-07 14:05:14 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(150));
|
|
|
|
|
2013-10-24 17:52:07 +00:00
|
|
|
colors[0] = SK_ColorMAGENTA;
|
|
|
|
colors[1] = SK_ColorYELLOW;
|
2015-09-09 18:29:09 +00:00
|
|
|
show_image(canvas, medium, medium, colors, fProc);
|
2012-08-07 14:05:14 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(150));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-10-24 17:52:07 +00:00
|
|
|
colors[0] = SK_ColorGREEN;
|
|
|
|
colors[1] = SK_ColorYELLOW;
|
2018-12-18 15:13:52 +00:00
|
|
|
// This used to be big enough that we didn't draw on CPU, but now we do.
|
2015-09-09 18:29:09 +00:00
|
|
|
show_image(canvas, veryBig, small, colors, fProc);
|
2012-08-07 14:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
2015-09-09 18:29:09 +00:00
|
|
|
DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
|
|
|
|
DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )
|