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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.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.
|
2014-09-18 20:57:05 +00:00
|
|
|
SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
|
|
|
|
if (NULL == data.get()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-22 19:48:26 +00:00
|
|
|
SkImage* image = SkImage::NewFromEncoded(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());
|
2014-09-24 20:03:30 +00:00
|
|
|
canvas->drawImage(image, 0, 0, NULL);
|
2012-07-31 03:57:11 +00:00
|
|
|
image->unref();
|
|
|
|
}
|
|
|
|
}
|
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);
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* imgR = surf->newImageSnapshot();
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
if (true) {
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* imgR2 = surf->newImageSnapshot();
|
2012-08-28 12:19:02 +00:00
|
|
|
SkASSERT(imgR == imgR2);
|
|
|
|
imgR2->unref();
|
|
|
|
}
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
drawContents(surf, SK_ColorGREEN);
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* imgG = surf->newImageSnapshot();
|
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);
|
|
|
|
|
2014-09-24 20:03:30 +00:00
|
|
|
canvas->drawImage(imgR, 0, 0, usePaint ? &paint : NULL);
|
|
|
|
canvas->drawImage(imgG, 0, 80, usePaint ? &paint : NULL);
|
2013-07-23 15:52:16 +00:00
|
|
|
surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-09-24 20:03:30 +00:00
|
|
|
canvas->drawImageRect(imgR, &src1, dst1, usePaint ? &paint : NULL);
|
|
|
|
canvas->drawImageRect(imgG, &src2, dst2, usePaint ? &paint : NULL);
|
|
|
|
canvas->drawImageRect(imgR, &src3, dst3, usePaint ? &paint : NULL);
|
|
|
|
canvas->drawImageRect(imgG, NULL, dst4, usePaint ? &paint : NULL);
|
2012-07-29 20:38:16 +00:00
|
|
|
|
|
|
|
imgG->unref();
|
|
|
|
imgR->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-10-31 14:48:39 +00:00
|
|
|
static const char* kLabel1 = "Original Img";
|
|
|
|
static const char* kLabel2 = "Modified Img";
|
|
|
|
static const char* kLabel3 = "Cur Surface";
|
2013-07-23 15:52:16 +00:00
|
|
|
static const char* kLabel4 = "Full Crop";
|
|
|
|
static const char* kLabel5 = "Over-crop";
|
|
|
|
static const char* kLabel6 = "Upper-left";
|
|
|
|
static const char* kLabel7 = "No Crop";
|
2012-10-31 14:48:39 +00:00
|
|
|
|
2013-07-23 15:52:16 +00:00
|
|
|
static const char* kLabel8 = "Pre-Alloc Img";
|
|
|
|
static const char* kLabel9 = "New Alloc Img";
|
2015-01-16 22:21:40 +00:00
|
|
|
static 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-15 21:23:50 +00:00
|
|
|
sk_tool_utils::set_portable_typeface_always(&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);
|
2012-11-15 02:37:45 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
|
|
|
|
SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
|
2015-01-16 22:21:40 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf2; // gpu
|
2012-10-31 14:48:39 +00:00
|
|
|
|
2015-01-16 22:21:40 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
surf2.reset(SkSurface::NewRenderTarget(canvas->getGrContext(),
|
|
|
|
SkSurface::kNo_Budgeted, info));
|
2012-10-31 14:48:39 +00:00
|
|
|
#endif
|
2012-07-29 20:38:16 +00:00
|
|
|
|
2013-07-23 15:52:16 +00:00
|
|
|
test_surface(canvas, surf0, true);
|
|
|
|
canvas->translate(80, 0);
|
|
|
|
test_surface(canvas, surf1, true);
|
2015-01-16 22:21:40 +00:00
|
|
|
if (surf2) {
|
2012-10-31 14:48:39 +00:00
|
|
|
canvas->translate(80, 0);
|
2015-01-16 22:21:40 +00:00
|
|
|
test_surface(canvas, surf2, 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; )
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-01-23 13:58:07 +00:00
|
|
|
class ImageResizeGM : public skiagm::GM {
|
|
|
|
enum {
|
|
|
|
W = 100,
|
|
|
|
H = 100,
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
ImageResizeGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override { return SkString("image-resize"); }
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(510, 480); }
|
2015-01-23 13:58:07 +00:00
|
|
|
|
|
|
|
void drawIntoImage(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(3);
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < 60; ++i) {
|
|
|
|
paint.setColor(rand.nextU());
|
|
|
|
SkScalar x = rand.nextUScalar1() * W;
|
|
|
|
SkScalar y = rand.nextUScalar1() * H;
|
|
|
|
SkScalar r = rand.nextUScalar1() * W / 2;
|
|
|
|
canvas->drawCircle(x, y, r, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkImage* makeImage(SkCanvas* canvas) {
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
|
|
|
|
SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
|
|
|
|
if (!surface) {
|
|
|
|
surface.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
|
|
|
this->drawIntoImage(surface->getCanvas());
|
|
|
|
return surface->newImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawResized(SkCanvas* canvas, SkImage* image, int newW, int newH, const SkIRect* subset,
|
|
|
|
SkFilterQuality fq) {
|
|
|
|
// canvas method
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(fq);
|
|
|
|
SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH));
|
|
|
|
SkRect srcR;
|
|
|
|
if (subset) {
|
|
|
|
srcR.set(*subset);
|
|
|
|
}
|
|
|
|
canvas->drawImageRect(image, subset ? &srcR : NULL, dstR, &paint);
|
|
|
|
canvas->translate(newW + 20.0f, 0);
|
|
|
|
|
|
|
|
// image method
|
|
|
|
SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq));
|
|
|
|
canvas->drawImage(image2, 0, 0, NULL);
|
|
|
|
canvas->translate(image2->width() + 20.0f, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawImage(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) {
|
|
|
|
|
|
|
|
canvas->drawImage(image, 0, 0, NULL);
|
|
|
|
canvas->translate(image->width() + 20.0f, 0);
|
|
|
|
this->drawResized(canvas, image, image->width()*4/10, image->height()*4/10, NULL, fq);
|
|
|
|
|
|
|
|
SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2);
|
|
|
|
this->drawResized(canvas, image, W, H, &subset, fq);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-01-23 13:58:07 +00:00
|
|
|
canvas->translate(10, 10);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkImage> image(this->makeImage(canvas));
|
|
|
|
|
|
|
|
const SkFilterQuality fq[] = {
|
|
|
|
kNone_SkFilterQuality,
|
|
|
|
kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality,
|
|
|
|
kHigh_SkFilterQuality,
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) {
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
this->drawImage(canvas, image, fq[i]);
|
|
|
|
}
|
|
|
|
canvas->translate(0, image->height() + 20.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ImageResizeGM; )
|
|
|
|
|