skia2/samplecode/SampleDrawBitmap.cpp
epoger@google.com fd03db0fe9 Manually update copyright notices in files that defied the automated process
used in http://codereview.appspot.com/4816058/ ('Automatic update of all copyright notices to reflect new license terms.')

Note that the following files have been left alone, because they name
copyright holders outside of Google.  Please let me know if we need to update
these files:

experimental/CiCarbonSampleMain.c
src/opts/memset16_neon.S
src/opts/memset32_neon.S
src/opts/opts_check_arm.cpp
src/ports/SkDebug_brew.cpp
src/ports/SkMemory_brew.cpp
src/ports/SkOSFile_brew.cpp
Review URL: http://codereview.appspot.com/4806054

git-svn-id: http://skia.googlecode.com/svn/trunk@1981 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:24:55 +00:00

85 lines
2.4 KiB
C++

/*
* 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 "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkShader.h"
#include "SkUtils.h"
#include "SkDevice.h"
static void create_bitmap(SkBitmap* bitmap) {
const int W = 100;
const int H = 100;
bitmap->setConfig(SkBitmap::kARGB_8888_Config, W, H);
bitmap->allocPixels();
SkCanvas canvas(*bitmap);
canvas.drawColor(SK_ColorRED);
SkPaint paint;
paint.setColor(SK_ColorBLUE);
canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint);
}
class DrawBitmapView : public SampleView {
SkPath fPath;
public:
DrawBitmapView() {}
protected:
// overrides from SkEventSink
virtual bool onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "DrawBitmap");
return true;
}
return this->INHERITED::onQuery(evt);
}
virtual void onDrawContent(SkCanvas* canvas) {
SkBitmap bitmap;
create_bitmap(&bitmap);
int x = bitmap.width() / 2;
int y = bitmap.height() / 2;
SkBitmap subset;
bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y));
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
canvas->drawBitmap(bitmap, 0, 0);
canvas->drawBitmap(subset, 0, 0);
// Now do the same but with a device bitmap as source image
SkRefPtr<SkDevice> primaryDevice(canvas->getDevice());
SkRefPtr<SkDevice> secondDevice(canvas->createCompatibleDevice(
SkBitmap::kARGB_8888_Config, bitmap.width(),
bitmap.height(), true));
secondDevice->unref();
SkCanvas secondCanvas(secondDevice.get());
secondCanvas.writePixels(bitmap, 0, 0);
SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
SkBitmap deviceSubset;
deviceBitmap.extractSubset(&deviceSubset,
SkIRect::MakeXYWH(x, y, x, y));
canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
canvas->drawBitmap(deviceBitmap, 0, 0);
canvas->drawBitmap(deviceSubset, 0, 0);
}
private:
typedef SampleView INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new DrawBitmapView; }
static SkViewRegister reg(MyFactory);