/* * Copyright 2005-2007, Stephan Aßmus . All rights reserved. * Distributed under the terms of the MIT License. * * API to the Anti-Grain Geometry based "Painter" drawing backend. Manages * rendering pipe-lines for stroke, fills, bitmap and text rendering. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "drawing_support.h" #include "DrawState.h" #include "DrawingMode.h" #include "PatternHandler.h" #include "RenderingBuffer.h" #include "ServerBitmap.h" #include "ServerFont.h" #include "SystemPalette.h" #include "Painter.h" #define CHECK_CLIPPING if (!fValidClipping) return BRect(0, 0, -1, -1); #define CHECK_CLIPPING_NO_RETURN if (!fValidClipping) return; // constructor Painter::Painter() : fBuffer(), fPixelFormat(fBuffer, &fPatternHandler), fBaseRenderer(fPixelFormat), fUnpackedScanline(), fPackedScanline(), fRasterizer(), fRenderer(fBaseRenderer), fRendererBin(fBaseRenderer), fPath(), fCurve(fPath), fSubpixelPrecise(false), fPenSize(1.0), fClippingRegion(NULL), fValidClipping(false), fDrawingMode(B_OP_COPY), fDrawingText(false), fAlphaSrcMode(B_PIXEL_ALPHA), fAlphaFncMode(B_ALPHA_OVERLAY), fLineCapMode(B_BUTT_CAP), fLineJoinMode(B_MITER_JOIN), fMiterLimit(B_DEFAULT_MITER_LIMIT), fPatternHandler(), fTextRenderer(fRenderer, fRendererBin, fUnpackedScanline) { fPixelFormat.SetDrawingMode(fDrawingMode, fAlphaSrcMode, fAlphaFncMode, false); #if ALIASED_DRAWING fRasterizer.gamma(agg::gamma_threshold(0.5)); #endif } // destructor Painter::~Painter() { } // #pragma mark - // AttachToBuffer void Painter::AttachToBuffer(RenderingBuffer* buffer) { if (buffer && buffer->InitCheck() >= B_OK && // TODO: implement drawing on B_RGB24, B_RGB15, B_RGB16, B_CMAP8 and B_GRAY8 :-[ (buffer->ColorSpace() == B_RGBA32 || buffer->ColorSpace() == B_RGB32)) { fBuffer.attach((uint8*)buffer->Bits(), buffer->Width(), buffer->Height(), buffer->BytesPerRow()); // These are the AGG renderes and rasterizes which // will be used for stroking paths _SetRendererColor(fPatternHandler.HighColor()); } } // DetachFromBuffer void Painter::DetachFromBuffer() { } // #pragma mark - // SetDrawState void Painter::SetDrawState(const DrawState* data, int32 xOffset, int32 yOffset) { // NOTE: The custom clipping in "data" is ignored, because it has already // been taken into account elsewhere // TODO: optimize "context switch" for speed... // but for now... SetPenSize(data->PenSize()); SetFont(data); fSubpixelPrecise = data->SubPixelPrecise(); // any of these conditions means we need to use a different drawing // mode instance bool updateDrawingMode = !(data->GetPattern() == fPatternHandler.GetPattern()) || data->GetDrawingMode() != fDrawingMode || (data->GetDrawingMode() == B_OP_ALPHA && (data->AlphaSrcMode() != fAlphaSrcMode || data->AlphaFncMode() != fAlphaFncMode)); fDrawingMode = data->GetDrawingMode(); fAlphaSrcMode = data->AlphaSrcMode(); fAlphaFncMode = data->AlphaFncMode(); fPatternHandler.SetPattern(data->GetPattern()); fPatternHandler.SetOffsets(xOffset, yOffset); fLineCapMode = data->LineCapMode(); fLineJoinMode = data->LineJoinMode(); fMiterLimit = data->MiterLimit(); // adopt the color *after* the pattern is set // to set the renderers to the correct color SetHighColor(data->HighColor()); SetLowColor(data->LowColor()); if (updateDrawingMode || fPixelFormat.UsesOpCopyForText()) _UpdateDrawingMode(); } // #pragma mark - state // ConstrainClipping void Painter::ConstrainClipping(const BRegion* region) { fClippingRegion = region; fBaseRenderer.set_clipping_region(const_cast(region)); fValidClipping = region->Frame().IsValid(); if (fValidClipping) { clipping_rect cb = fClippingRegion->FrameInt(); fRasterizer.clip_box(cb.left, cb.top, cb.right + 1, cb.bottom + 1); } } // SetHighColor void Painter::SetHighColor(const rgb_color& color) { if (fPatternHandler.HighColor() == color) return; fPatternHandler.SetHighColor(color); if (*(fPatternHandler.GetR5Pattern()) == B_SOLID_HIGH) _SetRendererColor(color); } // SetLowColor void Painter::SetLowColor(const rgb_color& color) { fPatternHandler.SetLowColor(color); if (*(fPatternHandler.GetR5Pattern()) == B_SOLID_LOW) _SetRendererColor(color); } // SetDrawingMode void Painter::SetDrawingMode(drawing_mode mode) { if (fDrawingMode != mode) { fDrawingMode = mode; _UpdateDrawingMode(); } } // SetBlendingMode void Painter::SetBlendingMode(source_alpha srcAlpha, alpha_function alphaFunc) { if (fAlphaSrcMode != srcAlpha || fAlphaFncMode != alphaFunc) { fAlphaSrcMode = srcAlpha; fAlphaFncMode = alphaFunc; if (fDrawingMode == B_OP_ALPHA) _UpdateDrawingMode(); } } // SetPenSize void Painter::SetPenSize(float size) { fPenSize = size; } // SetStrokeMode void Painter::SetStrokeMode(cap_mode lineCap, join_mode joinMode, float miterLimit) { fLineCapMode = lineCap; fLineJoinMode = joinMode; fMiterLimit = miterLimit; } // SetPattern void Painter::SetPattern(const pattern& p, bool drawingText) { if (!(p == *fPatternHandler.GetR5Pattern()) || drawingText != fDrawingText) { fPatternHandler.SetPattern(p); fDrawingText = drawingText; _UpdateDrawingMode(fDrawingText); // update renderer color if necessary if (fPatternHandler.IsSolidHigh()) { // pattern was not solid high before _SetRendererColor(fPatternHandler.HighColor()); } else if (fPatternHandler.IsSolidLow()) { // pattern was not solid low before _SetRendererColor(fPatternHandler.LowColor()); } } } // SetFont void Painter::SetFont(const ServerFont& font) { fTextRenderer.SetFont(font); fTextRenderer.SetAntialiasing( !(font.Flags() & B_DISABLE_ANTIALIASING)); } // SetFont void Painter::SetFont(const DrawState* state) { fTextRenderer.SetFont(state->Font()); fTextRenderer.SetAntialiasing(!(state->ForceFontAliasing() || state->Font().Flags() & B_DISABLE_ANTIALIASING)); } // #pragma mark - drawing // StrokeLine void Painter::StrokeLine(BPoint a, BPoint b) { CHECK_CLIPPING_NO_RETURN // "false" means not to do the pixel center offset, // because it would mess up our optimized versions _Transform(&a, false); _Transform(&b, false); // first, try an optimized version if (fPenSize == 1.0 && (fDrawingMode == B_OP_COPY || fDrawingMode == B_OP_OVER)) { pattern pat = *fPatternHandler.GetR5Pattern(); if (pat == B_SOLID_HIGH && StraightLine(a, b, fPatternHandler.HighColor())) { return; } else if (pat == B_SOLID_LOW && StraightLine(a, b, fPatternHandler.LowColor())) { return; } } fPath.remove_all(); if (a == b) { // special case dots if (fPenSize == 1.0 && !fSubpixelPrecise) { if (fClippingRegion->Contains(a)) { agg::rgba8 dummyColor; fPixelFormat.blend_pixel(a.x, a.y, dummyColor, 255); } } else { fPath.move_to(a.x, a.y); fPath.line_to(a.x + 1, a.y); fPath.line_to(a.x + 1, a.y + 1); fPath.line_to(a.x, a.y + 1); _FillPath(fPath); } } else { // do the pixel center offset here // tweak ends to "include" the pixel at the index, // we need to do this in order to produce results like R5, // where coordinates were inclusive if (!fSubpixelPrecise) { bool centerOnLine = fmodf(fPenSize, 2.0) != 0.0; if (a.x == b.x) { // shift to pixel center vertically if (centerOnLine) { a.x += 0.5; b.x += 0.5; } // extend on bottom end if (a.y < b.y) b.y++; else a.y++; } else if (a.y == b.y) { if (centerOnLine) { // shift to pixel center horizontally a.y += 0.5; b.y += 0.5; } // extend on right end if (a.x < b.x) b.x++; else a.x++; } else { // do this regardless of pensize if (a.x < b.x) b.x++; else a.x++; if (a.y < b.y) b.y++; else a.y++; } } fPath.move_to(a.x, a.y); fPath.line_to(b.x, b.y); _StrokePath(fPath); } } // StraightLine bool Painter::StraightLine(BPoint a, BPoint b, const rgb_color& c) const { if (!fValidClipping) return false; if (a.x == b.x) { // vertical uint8* dst = fBuffer.row_ptr(0); uint32 bpr = fBuffer.stride(); int32 x = (int32)a.x; dst += x * 4; int32 y1 = (int32)min_c(a.y, b.y); int32 y2 = (int32)max_c(a.y, b.y); pixel32 color; color.data8[0] = c.blue; color.data8[1] = c.green; color.data8[2] = c.red; color.data8[3] = 255; // draw a line, iterate over clipping boxes fBaseRenderer.first_clip_box(); do { if (fBaseRenderer.xmin() <= x && fBaseRenderer.xmax() >= x) { int32 i = max_c(fBaseRenderer.ymin(), y1); int32 end = min_c(fBaseRenderer.ymax(), y2); uint8* handle = dst + i * bpr; for (; i <= end; i++) { *(uint32*)handle = color.data32; handle += bpr; } } } while (fBaseRenderer.next_clip_box()); return true; } else if (a.y == b.y) { // horizontal int32 y = (int32)a.y; uint8* dst = fBuffer.row_ptr(y); int32 x1 = (int32)min_c(a.x, b.x); int32 x2 = (int32)max_c(a.x, b.x); pixel32 color; color.data8[0] = c.blue; color.data8[1] = c.green; color.data8[2] = c.red; color.data8[3] = 255; // draw a line, iterate over clipping boxes fBaseRenderer.first_clip_box(); do { if (fBaseRenderer.ymin() <= y && fBaseRenderer.ymax() >= y) { int32 i = max_c(fBaseRenderer.xmin(), x1); int32 end = min_c(fBaseRenderer.xmax(), x2); uint32* handle = (uint32*)(dst + i * 4); for (; i <= end; i++) { *handle++ = color.data32; } } } while (fBaseRenderer.next_clip_box()); return true; } return false; } // #pragma mark - // StrokeTriangle BRect Painter::StrokeTriangle(BPoint pt1, BPoint pt2, BPoint pt3) const { return _DrawTriangle(pt1, pt2, pt3, false); } // FillTriangle BRect Painter::FillTriangle(BPoint pt1, BPoint pt2, BPoint pt3) const { return _DrawTriangle(pt1, pt2, pt3, true); } // DrawPolygon BRect Painter::DrawPolygon(BPoint* p, int32 numPts, bool filled, bool closed) const { CHECK_CLIPPING if (numPts > 0) { fPath.remove_all(); _Transform(p); fPath.move_to(p->x, p->y); for (int32 i = 1; i < numPts; i++) { p++; _Transform(p); fPath.line_to(p->x, p->y); } if (closed) fPath.close_polygon(); if (filled) return _FillPath(fPath); else return _StrokePath(fPath); } return BRect(0.0, 0.0, -1.0, -1.0); } // DrawBezier BRect Painter::DrawBezier(BPoint* p, bool filled) const { CHECK_CLIPPING fPath.remove_all(); _Transform(&(p[0])); _Transform(&(p[1])); _Transform(&(p[2])); _Transform(&(p[3])); fPath.move_to(p[0].x, p[0].y); fPath.curve4(p[1].x, p[1].y, p[2].x, p[2].y, p[3].x, p[3].y); if (filled) { fPath.close_polygon(); return _FillPath(fCurve); } else { return _StrokePath(fCurve); } } // DrawShape BRect Painter::DrawShape(const int32& opCount, const uint32* opList, const int32& ptCount, const BPoint* points, bool filled) const { CHECK_CLIPPING // TODO: if shapes are ever used more heavily in Haiku, // it would be nice to use BShape data directly (write // an AGG "VertexSource" adaptor) fPath.remove_all(); for (int32 i = 0; i < opCount; i++) { uint32 op = opList[i] & 0xFF000000; if (op & OP_MOVETO) { fPath.move_to(points->x, points->y); points++; } if (op & OP_LINETO) { int32 count = opList[i] & 0x00FFFFFF; while (count--) { fPath.line_to(points->x, points->y); points++; } } if (op & OP_BEZIERTO) { int32 count = opList[i] & 0x00FFFFFF; while (count) { fPath.curve4(points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y); points += 3; count -= 3; } } if (op & OP_CLOSE) fPath.close_polygon(); } if (filled) return _FillPath(fCurve); else return _StrokePath(fCurve); } // StrokeRect BRect Painter::StrokeRect(const BRect& r) const { CHECK_CLIPPING // support invalid rects BPoint a(min_c(r.left, r.right), min_c(r.top, r.bottom)); BPoint b(max_c(r.left, r.right), max_c(r.top, r.bottom)); _Transform(&a, false); _Transform(&b, false); // first, try an optimized version if (fPenSize == 1.0 && (fDrawingMode == B_OP_COPY || fDrawingMode == B_OP_OVER)) { pattern p = *fPatternHandler.GetR5Pattern(); if (p == B_SOLID_HIGH) { BRect rect(a, b); StrokeRect(rect, fPatternHandler.HighColor()); return _Clipped(rect); } else if (p == B_SOLID_LOW) { BRect rect(a, b); StrokeRect(rect, fPatternHandler.LowColor()); return _Clipped(rect); } } if (fmodf(fPenSize, 2.0) != 0.0) { // shift coords to center of pixels a.x += 0.5; a.y += 0.5; b.x += 0.5; b.y += 0.5; } fPath.remove_all(); fPath.move_to(a.x, a.y); if (a.x == b.x || a.y == b.y) { // special case rects with one pixel height or width fPath.line_to(b.x, b.y); } else { fPath.line_to(b.x, a.y); fPath.line_to(b.x, b.y); fPath.line_to(a.x, b.y); } fPath.close_polygon(); return _StrokePath(fPath); } // StrokeRect void Painter::StrokeRect(const BRect& r, const rgb_color& c) const { StraightLine(BPoint(r.left, r.top), BPoint(r.right - 1, r.top), c); StraightLine(BPoint(r.right, r.top), BPoint(r.right, r.bottom - 1), c); StraightLine(BPoint(r.right, r.bottom), BPoint(r.left + 1, r.bottom), c); StraightLine(BPoint(r.left, r.bottom), BPoint(r.left, r.top + 1), c); } // FillRect BRect Painter::FillRect(const BRect& r) const { CHECK_CLIPPING // support invalid rects BPoint a(min_c(r.left, r.right), min_c(r.top, r.bottom)); BPoint b(max_c(r.left, r.right), max_c(r.top, r.bottom)); _Transform(&a, false); _Transform(&b, false); // first, try an optimized version if (fDrawingMode == B_OP_COPY || fDrawingMode == B_OP_OVER) { pattern p = *fPatternHandler.GetR5Pattern(); if (p == B_SOLID_HIGH) { BRect rect(a, b); FillRect(rect, fPatternHandler.HighColor()); return _Clipped(rect); } else if (p == B_SOLID_LOW) { BRect rect(a, b); FillRect(rect, fPatternHandler.LowColor()); return _Clipped(rect); } } if (fDrawingMode == B_OP_ALPHA && fAlphaFncMode == B_ALPHA_OVERLAY) { pattern p = *fPatternHandler.GetR5Pattern(); if (p == B_SOLID_HIGH) { BRect rect(a, b); _BlendRect32(rect, fPatternHandler.HighColor()); return _Clipped(rect); } else if (p == B_SOLID_LOW) { rgb_color c = fPatternHandler.LowColor(); if (fAlphaSrcMode == B_CONSTANT_ALPHA) c.alpha = fPatternHandler.HighColor().alpha; BRect rect(a, b); _BlendRect32(rect, c); return _Clipped(rect); } } // account for stricter interpretation of coordinates in AGG // the rectangle ranges from the top-left (.0, .0) // to the bottom-right (.9999, .9999) corner of pixels b.x += 1.0; b.y += 1.0; fPath.remove_all(); fPath.move_to(a.x, a.y); fPath.line_to(b.x, a.y); fPath.line_to(b.x, b.y); fPath.line_to(a.x, b.y); fPath.close_polygon(); return _FillPath(fPath); } // FillRect void Painter::FillRect(const BRect& r, const rgb_color& c) const { if (!fValidClipping) return; uint8* dst = fBuffer.row_ptr(0); uint32 bpr = fBuffer.stride(); int32 left = (int32)r.left; int32 top = (int32)r.top; int32 right = (int32)r.right; int32 bottom = (int32)r.bottom; // get a 32 bit pixel ready with the color pixel32 color; color.data8[0] = c.blue; color.data8[1] = c.green; color.data8[2] = c.red; color.data8[3] = c.alpha; // fill rects, iterate over clipping boxes fBaseRenderer.first_clip_box(); do { int32 x1 = max_c(fBaseRenderer.xmin(), left); int32 x2 = min_c(fBaseRenderer.xmax(), right); if (x1 <= x2) { int32 y1 = max_c(fBaseRenderer.ymin(), top); int32 y2 = min_c(fBaseRenderer.ymax(), bottom); uint8* offset = dst + x1 * 4; for (; y1 <= y2; y1++) { // uint32* handle = (uint32*)(offset + y1 * bpr); // for (int32 x = x1; x <= x2; x++) { // *handle++ = color.data32; // } gfxset32(offset + y1 * bpr, color.data32, (x2 - x1 + 1) * 4); } } } while (fBaseRenderer.next_clip_box()); } // FillRectNoClipping void Painter::FillRectNoClipping(const BRect& r, const rgb_color& c) const { int32 left = (int32)r.left; int32 y = (int32)r.top; int32 right = (int32)r.right; int32 bottom = (int32)r.bottom; uint8* dst = fBuffer.row_ptr(y); uint32 bpr = fBuffer.stride(); // get a 32 bit pixel ready with the color pixel32 color; color.data8[0] = c.blue; color.data8[1] = c.green; color.data8[2] = c.red; color.data8[3] = c.alpha; dst += left * 4; for (; y <= bottom; y++) { // uint32* handle = (uint32*)dst; // for (int32 x = left; x <= right; x++) { // *handle++ = color.data32; // } gfxset32(dst, color.data32, (right - left + 1) * 4); dst += bpr; } } // StrokeRoundRect BRect Painter::StrokeRoundRect(const BRect& r, float xRadius, float yRadius) const { CHECK_CLIPPING BPoint lt(r.left, r.top); BPoint rb(r.right, r.bottom); bool centerOffset = fPenSize == 1.0; // TODO: use this when using _StrokePath() // bool centerOffset = fmodf(fPenSize, 2.0) != 0.0; _Transform(<, centerOffset); _Transform(&rb, centerOffset); if (fPenSize == 1.0) { agg::rounded_rect rect; rect.rect(lt.x, lt.y, rb.x, rb.y); rect.radius(xRadius, yRadius); return _StrokePath(rect); } else { // NOTE: This implementation might seem a little strange, but it makes // stroked round rects look like on R5. A more correct way would be to use // _StrokePath() as above (independent from fPenSize). // The fact that the bounding box of the round rect is not enlarged // by fPenSize/2 is actually on purpose, though one could argue it is unexpected. // enclose the right and bottom edge rb.x++; rb.y++; agg::rounded_rect outer; outer.rect(lt.x, lt.y, rb.x, rb.y); outer.radius(xRadius, yRadius); fRasterizer.reset(); fRasterizer.add_path(outer); // don't add an inner hole if the "size is negative", this avoids some // defects that can be observed on R5 and could be regarded as a bug. if (2 * fPenSize < rb.x - lt.x && 2 * fPenSize < rb.y - lt.y) { agg::rounded_rect inner; inner.rect(lt.x + fPenSize, lt.y + fPenSize, rb.x - fPenSize, rb.y - fPenSize); inner.radius(max_c(0.0, xRadius - fPenSize), max_c(0.0, yRadius - fPenSize)); fRasterizer.add_path(inner); } // make the inner rect work as a hole fRasterizer.filling_rule(agg::fill_even_odd); if (fPenSize > 2) agg::render_scanlines(fRasterizer, fPackedScanline, fRenderer); else agg::render_scanlines(fRasterizer, fUnpackedScanline, fRenderer); // reset to default fRasterizer.filling_rule(agg::fill_non_zero); return _Clipped(_BoundingBox(outer)); } } // FillRoundRect BRect Painter::FillRoundRect(const BRect& r, float xRadius, float yRadius) const { CHECK_CLIPPING BPoint lt(r.left, r.top); BPoint rb(r.right, r.bottom); _Transform(<, false); _Transform(&rb, false); // account for stricter interpretation of coordinates in AGG // the rectangle ranges from the top-left (.0, .0) // to the bottom-right (.9999, .9999) corner of pixels rb.x += 1.0; rb.y += 1.0; agg::rounded_rect rect; rect.rect(lt.x, lt.y, rb.x, rb.y); rect.radius(xRadius, yRadius); return _FillPath(rect); } // AlignEllipseRect void Painter::AlignEllipseRect(BRect* rect, bool filled) const { if (!fSubpixelPrecise) { // align rect to pixels align_rect_to_pixels(rect); // account for "pixel index" versus "pixel area" rect->right++; rect->bottom++; if (!filled && fmodf(fPenSize, 2.0) != 0.0) { // align the stroke rect->InsetBy(0.5, 0.5); } } } // DrawEllipse BRect Painter::DrawEllipse(BRect r, bool fill) const { CHECK_CLIPPING AlignEllipseRect(&r, fill); float xRadius = r.Width() / 2.0; float yRadius = r.Height() / 2.0; BPoint center(r.left + xRadius, r.top + yRadius); int32 divisions = (int32)((xRadius + yRadius + 2 * fPenSize) * PI / 2); if (divisions < 12) divisions = 12; if (divisions > 4096) divisions = 4096; if (fill) { agg::ellipse path(center.x, center.y, xRadius, yRadius, divisions); return _FillPath(path); } else { // NOTE: This implementation might seem a little strange, but it makes // stroked ellipses look like on R5. A more correct way would be to use // _StrokePath(), but it currently has its own set of problems with narrow // ellipses (for small xRadii or yRadii). float inset = fPenSize / 2.0; agg::ellipse inner(center.x, center.y, max_c(0.0, xRadius - inset), max_c(0.0, yRadius - inset), divisions); agg::ellipse outer(center.x, center.y, xRadius + inset, yRadius + inset, divisions); fRasterizer.reset(); fRasterizer.add_path(outer); fRasterizer.add_path(inner); // make the inner ellipse work as a hole fRasterizer.filling_rule(agg::fill_even_odd); if (fPenSize > 4) agg::render_scanlines(fRasterizer, fPackedScanline, fRenderer); else agg::render_scanlines(fRasterizer, fUnpackedScanline, fRenderer); // reset to default fRasterizer.filling_rule(agg::fill_non_zero); return _Clipped(_BoundingBox(outer)); } } // StrokeArc BRect Painter::StrokeArc(BPoint center, float xRadius, float yRadius, float angle, float span) const { CHECK_CLIPPING _Transform(¢er); double angleRad = (angle * PI) / 180.0; double spanRad = (span * PI) / 180.0; agg::bezier_arc arc(center.x, center.y, xRadius, yRadius, -angleRad, -spanRad); agg::conv_curve path(arc); path.approximation_scale(2.0); return _StrokePath(path); } // FillArc BRect Painter::FillArc(BPoint center, float xRadius, float yRadius, float angle, float span) const { CHECK_CLIPPING _Transform(¢er); double angleRad = (angle * PI) / 180.0; double spanRad = (span * PI) / 180.0; agg::bezier_arc arc(center.x, center.y, xRadius, yRadius, -angleRad, -spanRad); agg::conv_curve segmentedArc(arc); fPath.remove_all(); // build a new path by starting at the center point, // then traversing the arc, then going back to the center fPath.move_to(center.x, center.y); segmentedArc.rewind(0); double x; double y; unsigned cmd = segmentedArc.vertex(&x, &y); while (!agg::is_stop(cmd)) { fPath.line_to(x, y); cmd = segmentedArc.vertex(&x, &y); } fPath.close_polygon(); return _FillPath(fPath); } // #pragma mark - // DrawString BRect Painter::DrawString(const char* utf8String, uint32 length, BPoint baseLine, const escapement_delta* delta, FontCacheReference* cacheReference) { CHECK_CLIPPING if (!fSubpixelPrecise) { baseLine.x = roundf(baseLine.x); baseLine.y = roundf(baseLine.y); } BRect bounds(0.0, 0.0, -1.0, -1.0); // text is not rendered with patterns, but we need to // make sure that the previous pattern is restored pattern oldPattern = *fPatternHandler.GetR5Pattern(); SetPattern(B_SOLID_HIGH, true); bounds = fTextRenderer.RenderString(utf8String, length, baseLine, fClippingRegion->Frame(), false, NULL, delta, cacheReference); SetPattern(oldPattern); return _Clipped(bounds); } // BoundingBox BRect Painter::BoundingBox(const char* utf8String, uint32 length, BPoint baseLine, BPoint* penLocation, const escapement_delta* delta, FontCacheReference* cacheReference) const { if (!fSubpixelPrecise) { baseLine.x = roundf(baseLine.x); baseLine.y = roundf(baseLine.y); } static BRect dummy; return fTextRenderer.RenderString(utf8String, length, baseLine, dummy, true, penLocation, delta, cacheReference); } // StringWidth float Painter::StringWidth(const char* utf8String, uint32 length, const escapement_delta* delta) { return Font().StringWidth(utf8String, length, delta); } // #pragma mark - // DrawBitmap BRect Painter::DrawBitmap(const ServerBitmap* bitmap, BRect bitmapRect, BRect viewRect) const { CHECK_CLIPPING BRect touched = _Clipped(viewRect); if (bitmap && bitmap->IsValid() && touched.IsValid()) { // the native bitmap coordinate system BRect actualBitmapRect(bitmap->Bounds()); agg::rendering_buffer srcBuffer; srcBuffer.attach(bitmap->Bits(), bitmap->Width(), bitmap->Height(), bitmap->BytesPerRow()); _DrawBitmap(srcBuffer, bitmap->ColorSpace(), actualBitmapRect, bitmapRect, viewRect); } return touched; } // #pragma mark - // FillRegion BRect Painter::FillRegion(const BRegion* region) const { CHECK_CLIPPING BRegion copy(*region); int32 count = copy.CountRects(); BRect touched = FillRect(copy.RectAt(0)); for (int32 i = 1; i < count; i++) { touched = touched | FillRect(copy.RectAt(i)); } return touched; } // InvertRect BRect Painter::InvertRect(const BRect& r) const { CHECK_CLIPPING BRegion region(r); if (fClippingRegion) { region.IntersectWith(fClippingRegion); } // implementation only for B_RGB32 at the moment int32 count = region.CountRects(); for (int32 i = 0; i < count; i++) { _InvertRect32(region.RectAt(i)); } return _Clipped(r); } // #pragma mark - private // _Transform inline void Painter::_Transform(BPoint* point, bool centerOffset) const { // rounding if (!fSubpixelPrecise) { // TODO: validate usage of floor() for values < 0 point->x = (int32)point->x; point->y = (int32)point->y; } // this code is supposed to move coordinates to the center of pixels, // as AGG considers (0,0) to be the "upper left corner" of a pixel, // but BViews are less strict on those details if (centerOffset) { point->x += 0.5; point->y += 0.5; } } // _Transform inline BPoint Painter::_Transform(const BPoint& point, bool centerOffset) const { BPoint ret = point; _Transform(&ret, centerOffset); return ret; } // _Clipped BRect Painter::_Clipped(const BRect& rect) const { if (rect.IsValid()) { return BRect(rect & fClippingRegion->Frame()); } return BRect(rect); } // _UpdateDrawingMode void Painter::_UpdateDrawingMode(bool drawingText) { // The AGG renderers have their own color setting, however // almost all drawing mode classes ignore the color given // by the AGG renderer and use the colors from the PatternHandler // instead. If we have a B_SOLID_* pattern, we can actually use // the color in the renderer and special versions of drawing modes // that don't use PatternHandler and are more efficient. This // has been implemented for B_OP_COPY and a couple others (the // DrawingMode*Solid ones) as of now. The PixelFormat knows the // PatternHandler and makes its decision based on the pattern. // The last parameter to SetDrawingMode() is a flag if a special // for when Painter is used to draw text. In this case, another // special version of B_OP_COPY is used that acts like R5 in that // anti-aliased pixel are not rendered against the actual background // but the current low color instead. This way, the frame buffer // doesn't need to be read. // When a solid pattern is used, _SetRendererColor() // has to be called so that all internal colors in the renderes // are up to date for use by the solid drawing mode version. fPixelFormat.SetDrawingMode(fDrawingMode, fAlphaSrcMode, fAlphaFncMode, drawingText); if (drawingText) fPatternHandler.MakeOpCopyColorCache(); } // _SetRendererColor void Painter::_SetRendererColor(const rgb_color& color) const { fRenderer.color(agg::rgba(color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0)); // TODO: bitmap fonts not yet correctly setup in AGGTextRenderer // fRendererBin.color(agg::rgba(color.red / 255.0, // color.green / 255.0, // color.blue / 255.0, // color.alpha / 255.0)); } // #pragma mark - // _DrawTriangle inline BRect Painter::_DrawTriangle(BPoint pt1, BPoint pt2, BPoint pt3, bool fill) const { CHECK_CLIPPING _Transform(&pt1); _Transform(&pt2); _Transform(&pt3); fPath.remove_all(); fPath.move_to(pt1.x, pt1.y); fPath.line_to(pt2.x, pt2.y); fPath.line_to(pt3.x, pt3.y); fPath.close_polygon(); if (fill) return _FillPath(fPath); else return _StrokePath(fPath); } // copy_bitmap_row_cmap8_copy static inline void copy_bitmap_row_cmap8_copy(uint8* dst, const uint8* src, int32 numPixels, const rgb_color* colorMap) { uint32* d = (uint32*)dst; const uint8* s = src; while (numPixels--) { const rgb_color c = colorMap[*s++]; *d++ = (c.alpha << 24) | (c.red << 16) | (c.green << 8) | (c.blue); } } // copy_bitmap_row_cmap8_over static inline void copy_bitmap_row_cmap8_over(uint8* dst, const uint8* src, int32 numPixels, const rgb_color* colorMap) { uint32* d = (uint32*)dst; const uint8* s = src; while (numPixels--) { const rgb_color c = colorMap[*s++]; if (c.alpha) *d = (c.alpha << 24) | (c.red << 16) | (c.green << 8) | (c.blue); d++; } } // copy_bitmap_row_bgr32_copy static inline void copy_bitmap_row_bgr32_copy(uint8* dst, const uint8* src, int32 numPixels, const rgb_color* colorMap) { memcpy(dst, src, numPixels * 4); } // copy_bitmap_row_bgr32_alpha static inline void copy_bitmap_row_bgr32_alpha(uint8* dst, const uint8* src, int32 numPixels, const rgb_color* colorMap) { uint32* d = (uint32*)dst; int32 bytes = numPixels * 4; uint8 buffer[bytes]; uint8* b = buffer; while (numPixels--) { if (src[3] == 255) { *(uint32*)b = *(uint32*)src; } else { *(uint32*)b = *d; b[0] = ((src[0] - b[0]) * src[3] + (b[0] << 8)) >> 8; b[1] = ((src[1] - b[1]) * src[3] + (b[1] << 8)) >> 8; b[2] = ((src[2] - b[2]) * src[3] + (b[2] << 8)) >> 8; } d ++; b += 4; src += 4; } memcpy(dst, buffer, bytes); } // _DrawBitmap void Painter::_DrawBitmap(agg::rendering_buffer& srcBuffer, color_space format, BRect actualBitmapRect, BRect bitmapRect, BRect viewRect) const { if (!fValidClipping || !bitmapRect.IsValid() || !bitmapRect.Intersects(actualBitmapRect) || !viewRect.IsValid()) { return; } if (!fSubpixelPrecise) { align_rect_to_pixels(&viewRect); align_rect_to_pixels(&bitmapRect); } double xScale = (viewRect.Width() + 1) / (bitmapRect.Width() + 1); double yScale = (viewRect.Height() + 1) / (bitmapRect.Height() + 1); if (xScale == 0.0 || yScale == 0.0) return; // compensate for the lefttop offset the actualBitmapRect might have // actualBitmapRect has the right size, but put it at B_ORIGIN // bitmapRect is already in good coordinates actualBitmapRect.OffsetBy(-actualBitmapRect.left, -actualBitmapRect.top); // constrain rect to passed bitmap bounds // and transfer the changes to the viewRect with the right scale if (bitmapRect.left < actualBitmapRect.left) { float diff = actualBitmapRect.left - bitmapRect.left; viewRect.left += diff * xScale; bitmapRect.left = actualBitmapRect.left; } if (bitmapRect.top < actualBitmapRect.top) { float diff = actualBitmapRect.top - bitmapRect.top; viewRect.top += diff * yScale; bitmapRect.top = actualBitmapRect.top; } if (bitmapRect.right > actualBitmapRect.right) { float diff = bitmapRect.right - actualBitmapRect.right; viewRect.right -= diff * xScale; bitmapRect.right = actualBitmapRect.right; } if (bitmapRect.bottom > actualBitmapRect.bottom) { float diff = bitmapRect.bottom - actualBitmapRect.bottom; viewRect.bottom -= diff * yScale; bitmapRect.bottom = actualBitmapRect.bottom; } double xOffset = viewRect.left - bitmapRect.left; double yOffset = viewRect.top - bitmapRect.top; switch (format) { case B_RGB32: case B_RGBA32: { // maybe we can use an optimized version if (xScale == 1.0 && yScale == 1.0) { if (fDrawingMode == B_OP_COPY) { _DrawBitmapNoScale32(copy_bitmap_row_bgr32_copy, 4, srcBuffer, xOffset, yOffset, viewRect); return; } else if (fDrawingMode == B_OP_ALPHA && fAlphaSrcMode == B_PIXEL_ALPHA && fAlphaFncMode == B_ALPHA_OVERLAY) { _DrawBitmapNoScale32(copy_bitmap_row_bgr32_alpha, 4, srcBuffer, xOffset, yOffset, viewRect); return; } } _DrawBitmapGeneric32(srcBuffer, xOffset, yOffset, xScale, yScale, viewRect); break; } default: { if (format == B_CMAP8 && xScale == 1.0 && yScale == 1.0) { if (fDrawingMode == B_OP_COPY) { _DrawBitmapNoScale32(copy_bitmap_row_cmap8_copy, 1, srcBuffer, xOffset, yOffset, viewRect); return; } else if (fDrawingMode == B_OP_OVER) { _DrawBitmapNoScale32(copy_bitmap_row_cmap8_over, 1, srcBuffer, xOffset, yOffset, viewRect); return; } } // TODO: this is only a temporary implementation, // to really handle other colorspaces, one would // rather do the conversion with much less overhead, // for example in the nn filter (hm), or in the // scanline generator (better) // maybe we can use an optimized version BBitmap temp(actualBitmapRect, B_BITMAP_NO_SERVER_LINK, B_RGBA32); status_t err = temp.ImportBits(srcBuffer.buf(), srcBuffer.height() * srcBuffer.stride(), srcBuffer.stride(), 0, format); if (err >= B_OK) { agg::rendering_buffer convertedBuffer; convertedBuffer.attach((uint8*)temp.Bits(), (uint32)actualBitmapRect.IntegerWidth() + 1, (uint32)actualBitmapRect.IntegerHeight() + 1, temp.BytesPerRow()); _DrawBitmapGeneric32(convertedBuffer, xOffset, yOffset, xScale, yScale, viewRect); } else { fprintf(stderr, "Painter::_DrawBitmap() - " "colorspace conversion failed: %s\n", strerror(err)); } break; } } } #define DEBUG_DRAW_BITMAP 0 // _DrawBitmapNoScale32 template void Painter::_DrawBitmapNoScale32(F copyRowFunction, uint32 bytesPerSourcePixel, agg::rendering_buffer& srcBuffer, int32 xOffset, int32 yOffset, BRect viewRect) const { // NOTE: this would crash if viewRect was large enough to read outside the // bitmap, so make sure this is not the case before calling this function! uint8* dst = fBuffer.row_ptr(0); uint32 dstBPR = fBuffer.stride(); const uint8* src = srcBuffer.row_ptr(0); uint32 srcBPR = srcBuffer.stride(); int32 left = (int32)viewRect.left; int32 top = (int32)viewRect.top; int32 right = (int32)viewRect.right; int32 bottom = (int32)viewRect.bottom; #if DEBUG_DRAW_BITMAP if (left - xOffset < 0 || left - xOffset >= (int32)srcBuffer.width() || right - xOffset >= (int32)srcBuffer.width() || top - yOffset < 0 || top - yOffset >= (int32)srcBuffer.height() || bottom - yOffset >= (int32)srcBuffer.height()) { char message[256]; sprintf(message, "reading outside of bitmap (%ld, %ld, %ld, %ld) " "(%d, %d) (%ld, %ld)", left - xOffset, top - yOffset, right - xOffset, bottom - yOffset, srcBuffer.width(), srcBuffer.height(), xOffset, yOffset); debugger(message); } #endif const rgb_color* colorMap = SystemPalette(); // copy rects, iterate over clipping boxes fBaseRenderer.first_clip_box(); do { int32 x1 = max_c(fBaseRenderer.xmin(), left); int32 x2 = min_c(fBaseRenderer.xmax(), right); if (x1 <= x2) { int32 y1 = max_c(fBaseRenderer.ymin(), top); int32 y2 = min_c(fBaseRenderer.ymax(), bottom); if (y1 <= y2) { uint8* dstHandle = dst + y1 * dstBPR + x1 * 4; const uint8* srcHandle = src + (y1 - yOffset) * srcBPR + (x1 - xOffset) * bytesPerSourcePixel; for (; y1 <= y2; y1++) { copyRowFunction(dstHandle, srcHandle, x2 - x1 + 1, colorMap); dstHandle += dstBPR; srcHandle += srcBPR; } } } } while (fBaseRenderer.next_clip_box()); } // _DrawBitmapGeneric32 void Painter::_DrawBitmapGeneric32(agg::rendering_buffer& srcBuffer, double xOffset, double yOffset, double xScale, double yScale, BRect viewRect) const { // AGG pipeline // pixel format attached to bitmap typedef agg::pixfmt_bgra32 pixfmt_image; pixfmt_image pixf_img(srcBuffer); agg::trans_affine srcMatrix; // NOTE: R5 seems to ignore this offset when drawing bitmaps // srcMatrix *= agg::trans_affine_translation(-actualBitmapRect.left, // -actualBitmapRect.top); agg::trans_affine imgMatrix; imgMatrix *= agg::trans_affine_scaling(xScale, yScale); imgMatrix *= agg::trans_affine_translation(xOffset, yOffset); imgMatrix.invert(); // image interpolator typedef agg::span_interpolator_linear<> interpolator_type; interpolator_type interpolator(imgMatrix); // scanline allocator agg::span_allocator spanAllocator; // image accessor attached to pixel format of bitmap typedef agg::image_accessor_clip source_type; source_type source(pixf_img, agg::rgba8(0, 0, 0, 0)); // image filter (nearest neighbor) typedef agg::span_image_filter_rgba_nn span_gen_type; span_gen_type spanGenerator(source, interpolator); // clip to the current clipping region's frame viewRect = viewRect & fClippingRegion->Frame(); // convert to pixel coords (versus pixel indices) viewRect.right++; viewRect.bottom++; // path enclosing the bitmap fPath.remove_all(); fPath.move_to(viewRect.left, viewRect.top); fPath.line_to(viewRect.right, viewRect.top); fPath.line_to(viewRect.right, viewRect.bottom); fPath.line_to(viewRect.left, viewRect.bottom); fPath.close_polygon(); agg::conv_transform transformedPath(fPath, srcMatrix); fRasterizer.reset(); fRasterizer.add_path(transformedPath); // render the path with the bitmap as scanline fill agg::render_scanlines_aa(fRasterizer, fUnpackedScanline, fBaseRenderer, spanAllocator, spanGenerator); } // _InvertRect32 void Painter::_InvertRect32(BRect r) const { int32 width = r.IntegerWidth() + 1; for (int32 y = (int32)r.top; y <= (int32)r.bottom; y++) { uint8* dst = fBuffer.row_ptr(y); dst += (int32)r.left * 4; for (int32 i = 0; i < width; i++) { dst[0] = 255 - dst[0]; dst[1] = 255 - dst[1]; dst[2] = 255 - dst[2]; dst += 4; } } } // _BlendRect32 void Painter::_BlendRect32(const BRect& r, const rgb_color& c) const { if (!fValidClipping) return; uint8* dst = fBuffer.row_ptr(0); uint32 bpr = fBuffer.stride(); int32 left = (int32)r.left; int32 top = (int32)r.top; int32 right = (int32)r.right; int32 bottom = (int32)r.bottom; // fill rects, iterate over clipping boxes fBaseRenderer.first_clip_box(); do { int32 x1 = max_c(fBaseRenderer.xmin(), left); int32 x2 = min_c(fBaseRenderer.xmax(), right); if (x1 <= x2) { int32 y1 = max_c(fBaseRenderer.ymin(), top); int32 y2 = min_c(fBaseRenderer.ymax(), bottom); uint8* offset = dst + x1 * 4 + y1 * bpr; for (; y1 <= y2; y1++) { blend_line32(offset, x2 - x1 + 1, c.red, c.green, c.blue, c.alpha); offset += bpr; } } } while (fBaseRenderer.next_clip_box()); } // #pragma mark - template BRect Painter::_BoundingBox(VertexSource& path) const { double left = 0.0; double top = 0.0; double right = -1.0; double bottom = -1.0; uint32 pathID[1]; pathID[0] = 0; agg::bounding_rect(path, pathID, 0, 1, &left, &top, &right, &bottom); return BRect(left, top, right, bottom); } // agg_line_cap_mode_for inline agg::line_cap_e agg_line_cap_mode_for(cap_mode mode) { switch (mode) { case B_BUTT_CAP: return agg::butt_cap; case B_SQUARE_CAP: return agg::square_cap; case B_ROUND_CAP: return agg::round_cap; } return agg::butt_cap; } // agg_line_join_mode_for inline agg::line_join_e agg_line_join_mode_for(join_mode mode) { switch (mode) { case B_MITER_JOIN: return agg::miter_join; case B_ROUND_JOIN: return agg::round_join; case B_BEVEL_JOIN: case B_BUTT_JOIN: // ?? case B_SQUARE_JOIN: // ?? return agg::bevel_join; } return agg::miter_join; } // _StrokePath template BRect Painter::_StrokePath(VertexSource& path) const { agg::conv_stroke stroke(path); stroke.width(fPenSize); stroke.line_cap(agg_line_cap_mode_for(fLineCapMode)); stroke.line_join(agg_line_join_mode_for(fLineJoinMode)); stroke.miter_limit(fMiterLimit); fRasterizer.reset(); fRasterizer.add_path(stroke); agg::render_scanlines(fRasterizer, fPackedScanline, fRenderer); BRect touched = _BoundingBox(path); float penSize = ceilf(fPenSize / 2.0); touched.InsetBy(-penSize, -penSize); return _Clipped(touched); } // _FillPath template BRect Painter::_FillPath(VertexSource& path) const { fRasterizer.reset(); fRasterizer.add_path(path); agg::render_scanlines(fRasterizer, fPackedScanline, fRenderer); return _Clipped(_BoundingBox(path)); }