2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPixmap.h"
12 #include "include/core/SkRasterHandleAllocator.h"
13 #include "include/core/SkSurface.h"
20 GraphicsPort(SkCanvas* canvas) : fCanvas(canvas) {}
21 virtual ~GraphicsPort() {}
23 void save() { fCanvas->save(); }
24 void saveLayer(const SkRect& bounds, SkAlpha alpha) {
25 fCanvas->saveLayerAlpha(&bounds, alpha);
27 void restore() { fCanvas->restore(); }
29 void translate(float x, float y) { fCanvas->translate(x, y); }
30 void scale(float s) { fCanvas->scale(s, s); }
31 void clip(const SkRect& r) { fCanvas->clipRect(r); }
33 void drawOval(const SkRect& r, SkColor c) {
36 fCanvas->drawOval(r, p);
39 virtual void drawRect(const SkRect& r, SkColor c) {
42 fCanvas->drawRect(r, p);
45 SkCanvas* peekCanvas() const { return fCanvas; }
48 class SkiaGraphicsPort : public GraphicsPort {
50 SkiaGraphicsPort(SkCanvas* canvas) : GraphicsPort(canvas) {}
52 void drawRect(const SkRect& r, SkColor c) override {
53 SkCanvas* canvas = (SkCanvas*)fCanvas->accessTopRasterHandle();
54 canvas->drawRect(r, SkPaint(SkColor4f::FromColor(c)));
58 class SkiaAllocator : public SkRasterHandleAllocator {
62 bool allocHandle(const SkImageInfo& info, Rec* rec) override {
63 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
67 SkCanvas* canvas = surface->getCanvas();
69 canvas->peekPixels(&pixmap);
71 rec->fReleaseProc = [](void* pixels, void* ctx){ SkSafeUnref((SkSurface*)ctx); };
72 rec->fReleaseCtx = surface.release();
73 rec->fPixels = pixmap.writable_addr();
74 rec->fRowBytes = pixmap.rowBytes();
75 rec->fHandle = canvas;
76 canvas->save(); // balanced each time updateHandle is called
80 void updateHandle(Handle hndl, const SkMatrix& ctm, const SkIRect& clip) override {
81 SkCanvas* canvas = (SkCanvas*)hndl;
84 canvas->clipRect(SkRect::Make(clip));
89 #ifdef SK_BUILD_FOR_MAC
91 #include "include/utils/mac/SkCGUtils.h"
92 class CGGraphicsPort : public GraphicsPort {
94 CGGraphicsPort(SkCanvas* canvas) : GraphicsPort(canvas) {}
96 void drawRect(const SkRect& r, SkColor c) override {
97 CGContextRef cg = (CGContextRef)fCanvas->accessTopRasterHandle();
99 CGColorRef color = CGColorCreateGenericRGB(SkColorGetR(c)/255.f,
100 SkColorGetG(c)/255.f,
101 SkColorGetB(c)/255.f,
102 SkColorGetA(c)/255.f);
104 CGContextSetFillColorWithColor(cg, color);
105 CGContextFillRect(cg, CGRectMake(r.x(), r.y(), r.width(), r.height()));
109 static CGAffineTransform matrix_to_transform(CGContextRef cg, const SkMatrix& ctm) {
111 matrix.setScale(1, -1);
112 matrix.postTranslate(0, SkIntToScalar(CGBitmapContextGetHeight(cg)));
113 matrix.preConcat(ctm);
115 return CGAffineTransformMake(matrix[SkMatrix::kMScaleX],
116 matrix[SkMatrix::kMSkewY],
117 matrix[SkMatrix::kMSkewX],
118 matrix[SkMatrix::kMScaleY],
119 matrix[SkMatrix::kMTransX],
120 matrix[SkMatrix::kMTransY]);
123 class CGAllocator : public SkRasterHandleAllocator {
127 bool allocHandle(const SkImageInfo& info, Rec* rec) override {
128 // let CG allocate the pixels
129 CGContextRef cg = SkCreateCGContext(SkPixmap(info, nullptr, 0));
133 rec->fReleaseProc = [](void* pixels, void* ctx){ CGContextRelease((CGContextRef)ctx); };
134 rec->fReleaseCtx = cg;
135 rec->fPixels = CGBitmapContextGetData(cg);
136 rec->fRowBytes = CGBitmapContextGetBytesPerRow(cg);
138 CGContextSaveGState(cg); // balanced each time updateHandle is called
142 void updateHandle(Handle hndl, const SkMatrix& ctm, const SkIRect& clip) override {
143 CGContextRef cg = (CGContextRef)hndl;
145 CGContextRestoreGState(cg);
146 CGContextSaveGState(cg);
147 CGContextClipToRect(cg, CGRectMake(clip.x(), clip.y(), clip.width(), clip.height()));
148 CGContextConcatCTM(cg, matrix_to_transform(cg, ctm));
152 using MyPort = CGGraphicsPort;
153 using MyAllocator = CGAllocator;
155 #elif defined(SK_BUILD_FOR_WIN)
157 #include "src/core/SkLeanWindows.h"
159 static RECT toRECT(const SkIRect& r) {
160 return { r.left(), r.top(), r.right(), r.bottom() };
163 class GDIGraphicsPort : public GraphicsPort {
165 GDIGraphicsPort(SkCanvas* canvas) : GraphicsPort(canvas) {}
167 void drawRect(const SkRect& r, SkColor c) override {
168 HDC hdc = (HDC)fCanvas->accessTopRasterHandle();
170 COLORREF cr = RGB(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));// SkEndian_Swap32(c) >> 8;
171 RECT rounded = toRECT(r.round());
172 FillRect(hdc, &rounded, CreateSolidBrush(cr));
174 // Assuming GDI wrote zeros for alpha, this will or-in 0xFF for alpha
176 paint.setBlendMode(SkBlendMode::kDstATop);
177 fCanvas->drawRect(r, paint);
181 // We use this static factory function instead of the regular constructor so
182 // that we can create the pixel data before calling the constructor. This is
183 // required so that we can call the base class' constructor with the pixel
185 static bool Create(int width, int height, bool is_opaque, SkRasterHandleAllocator::Rec* rec) {
186 BITMAPINFOHEADER hdr;
187 memset(&hdr, 0, sizeof(hdr));
188 hdr.biSize = sizeof(BITMAPINFOHEADER);
190 hdr.biHeight = -height; // Minus means top-down bitmap.
193 hdr.biCompression = BI_RGB; // No compression.
195 hdr.biXPelsPerMeter = 1;
196 hdr.biYPelsPerMeter = 1;
198 HBITMAP hbitmap = CreateDIBSection(nullptr, (const BITMAPINFO*)&hdr, 0, &pixels, 0, 0);
203 size_t row_bytes = width * sizeof(SkPMColor);
204 sk_bzero(pixels, row_bytes * height);
206 HDC hdc = CreateCompatibleDC(nullptr);
208 DeleteObject(hbitmap);
211 SetGraphicsMode(hdc, GM_ADVANCED);
212 HGDIOBJ origBitmap = SelectObject(hdc, hbitmap);
214 struct ReleaseContext {
218 rec->fReleaseProc = [](void*, void* context) {
219 ReleaseContext* ctx = static_cast<ReleaseContext*>(context);
220 HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(ctx->hdc, ctx->hbitmap));
221 DeleteObject(hbitmap);
225 rec->fReleaseCtx = new ReleaseContext{hdc, origBitmap};
226 rec->fPixels = pixels;
227 rec->fRowBytes = row_bytes;
233 * Subclass of SkRasterHandleAllocator that returns an HDC as its "handle".
235 class GDIAllocator : public SkRasterHandleAllocator {
239 bool allocHandle(const SkImageInfo& info, Rec* rec) override {
240 SkASSERT(info.colorType() == kN32_SkColorType);
241 return Create(info.width(), info.height(), info.isOpaque(), rec);
244 void updateHandle(Handle handle, const SkMatrix& ctm, const SkIRect& clip_bounds) override {
245 HDC hdc = static_cast<HDC>(handle);
248 xf.eM11 = ctm[SkMatrix::kMScaleX];
249 xf.eM21 = ctm[SkMatrix::kMSkewX];
250 xf.eDx = ctm[SkMatrix::kMTransX];
251 xf.eM12 = ctm[SkMatrix::kMSkewY];
252 xf.eM22 = ctm[SkMatrix::kMScaleY];
253 xf.eDy = ctm[SkMatrix::kMTransY];
254 SetWorldTransform(hdc, &xf);
256 RECT clip_bounds_RECT = toRECT(clip_bounds);
257 HRGN hrgn = CreateRectRgnIndirect(&clip_bounds_RECT);
258 SK_MAYBE_UNUSED int result = SelectClipRgn(hdc, hrgn);
259 SkASSERT(result != ERROR);
260 result = DeleteObject(hrgn);
261 SkASSERT(result != 0);
265 using MyPort = GDIGraphicsPort;
266 using MyAllocator = GDIAllocator;
270 using MyPort = SkiaGraphicsPort;
271 using MyAllocator = SkiaAllocator;
275 DEF_SIMPLE_GM(rasterallocator, canvas, 600, 300) {
276 auto doDraw = [](GraphicsPort* port) {
277 SkAutoCanvasRestore acr(port->peekCanvas(), true);
279 port->drawRect({0, 0, 256, 256}, SK_ColorRED);
281 port->translate(30, 30);
282 port->drawRect({0, 0, 30, 30}, SK_ColorBLUE);
283 port->drawOval({10, 10, 20, 20}, SK_ColorWHITE);
286 port->saveLayer({50, 50, 100, 100}, 0x80);
287 port->drawRect({55, 55, 95, 95}, SK_ColorGREEN);
290 port->clip({150, 50, 200, 200});
291 port->drawRect({0, 0, 256, 256}, 0xFFCCCCCC);
294 // TODO: this common code fails pic-8888 and serialize-8888
295 //GraphicsPort skiaPort(canvas);
298 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
299 std::unique_ptr<SkCanvas> nativeCanvas =
300 SkRasterHandleAllocator::MakeCanvas(std::make_unique<MyAllocator>(), info);
301 MyPort nativePort(nativeCanvas.get());
305 nativeCanvas->peekPixels(&pm);
306 canvas->drawImage(SkImage::MakeRasterCopy(pm), 280, 0);