2011-07-28 18:26:00 +04: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.
|
|
|
|
*/
|
2008-12-17 18:59:43 +03:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
2012-08-02 18:03:32 +04:00
|
|
|
#if SK_SUPPORT_GPU
|
2011-07-06 17:59:47 +04:00
|
|
|
#include "SkGpuDevice.h"
|
2012-08-02 18:03:32 +04:00
|
|
|
#else
|
|
|
|
class GrContext;
|
|
|
|
#endif
|
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
#define INT_SIZE 64
|
|
|
|
#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
|
2011-07-06 17:59:47 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
static void make_bitmap(SkBitmap* bitmap) {
|
|
|
|
bitmap->setConfig(SkBitmap::kARGB_8888_Config, INT_SIZE, INT_SIZE);
|
|
|
|
bitmap->allocPixels();
|
|
|
|
SkCanvas canvas(*bitmap);
|
2011-07-06 17:59:47 +04:00
|
|
|
|
2009-10-15 22:51:46 +04:00
|
|
|
canvas.drawColor(SK_ColorRED);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-09-25 19:37:50 +04:00
|
|
|
const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
|
2009-10-15 22:51:46 +04:00
|
|
|
const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
|
2011-07-06 17:59:47 +04:00
|
|
|
SkShader::kClamp_TileMode))->unref();
|
2012-09-25 19:37:50 +04:00
|
|
|
canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPoint unit_vec(int degrees) {
|
|
|
|
SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
|
|
|
|
SkScalar s, c;
|
|
|
|
s = SkScalarSinCos(rad, &c);
|
|
|
|
return SkPoint::Make(c, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
|
|
|
|
*value += *delta;
|
|
|
|
if (*value < min) {
|
|
|
|
*value = min;
|
|
|
|
*delta = - *delta;
|
|
|
|
} else if (*value > max) {
|
|
|
|
*value = max;
|
|
|
|
*delta = - *delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
|
|
|
|
bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
|
|
|
|
bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
|
2009-10-15 22:51:46 +04:00
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2011-04-22 18:10:48 +04:00
|
|
|
class BitmapRectView : public SampleView {
|
2012-09-25 19:37:50 +04:00
|
|
|
SkPoint fSrcPts[2];
|
|
|
|
SkPoint fSrcVec[2];
|
|
|
|
SkRect fSrcLimit;
|
|
|
|
SkRect fDstR[2];
|
|
|
|
|
|
|
|
void bounce() {
|
|
|
|
bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
|
|
|
|
bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
|
|
|
|
}
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2012-08-23 22:19:56 +04:00
|
|
|
BitmapRectView() {
|
2011-04-22 18:10:48 +04:00
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2012-09-25 19:37:50 +04:00
|
|
|
|
|
|
|
fSrcPts[0].set(0, 0);
|
|
|
|
fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
|
|
|
|
|
|
|
|
fSrcVec[0] = unit_vec(30);
|
|
|
|
fSrcVec[1] = unit_vec(107);
|
|
|
|
|
|
|
|
fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
|
|
|
|
SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
|
|
|
|
|
|
|
|
fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
|
|
|
|
SkIntToScalar(250), SkIntToScalar(300));
|
|
|
|
fDstR[1] = fDstR[0];
|
|
|
|
fDstR[1].offset(fDstR[0].width() * 5/4, 0);
|
|
|
|
|
|
|
|
fSrcPts[0].set(32, 32);
|
|
|
|
fSrcPts[1].set(90, 90);
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2009-10-15 22:51:46 +04:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "BitmapRect");
|
2008-12-17 18:59:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2011-04-22 18:10:48 +04:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2012-09-25 19:37:50 +04:00
|
|
|
SkRect srcR;
|
|
|
|
srcR.set(fSrcPts[0], fSrcPts[1]);
|
|
|
|
srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
|
|
|
|
srcR.offset(-srcR.width()/2, -srcR.height()/2);
|
2009-10-15 22:51:46 +04:00
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
SkPaint paint;
|
2009-10-15 22:51:46 +04:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-09-25 19:37:50 +04:00
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2009-10-15 22:51:46 +04:00
|
|
|
|
2011-07-06 17:59:47 +04:00
|
|
|
SkBitmap bitmap;
|
2012-09-25 19:37:50 +04:00
|
|
|
make_bitmap(&bitmap);
|
|
|
|
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
|
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
|
|
|
canvas->drawRect(srcR, paint);
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
paint.setFilterBitmap(1 == i);
|
|
|
|
canvas->drawBitmapRectToRect(bitmap, &srcR, fDstR[i], &paint);
|
|
|
|
canvas->drawRect(fDstR[i], paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bounce();
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void make_big_bitmap(SkBitmap* bm) {
|
|
|
|
static const char gText[] =
|
|
|
|
"We the people, in order to form a more perfect union, establish justice,"
|
|
|
|
" ensure domestic tranquility, provide for the common defense, promote the"
|
|
|
|
" general welfare and ensure the blessings of liberty to ourselves and our"
|
|
|
|
" posterity, do ordain and establish this constitution for the United"
|
|
|
|
" States of America.";
|
|
|
|
|
|
|
|
const int BIG_H = 120;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(BIG_H));
|
|
|
|
|
|
|
|
const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
|
|
|
|
|
|
|
|
bm->setConfig(SkBitmap::kARGB_8888_Config, BIG_W, BIG_H);
|
|
|
|
bm->allocPixels();
|
|
|
|
bm->eraseColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
|
|
|
|
canvas.drawText(gText, strlen(gText), 0, paint.getTextSize()*4/5, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
class BitmapRectView2 : public SampleView {
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
SkRect fSrcR;
|
|
|
|
SkRect fLimitR;
|
|
|
|
SkScalar fDX;
|
|
|
|
SkRect fDstR[2];
|
|
|
|
|
|
|
|
void bounceMe() {
|
|
|
|
SkScalar width = fSrcR.width();
|
|
|
|
bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
|
|
|
|
fSrcR.fRight = fSrcR.fLeft + width;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
BitmapRectView2() {
|
|
|
|
make_big_bitmap(&fBitmap);
|
2011-07-06 17:59:47 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2009-10-15 22:51:46 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
fSrcR.fLeft = 0;
|
|
|
|
fSrcR.fTop = 0;
|
|
|
|
fSrcR.fRight = SkIntToScalar(fBitmap.height()) * 3;
|
|
|
|
fSrcR.fBottom = SkIntToScalar(fBitmap.height());
|
2010-12-20 21:26:13 +03:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
fLimitR.set(0, 0,
|
|
|
|
SkIntToScalar(fBitmap.width()),
|
|
|
|
SkIntToScalar(fBitmap.height()));
|
2009-10-15 22:51:46 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
fDX = SK_Scalar1;
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(20), SkIntToScalar(20),
|
|
|
|
SkIntToScalar(600), SkIntToScalar(200));
|
|
|
|
fDstR[1] = fDstR[0];
|
|
|
|
fDstR[1].offset(0, fDstR[0].height() * 5/4);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "BigBitmapRect");
|
|
|
|
return true;
|
2009-10-15 22:51:46 +04:00
|
|
|
}
|
2012-09-25 19:37:50 +04:00
|
|
|
return this->INHERITED::onQuery(evt);
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
2012-09-25 19:37:50 +04:00
|
|
|
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2012-08-23 22:19:56 +04:00
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
paint.setFilterBitmap(1 == i);
|
|
|
|
canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR[i], &paint);
|
|
|
|
canvas->drawRect(fDstR[i], paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->bounceMe();
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
2008-12-17 18:59:43 +03:00
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-09-25 19:37:50 +04:00
|
|
|
static SkView* F0() { return new BitmapRectView; }
|
|
|
|
static SkView* F1() { return new BitmapRectView2; }
|
|
|
|
static SkViewRegister gR0(F0);
|
|
|
|
static SkViewRegister gR1(F1);
|
2008-12-17 18:59:43 +03:00
|
|
|
|