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"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkCanvas.h"
|
2012-07-31 03:57:11 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
|
2012-10-31 14:48:39 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
2012-08-28 12:19:02 +00:00
|
|
|
extern GrContext* GetGr();
|
2012-10-31 14:48:39 +00:00
|
|
|
};
|
|
|
|
#endif
|
2012-08-28 12:19:02 +00:00
|
|
|
|
2012-07-31 03:57:11 +00:00
|
|
|
static SkData* fileToData(const char path[]) {
|
|
|
|
SkFILEStream stream(path);
|
|
|
|
if (!stream.isValid()) {
|
|
|
|
return SkData::NewEmpty();
|
|
|
|
}
|
|
|
|
size_t size = stream.getLength();
|
|
|
|
void* mem = sk_malloc_throw(size);
|
|
|
|
stream.read(mem, size);
|
|
|
|
return SkData::NewFromMalloc(mem, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
|
|
|
|
SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
|
|
|
|
SkImage* image = SkImage::NewEncodedData(data);
|
|
|
|
if (image) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->scale(size.width() * 1.0f / image->width(),
|
|
|
|
size.height() * 1.0f / image->height());
|
|
|
|
image->draw(canvas,0, 0, NULL);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_surface(SkCanvas* canvas, SkSurface* surf) {
|
|
|
|
drawContents(surf, SK_ColorRED);
|
|
|
|
SkImage* imgR = surf->newImageShapshot();
|
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
if (true) {
|
|
|
|
SkImage* imgR2 = surf->newImageShapshot();
|
|
|
|
SkASSERT(imgR == imgR2);
|
|
|
|
imgR2->unref();
|
|
|
|
}
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
drawContents(surf, SK_ColorGREEN);
|
|
|
|
SkImage* imgG = surf->newImageShapshot();
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
imgR->draw(canvas, 0, 0, &paint);
|
|
|
|
imgG->draw(canvas, 0, 80, &paint);
|
|
|
|
surf->draw(canvas, 0, 160, &paint);
|
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:
|
|
|
|
virtual SkString onShortName() {
|
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
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
virtual SkISize onISize() {
|
2012-10-31 14:48:39 +00:00
|
|
|
return SkISize::Make(800, 500);
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
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";
|
|
|
|
|
|
|
|
static const char* kLabel4 = "Pre-Alloc Img";
|
|
|
|
static const char* kLabel5 = "New Alloc Img";
|
|
|
|
static const char* kLabel6 = "SkPicture";
|
|
|
|
static const char* kLabel7 = "GPU";
|
|
|
|
|
|
|
|
SkPaint textPaint;
|
|
|
|
|
|
|
|
canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
|
|
|
|
canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
|
|
|
|
canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
|
|
|
|
|
|
|
|
canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint);
|
|
|
|
canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint);
|
|
|
|
canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint);
|
|
|
|
canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
SkImage::Info info;
|
|
|
|
|
|
|
|
info.fWidth = W;
|
|
|
|
info.fHeight = H;
|
|
|
|
info.fColorType = SkImage::kPMColor_ColorType;
|
|
|
|
info.fAlphaType = SkImage::kPremul_AlphaType;
|
2012-11-15 02:37:45 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
|
|
|
|
SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
|
2012-07-29 20:38:16 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
|
2012-10-31 14:48:39 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
GrContext* ctx = skiagm::GetGr();
|
|
|
|
|
2012-11-15 02:37:45 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0));
|
2012-10-31 14:48:39 +00:00
|
|
|
#endif
|
2012-07-29 20:38:16 +00:00
|
|
|
|
|
|
|
test_surface(canvas, surf0);
|
|
|
|
canvas->translate(80, 0);
|
|
|
|
test_surface(canvas, surf1);
|
|
|
|
canvas->translate(80, 0);
|
|
|
|
test_surface(canvas, surf2);
|
2012-10-31 14:48:39 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (NULL != ctx) {
|
|
|
|
canvas->translate(80, 0);
|
|
|
|
test_surface(canvas, surf3);
|
|
|
|
}
|
|
|
|
#endif
|
2012-07-29 20:38:16 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-28 12:19:02 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
2012-08-28 15:54:54 +00:00
|
|
|
return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
|
2012-08-28 12:19:02 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-29 20:38:16 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static skiagm::GM* MyFactory(void*) { return new ImageGM; }
|
|
|
|
static skiagm::GMRegistry reg(MyFactory);
|
2012-08-23 18:19:56 +00:00
|
|
|
|