Add SkPictureRecord subclass that computes bounding boxes.

Review URL: https://codereview.appspot.com/6506082

git-svn-id: http://skia.googlecode.com/svn/trunk@5423 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
rileya@google.com 2012-09-06 20:50:11 +00:00
Родитель f06df1bb9a
Коммит e0201a4448
3 изменённых файлов: 331 добавлений и 0 удалений

Просмотреть файл

@ -15,6 +15,8 @@
'<(skia_src_path)/core/SkAlphaRuns.cpp',
'<(skia_src_path)/core/SkAntiRun.h',
'<(skia_src_path)/core/SkBBoxHierarchy.h',
'<(skia_src_path)/core/SkBBoxRecord.cpp',
'<(skia_src_path)/core/SkBBoxRecord.h',
'<(skia_src_path)/core/SkBitmap.cpp',
'<(skia_src_path)/core/SkBitmapHeap.cpp',
'<(skia_src_path)/core/SkBitmapHeap.h',

253
src/core/SkBBoxRecord.cpp Normal file
Просмотреть файл

@ -0,0 +1,253 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBBoxRecord.h"
void SkBBoxRecord::drawRect(const SkRect& rect, const SkPaint& paint) {
if (this->transformBounds(rect, &paint)) {
INHERITED::drawRect(rect, paint);
}
}
void SkBBoxRecord::drawPath(const SkPath& path, const SkPaint& paint) {
if (this->transformBounds(path.getBounds(), &paint)) {
INHERITED::drawPath(path, paint);
}
}
void SkBBoxRecord::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
const SkPaint& paint) {
SkRect bbox;
bbox.set(pts, count);
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawPoints(mode, count, pts, paint);
}
}
void SkBBoxRecord::drawPaint(const SkPaint& paint) {
SkRect bbox;
if (this->getClipBounds(&bbox)) {
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawPaint(paint);
}
}
}
void SkBBoxRecord::clear(SkColor color) {
SkISize size = this->getDeviceSize();
SkRect bbox = {0, 0, size.width(), size.height()};
this->handleBBox(bbox);
INHERITED::clear(color);
}
void SkBBoxRecord::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
const SkPaint& paint) {
SkRect bbox;
paint.measureText(text, byteLength, &bbox);
SkPaint::FontMetrics metrics;
paint.getFontMetrics(&metrics);
// Vertical and aligned text need to be offset
if (paint.isVerticalText()) {
SkScalar h = bbox.fBottom - bbox.fTop;
if (paint.getTextAlign() == SkPaint::kCenter_Align) {
bbox.fTop -= h / 2;
bbox.fBottom -= h / 2;
}
// Pad top and bottom with max extents from FontMetrics
bbox.fBottom += metrics.fBottom;
bbox.fTop += metrics.fTop;
} else {
SkScalar w = bbox.fRight - bbox.fLeft;
if (paint.getTextAlign() == SkPaint::kCenter_Align) {
bbox.fLeft -= w / 2;
bbox.fRight -= w / 2;
} else if (paint.getTextAlign() == SkPaint::kRight_Align) {
bbox.fLeft -= w;
bbox.fRight -= w;
}
// Set vertical bounds to max extents from font metrics
bbox.fTop = metrics.fTop;
bbox.fBottom = metrics.fBottom;
}
// Pad horizontal bounds on each side by half of max vertical extents (this is sort of
// arbitrary, but seems to produce reasonable results, if there were a way of getting max
// glyph X-extents to pad by, that may be better here, but FontMetrics fXMin and fXMax seem
// incorrect on most platforms (too small in Linux, never even set in Windows).
SkScalar pad = (metrics.fBottom - metrics.fTop) / 2;
bbox.fLeft -= pad;
bbox.fRight += pad;
bbox.fLeft += x;
bbox.fRight += x;
bbox.fTop += y;
bbox.fBottom += y;
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawText(text, byteLength, x, y, paint);
}
}
void SkBBoxRecord::drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
const SkPaint* paint) {
SkRect bbox = {left, top, left + bitmap.width(), top + bitmap.height()};
if (this->transformBounds(bbox, paint)) {
INHERITED::drawBitmap(bitmap, left, top, paint);
}
}
void SkBBoxRecord::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
const SkRect& dst, const SkPaint* paint) {
if (this->transformBounds(dst, paint)) {
INHERITED::drawBitmapRect(bitmap, src, dst, paint);
}
}
void SkBBoxRecord::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
const SkPaint* paint) {
SkMatrix m = mat;
SkRect bbox = {0, 0, bitmap.width(), bitmap.height()};
m.mapRect(&bbox);
if (this->transformBounds(bbox, paint)) {
INHERITED::drawBitmapMatrix(bitmap, mat, paint);
}
}
void SkBBoxRecord::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) {
if (this->transformBounds(dst, paint)) {
INHERITED::drawBitmapNine(bitmap, center, dst, paint);
}
}
void SkBBoxRecord::drawPosText(const void* text, size_t byteLength,
const SkPoint pos[], const SkPaint& paint) {
SkRect bbox;
bbox.set(pos, paint.countText(text, byteLength));
SkPaint::FontMetrics metrics;
paint.getFontMetrics(&metrics);
bbox.fTop += metrics.fTop;
bbox.fBottom += metrics.fBottom;
// pad on left and right by half of max vertical glyph extents
SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
bbox.fLeft += pad;
bbox.fRight -= pad;
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawPosText(text, byteLength, pos, paint);
}
}
void SkBBoxRecord::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
SkScalar constY, const SkPaint& paint) {
SkRect bbox;
size_t numChars = paint.countText(text, byteLength);
if (numChars > 0) {
bbox.fLeft = xpos[0];
bbox.fRight = xpos[numChars - 1];
// if we had a guarantee that these will be monotonically increasing, this could be sped up
for (size_t i = 1; i < numChars; ++i) {
if (xpos[i] < bbox.fLeft) {
bbox.fLeft = xpos[i];
}
if (xpos[i] > bbox.fRight) {
bbox.fRight = xpos[i];
}
}
SkPaint::FontMetrics metrics;
paint.getFontMetrics(&metrics);
// pad horizontally by half max glyph height
SkScalar pad = (metrics.fTop - metrics.fBottom) / 2;
bbox.fLeft += pad;
bbox.fRight -= pad;
bbox.fTop = metrics.fTop + constY;
bbox.fBottom = metrics.fBottom + constY;
if (!this->transformBounds(bbox, &paint)) {
return;
}
}
INHERITED::drawPosTextH(text, byteLength, xpos, constY, paint);
}
void SkBBoxRecord::drawSprite(const SkBitmap& bitmap, int left, int top,
const SkPaint* paint) {
SkRect bbox = {left, top, left + bitmap.width(), top + bitmap.height()};
this->handleBBox(bbox); // directly call handleBBox, matrix is ignored
INHERITED::drawBitmap(bitmap, left, top, paint);
}
void SkBBoxRecord::drawTextOnPath(const void* text, size_t byteLength,
const SkPath& path, const SkMatrix* matrix,
const SkPaint& paint) {
SkRect bbox = path.getBounds();
SkPaint::FontMetrics metrics;
paint.getFontMetrics(&metrics);
// pad out all sides by the max glyph height above baseline
SkScalar pad = metrics.fTop;
bbox.fLeft += pad;
bbox.fRight -= pad;
bbox.fTop += pad;
bbox.fBottom -= pad;
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawTextOnPath(text, byteLength, path, matrix, paint);
}
}
void SkBBoxRecord::drawVertices(VertexMode mode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[],
const SkColor colors[], SkXfermode* xfer,
const uint16_t indices[], int indexCount,
const SkPaint& paint) {
SkRect bbox;
bbox.set(vertices, vertexCount);
if (this->transformBounds(bbox, &paint)) {
INHERITED::drawVertices(mode, vertexCount, vertices, texs,
colors, xfer, indices, indexCount, paint);
}
}
void SkBBoxRecord::drawPicture(SkPicture& picture) {
SkRect bbox = {0, 0, picture.width(), picture.height()};
if (this->transformBounds(bbox, NULL)) {
INHERITED::drawPicture(picture);
}
}
bool SkBBoxRecord::transformBounds(const SkRect& bounds, const SkPaint* paint) {
SkRect outBounds = bounds;
if (paint) {
// account for stroking, path effects, shadows, etc
if (paint->canComputeFastBounds()) {
SkRect temp;
outBounds = paint->computeFastBounds(bounds, &temp);
} else {
// set bounds to current clip
if (!this->getClipBounds(&outBounds)) {
// current clip is empty
return false;
}
}
}
SkRect clip;
if (this->getClipBounds(&clip) && outBounds.intersect(clip)) {
this->getTotalMatrix().mapRect(&outBounds);
this->handleBBox(outBounds);
return true;
}
return false;
}

76
src/core/SkBBoxRecord.h Normal file
Просмотреть файл

@ -0,0 +1,76 @@
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkBBoxRecord_DEFINED
#define SkBBoxRecord_DEFINED
#include "SkPictureRecord.h"
/**
* This is an abstract SkPictureRecord subclass that intercepts draw calls and computes an
* axis-aligned bounding box for each draw that it sees, subclasses implement handleBBox()
* which will be called every time we get a new bounding box.
*/
class SkBBoxRecord : public SkPictureRecord {
public:
SkBBoxRecord(uint32_t recordFlags) :INHERITED(recordFlags) { }
virtual ~SkBBoxRecord() { }
/**
* This is called each time we get a bounding box, it will be axis-aligned,
* in device coordinates, and expanded to include stroking, shadows, etc.
*/
virtual void handleBBox(const SkRect& bbox) = 0;
virtual void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
const SkPaint& paint) SK_OVERRIDE;
virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
virtual void clear(SkColor) SK_OVERRIDE;
virtual void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
const SkPaint& paint) SK_OVERRIDE;
virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
const SkPaint* paint = NULL) SK_OVERRIDE;
virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
const SkRect& dst, const SkPaint* paint) SK_OVERRIDE;
virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
const SkPaint* paint) SK_OVERRIDE;
virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
const SkRect& dst, const SkPaint* paint) SK_OVERRIDE;
virtual void drawPosText(const void* text, size_t byteLength,
const SkPoint pos[], const SkPaint& paint) SK_OVERRIDE;
virtual void drawPosTextH(const void* text, size_t byteLength,
const SkScalar xpos[], SkScalar constY,
const SkPaint& paint) SK_OVERRIDE;
virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
const SkPaint* paint) SK_OVERRIDE;
virtual void drawTextOnPath(const void* text, size_t byteLength,
const SkPath& path, const SkMatrix* matrix,
const SkPaint& paint) SK_OVERRIDE;
virtual void drawVertices(VertexMode mode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[],
const SkColor colors[], SkXfermode* xfer,
const uint16_t indices[], int indexCount,
const SkPaint& paint) SK_OVERRIDE;
virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
private:
/**
* Takes a bounding box in current canvas view space, accounts for stroking and effects, and
* computes an axis-aligned bounding box in device coordinates, then passes it to handleBBox()
* returns false if the draw is completely clipped out, and may safely be ignored.
**/
bool transformBounds(const SkRect& bounds, const SkPaint* paint);
typedef SkPictureRecord INHERITED;
};
#endif