Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Unified Diff: src/core/SkDraw.cpp

Issue 179343005: Add a class to allocate small objects w/o extra calls to new. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rename the .h file Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkDraw.cpp
diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp
index 175abb68440b34de7270c543c354fd9c273415b6..05b06453dbf200df8cebccfc3aee4c69825d5143 100644
--- a/src/core/SkDraw.cpp
+++ b/src/core/SkDraw.cpp
@@ -21,9 +21,9 @@
#include "SkRRect.h"
#include "SkScan.h"
#include "SkShader.h"
+#include "SkSmallAllocator.h"
#include "SkString.h"
#include "SkStroke.h"
-#include "SkTemplatesPriv.h"
#include "SkTLazy.h"
#include "SkUtils.h"
@@ -32,9 +32,9 @@
#include "SkDrawProcs.h"
#include "SkMatrixUtils.h"
+
//#define TRACE_BITMAP_DRAWS
-#define kBlitterStorageLongCount (sizeof(SkBitmapProcShader) >> 2)
/** Helper for allocating small blitters on the stack.
*/
@@ -45,16 +45,8 @@ public:
}
SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix,
const SkPaint& paint, bool drawCoverage = false) {
- fBlitter = SkBlitter::Choose(device, matrix, paint,
- fStorage, sizeof(fStorage), drawCoverage);
- }
-
- ~SkAutoBlitterChoose() {
- if ((void*)fBlitter == (void*)fStorage) {
- fBlitter->~SkBlitter();
- } else {
- SkDELETE(fBlitter);
- }
+ fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator,
+ drawCoverage);
}
SkBlitter* operator->() { return fBlitter; }
@@ -63,13 +55,13 @@ public:
void choose(const SkBitmap& device, const SkMatrix& matrix,
const SkPaint& paint) {
SkASSERT(!fBlitter);
- fBlitter = SkBlitter::Choose(device, matrix, paint,
- fStorage, sizeof(fStorage));
+ fBlitter = SkBlitter::Choose(device, matrix, paint, &fAllocator);
}
private:
- SkBlitter* fBlitter;
- uint32_t fStorage[kBlitterStorageLongCount];
+ // Owned by fAllocator, which will handle the delete.
+ SkBlitter* fBlitter;
+ SkTBlitterAllocator fAllocator;
};
#define SkAutoBlitterChoose(...) SK_REQUIRE_LOCAL_VAR(SkAutoBlitterChoose)
@@ -82,34 +74,29 @@ class SkAutoBitmapShaderInstall : SkNoncopyable {
public:
SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint& paint)
: fPaint(paint) /* makes a copy of the paint */ {
- fPaint.setShader(SkShader::CreateBitmapShader(src,
- SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
- fStorage, sizeof(fStorage)));
+ fPaint.setShader(CreateBitmapShader(src, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode,
+ &fAllocator));
// we deliberately left the shader with an owner-count of 2
SkASSERT(2 == fPaint.getShader()->getRefCnt());
}
~SkAutoBitmapShaderInstall() {
- SkShader* shader = fPaint.getShader();
- // since we manually destroy shader, we insist that owners == 2
- SkASSERT(2 == shader->getRefCnt());
+ // since fAllocator will destroy shader, we insist that owners == 2
+ SkASSERT(2 == fPaint.getShader()->getRefCnt());
fPaint.setShader(NULL); // unref the shader by 1
- // now destroy to take care of the 2nd owner-count
- if ((void*)shader == (void*)fStorage) {
- shader->~SkShader();
- } else {
- SkDELETE(shader);
- }
}
// return the new paint that has the shader applied
const SkPaint& paintWithShader() const { return fPaint; }
private:
- SkPaint fPaint; // copy of caller's paint (which we then modify)
- uint32_t fStorage[kBlitterStorageLongCount];
+ // copy of caller's paint (which we then modify)
+ SkPaint fPaint;
+ // Stores the shader.
+ SkTBlitterAllocator fAllocator;
};
#define SkAutoBitmapShaderInstall(...) SK_REQUIRE_LOCAL_VAR(SkAutoBitmapShaderInstall)
@@ -1323,12 +1310,11 @@ void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix,
int ix = SkScalarRoundToInt(matrix.getTranslateX());
int iy = SkScalarRoundToInt(matrix.getTranslateY());
if (clipHandlesSprite(*fRC, ix, iy, bitmap)) {
- uint32_t storage[kBlitterStorageLongCount];
- SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
- ix, iy, storage, sizeof(storage));
+ SkTBlitterAllocator allocator;
+ // blitter will be owned by the allocator.
+ SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
+ ix, iy, &allocator);
if (blitter) {
- SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
-
SkIRect ir;
ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height());
@@ -1378,13 +1364,12 @@ void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y,
paint.setStyle(SkPaint::kFill_Style);
if (NULL == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, bitmap)) {
- uint32_t storage[kBlitterStorageLongCount];
- SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
- x, y, storage, sizeof(storage));
+ SkTBlitterAllocator allocator;
+ // blitter will be owned by the allocator.
+ SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap,
+ x, y, &allocator);
if (blitter) {
- SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage);
-
if (fBounder && !fBounder->doIRect(bounds)) {
return;
}
« no previous file with comments | « src/core/SkCoreBlitters.h ('k') | src/core/SkShader.cpp » ('j') | src/core/SkSmallAllocator.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698