2014-09-22 14:29:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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 "SkGradientShader.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkSurfaceProps.h"
|
|
|
|
|
|
|
|
#define W 200
|
|
|
|
#define H 100
|
|
|
|
|
|
|
|
static SkShader* make_shader() {
|
|
|
|
int a = 0x99;
|
|
|
|
int b = 0xBB;
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { W, H } };
|
|
|
|
SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
|
2015-08-27 14:41:13 +00:00
|
|
|
return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2014-09-22 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkSurface* make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo,
|
|
|
|
int disallowAA, int disallowDither) {
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (disallowAA) {
|
|
|
|
flags |= SkSurfaceProps::kDisallowAntiAlias_Flag;
|
|
|
|
}
|
|
|
|
if (disallowDither) {
|
|
|
|
flags |= SkSurfaceProps::kDisallowDither_Flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSurfaceProps props(flags, geo);
|
|
|
|
if (ctx) {
|
2015-01-16 15:32:33 +00:00
|
|
|
return SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, &props);
|
2014-09-22 14:29:03 +00:00
|
|
|
} else {
|
|
|
|
return SkSurface::NewRaster(info, &props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_draw(SkCanvas* canvas, const char label[]) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setDither(true);
|
|
|
|
|
|
|
|
paint.setShader(make_shader())->unref();
|
|
|
|
canvas->drawRect(SkRect::MakeWH(W, H), paint);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-09-22 14:29:03 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
paint.setTextSize(32);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
2015-07-28 17:37:53 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2014-09-22 14:29:03 +00:00
|
|
|
canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SurfacePropsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
SurfacePropsGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-09-22 14:29:03 +00:00
|
|
|
return SkString("surfaceprops");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-09-22 14:29:03 +00:00
|
|
|
return SkISize::Make(W * 4, H * 5);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-09-22 14:29:03 +00:00
|
|
|
GrContext* ctx = canvas->getGrContext();
|
|
|
|
|
|
|
|
// must be opaque to have a hope of testing LCD text
|
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
SkPixelGeometry fGeo;
|
|
|
|
const char* fLabel;
|
|
|
|
} rec[] = {
|
|
|
|
{ kUnknown_SkPixelGeometry, "Unknown" },
|
|
|
|
{ kRGB_H_SkPixelGeometry, "RGB_H" },
|
|
|
|
{ kBGR_H_SkPixelGeometry, "BGR_H" },
|
|
|
|
{ kRGB_V_SkPixelGeometry, "RGB_V" },
|
|
|
|
{ kBGR_V_SkPixelGeometry, "BGR_V" },
|
|
|
|
};
|
|
|
|
|
|
|
|
SkScalar x = 0;
|
|
|
|
for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) {
|
|
|
|
for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) {
|
|
|
|
SkScalar y = 0;
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
|
|
|
|
SkAutoTUnref<SkSurface> surface(make_surface(ctx, info, rec[i].fGeo,
|
|
|
|
disallowAA, disallowDither));
|
|
|
|
test_draw(surface->getCanvas(), rec[i].fLabel);
|
2015-08-27 14:41:13 +00:00
|
|
|
surface->draw(canvas, x, y, nullptr);
|
2014-09-22 14:29:03 +00:00
|
|
|
y += H;
|
|
|
|
}
|
|
|
|
x += W;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new SurfacePropsGM )
|
2014-11-21 16:46:37 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
|
|
|
|
return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class NewSurfaceGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
NewSurfaceGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-11-21 16:46:37 +00:00
|
|
|
return SkString("surfacenew");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-11-21 16:46:37 +00:00
|
|
|
return SkISize::Make(300, 140);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawInto(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-11-21 16:46:37 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf(canvas->newSurface(info, nullptr));
|
2014-11-21 16:46:37 +00:00
|
|
|
if (!surf.get()) {
|
|
|
|
surf.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
|
|
|
drawInto(surf->getCanvas());
|
|
|
|
|
|
|
|
SkAutoTUnref<SkImage> image(surf->newImageSnapshot());
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImage(image, 10, 10, nullptr);
|
2014-11-21 16:46:37 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf2(image->newSurface(info, nullptr));
|
2014-11-21 16:46:37 +00:00
|
|
|
drawInto(surf2->getCanvas());
|
|
|
|
|
|
|
|
// Assert that the props were communicated transitively through the first image
|
|
|
|
SkASSERT(equal(surf->props(), surf2->props()));
|
|
|
|
|
|
|
|
SkAutoTUnref<SkImage> image2(surf2->newImageSnapshot());
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImage(image2, 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
|
2014-11-21 16:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new NewSurfaceGM )
|
|
|
|
|