Replace SkPictureReplacementPlayback with GrRecordReplaceDraw
I think this is sufficiently specialized to keep it in Ganesh for the time being. R=bsalomon@google.com, mtklein@google.com Author: robertphillips@google.com Review URL: https://codereview.chromium.org/535953002
This commit is contained in:
parent
74364c9774
commit
d982eb22d7
@ -107,6 +107,8 @@
|
||||
'<(skia_src_path)/gpu/GrPictureUtils.h',
|
||||
'<(skia_src_path)/gpu/GrPictureUtils.cpp',
|
||||
'<(skia_src_path)/gpu/GrPlotMgr.h',
|
||||
'<(skia_src_path)/gpu/GrRecordReplaceDraw.cpp',
|
||||
'<(skia_src_path)/gpu/GrRecordReplaceDraw.h',
|
||||
'<(skia_src_path)/gpu/GrRectanizer.h',
|
||||
'<(skia_src_path)/gpu/GrRectanizer_pow2.cpp',
|
||||
'<(skia_src_path)/gpu/GrRectanizer_pow2.h',
|
||||
|
@ -166,6 +166,7 @@
|
||||
'../tests/ReadWriteAlphaTest.cpp',
|
||||
'../tests/Reader32Test.cpp',
|
||||
'../tests/RecordDrawTest.cpp',
|
||||
'../tests/RecordReplaceDrawTest.cpp',
|
||||
'../tests/RecordOptsTest.cpp',
|
||||
'../tests/RecordPatternTest.cpp',
|
||||
'../tests/RecordTest.cpp',
|
||||
|
@ -8,6 +8,8 @@
|
||||
#ifndef SkDrawPictureCallback_DEFINED
|
||||
#define SkDrawPictureCallback_DEFINED
|
||||
|
||||
#include "SkTypes.h"
|
||||
|
||||
/**
|
||||
* Subclasses of this can be passed to canvas.drawPicture(). During the drawing
|
||||
* of the picture, this callback will periodically be invoked. If its
|
||||
|
119
src/gpu/GrRecordReplaceDraw.cpp
Normal file
119
src/gpu/GrRecordReplaceDraw.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2014 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "GrRecordReplaceDraw.h"
|
||||
#include "SkImage.h"
|
||||
#include "SkRecordDraw.h"
|
||||
|
||||
GrReplacements::ReplacementInfo* GrReplacements::push() {
|
||||
SkDEBUGCODE(this->validate());
|
||||
return fReplacements.push();
|
||||
}
|
||||
|
||||
void GrReplacements::freeAll() {
|
||||
for (int i = 0; i < fReplacements.count(); ++i) {
|
||||
fReplacements[i].fImage->unref();
|
||||
}
|
||||
fReplacements.reset();
|
||||
}
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
void GrReplacements::validate() const {
|
||||
// Check that the ranges are monotonically increasing and non-overlapping
|
||||
if (fReplacements.count() > 0) {
|
||||
SkASSERT(fReplacements[0].fStart < fReplacements[0].fStop);
|
||||
|
||||
for (int i = 1; i < fReplacements.count(); ++i) {
|
||||
SkASSERT(fReplacements[i].fStart < fReplacements[i].fStop);
|
||||
SkASSERT(fReplacements[i - 1].fStop < fReplacements[i].fStart);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const GrReplacements::ReplacementInfo*
|
||||
GrReplacements::lookupByStart(size_t start, int* searchStart) const {
|
||||
SkDEBUGCODE(this->validate());
|
||||
for (int i = *searchStart; i < fReplacements.count(); ++i) {
|
||||
if (start == fReplacements[i].fStart) {
|
||||
*searchStart = i + 1;
|
||||
return &fReplacements[i];
|
||||
} else if (start < fReplacements[i].fStart) {
|
||||
return NULL; // the ranges are monotonically increasing and non-overlapping
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline void draw_replacement_bitmap(const GrReplacements::ReplacementInfo* ri,
|
||||
SkCanvas* canvas) {
|
||||
SkRect src = SkRect::Make(ri->fSrcRect);
|
||||
SkRect dst = SkRect::MakeXYWH(SkIntToScalar(ri->fPos.fX),
|
||||
SkIntToScalar(ri->fPos.fY),
|
||||
SkIntToScalar(ri->fSrcRect.width()),
|
||||
SkIntToScalar(ri->fSrcRect.height()));
|
||||
ri->fImage->draw(canvas, &src, dst, ri->fPaint);
|
||||
}
|
||||
|
||||
void GrRecordReplaceDraw(const SkRecord& record,
|
||||
SkCanvas* canvas,
|
||||
const SkBBoxHierarchy* bbh,
|
||||
const GrReplacements* replacements,
|
||||
SkDrawPictureCallback* callback) {
|
||||
SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
|
||||
|
||||
SkRecords::Draw draw(canvas);
|
||||
const GrReplacements::ReplacementInfo* ri = NULL;
|
||||
int searchStart = 0;
|
||||
|
||||
if (NULL != bbh) {
|
||||
// Draw only ops that affect pixels in the canvas's current clip.
|
||||
// The SkRecord and BBH were recorded in identity space. This canvas
|
||||
// is not necessarily in that same space. getClipBounds() returns us
|
||||
// this canvas' clip bounds transformed back into identity space, which
|
||||
// lets us query the BBH.
|
||||
SkRect query = { 0, 0, 0, 0 };
|
||||
(void)canvas->getClipBounds(&query);
|
||||
|
||||
SkTDArray<void*> ops;
|
||||
bbh->search(query, &ops);
|
||||
|
||||
for (int i = 0; i < ops.count(); i++) {
|
||||
if (NULL != callback && callback->abortDrawing()) {
|
||||
return;
|
||||
}
|
||||
ri = replacements->lookupByStart(i, &searchStart);
|
||||
if (NULL != ri) {
|
||||
draw_replacement_bitmap(ri, canvas);
|
||||
|
||||
while ((uintptr_t)ops[i] < ri->fStop) {
|
||||
++i;
|
||||
}
|
||||
SkASSERT((uintptr_t)ops[i] == ri->fStop);
|
||||
continue;
|
||||
}
|
||||
|
||||
record.visit<void>((uintptr_t)ops[i], draw);
|
||||
}
|
||||
} else {
|
||||
for (unsigned int i = 0; i < record.count(); ++i) {
|
||||
if (NULL != callback && callback->abortDrawing()) {
|
||||
return;
|
||||
}
|
||||
ri = replacements->lookupByStart(i, &searchStart);
|
||||
if (NULL != ri) {
|
||||
draw_replacement_bitmap(ri, canvas);
|
||||
|
||||
i = ri->fStop;
|
||||
continue;
|
||||
}
|
||||
|
||||
record.visit<void>(i, draw);
|
||||
}
|
||||
}
|
||||
}
|
69
src/gpu/GrRecordReplaceDraw.h
Normal file
69
src/gpu/GrRecordReplaceDraw.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2014 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef GrRecordReplaceDraw_DEFINED
|
||||
#define GrRecordReplaceDraw_DEFINED
|
||||
|
||||
#include "SkDrawPictureCallback.h"
|
||||
#include "SkRect.h"
|
||||
#include "SkTDArray.h"
|
||||
|
||||
class SkBBoxHierarchy;
|
||||
class SkBitmap;
|
||||
class SkCanvas;
|
||||
class SkImage;
|
||||
class SkPaint;
|
||||
class SkRecord;
|
||||
|
||||
// GrReplacements collects op ranges that can be replaced with
|
||||
// a single drawBitmap call (using a precomputed bitmap).
|
||||
class GrReplacements {
|
||||
public:
|
||||
// All the operations between fStart and fStop (inclusive) will be replaced with
|
||||
// a single drawBitmap call using fPos, fBM and fPaint.
|
||||
struct ReplacementInfo {
|
||||
unsigned fStart;
|
||||
unsigned fStop;
|
||||
SkIPoint fPos;
|
||||
SkImage* fImage; // Owns a ref
|
||||
const SkPaint* fPaint; // Note: this object doesn't own the paint
|
||||
|
||||
SkIRect fSrcRect;
|
||||
};
|
||||
|
||||
~GrReplacements() { this->freeAll(); }
|
||||
|
||||
// Add a new replacement range. The replacement ranges should be
|
||||
// sorted in increasing order and non-overlapping (esp. no nested
|
||||
// saveLayers).
|
||||
ReplacementInfo* push();
|
||||
|
||||
// look up a replacement range by its start offset.
|
||||
// lastLookedUp is an in/out parameter that is used to speed up the search.
|
||||
// It should be initialized to 0 on the first call and then passed back in
|
||||
// unmodified on subsequent calls.
|
||||
const ReplacementInfo* lookupByStart(size_t start, int* lastLookedUp) const;
|
||||
|
||||
private:
|
||||
SkTDArray<ReplacementInfo> fReplacements;
|
||||
|
||||
void freeAll();
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
void validate() const;
|
||||
#endif
|
||||
};
|
||||
|
||||
// Draw an SkRecord into an SkCanvas replacing saveLayer/restore blocks with
|
||||
// drawBitmap calls. A convenience wrapper around SkRecords::Draw.
|
||||
void GrRecordReplaceDraw(const SkRecord&,
|
||||
SkCanvas*,
|
||||
const SkBBoxHierarchy*,
|
||||
const GrReplacements*,
|
||||
SkDrawPictureCallback*);
|
||||
|
||||
#endif // GrRecordReplaceDraw_DEFINED
|
125
tests/RecordReplaceDrawTest.cpp
Normal file
125
tests/RecordReplaceDrawTest.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2014 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#if SK_SUPPORT_GPU
|
||||
|
||||
#include "Test.h"
|
||||
#include "RecordTestUtils.h"
|
||||
|
||||
#include "SkBBHFactory.h"
|
||||
#include "SkRecordDraw.h"
|
||||
#include "SkRecorder.h"
|
||||
#include "SkUtils.h"
|
||||
#include "GrRecordReplaceDraw.h"
|
||||
|
||||
static const int kWidth = 100;
|
||||
static const int kHeight = 100;
|
||||
|
||||
class JustOneDraw : public SkDrawPictureCallback {
|
||||
public:
|
||||
JustOneDraw() : fCalls(0) {}
|
||||
|
||||
virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; }
|
||||
private:
|
||||
int fCalls;
|
||||
};
|
||||
|
||||
// Make sure the abort callback works
|
||||
DEF_TEST(RecordReplaceDraw_Abort, r) {
|
||||
// Record two commands.
|
||||
SkRecord record;
|
||||
SkRecorder recorder(&record, kWidth, kHeight);
|
||||
recorder.drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint());
|
||||
recorder.clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)));
|
||||
|
||||
SkRecord rerecord;
|
||||
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
||||
|
||||
GrReplacements replacements;
|
||||
JustOneDraw callback;
|
||||
GrRecordReplaceDraw(record, &canvas, NULL/*bbh*/, &replacements, &callback);
|
||||
|
||||
REPORTER_ASSERT(r, 3 == rerecord.count());
|
||||
assert_type<SkRecords::Save>(r, rerecord, 0);
|
||||
assert_type<SkRecords::DrawRect>(r, rerecord, 1);
|
||||
assert_type<SkRecords::Restore>(r, rerecord, 2);
|
||||
}
|
||||
|
||||
// Make sure GrRecordReplaceDraw balances unbalanced saves
|
||||
DEF_TEST(RecordReplaceDraw_Unbalanced, r) {
|
||||
SkRecord record;
|
||||
SkRecorder recorder(&record, kWidth, kHeight);
|
||||
recorder.save(); // We won't balance this, but GrRecordReplaceDraw will for us.
|
||||
|
||||
SkRecord rerecord;
|
||||
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
||||
|
||||
GrReplacements replacements;
|
||||
GrRecordReplaceDraw(record, &canvas, NULL/*bbh*/, &replacements, NULL/*callback*/);
|
||||
|
||||
REPORTER_ASSERT(r, 4 == rerecord.count());
|
||||
assert_type<SkRecords::Save>(r, rerecord, 0);
|
||||
assert_type<SkRecords::Save>(r, rerecord, 1);
|
||||
assert_type<SkRecords::Restore>(r, rerecord, 2);
|
||||
assert_type<SkRecords::Restore>(r, rerecord, 3);
|
||||
}
|
||||
|
||||
static SkImage* make_image(SkColor color) {
|
||||
const SkPMColor pmcolor = SkPreMultiplyColor(color);
|
||||
const SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
|
||||
const size_t rowBytes = info.minRowBytes();
|
||||
const size_t size = rowBytes * info.fHeight;
|
||||
|
||||
SkAutoMalloc addr(size);
|
||||
sk_memset32((SkPMColor*)addr.get(), pmcolor, SkToInt(size >> 2));
|
||||
|
||||
return SkImage::NewRasterCopy(info, addr.get(), rowBytes);
|
||||
}
|
||||
|
||||
// Test out the layer replacement functionality with and w/o a BBH
|
||||
void test_replacements(skiatest::Reporter* r, bool useBBH) {
|
||||
SkRecord record;
|
||||
SkRecorder recorder(&record, kWidth, kHeight);
|
||||
SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint));
|
||||
recorder.saveLayer(NULL, paint);
|
||||
recorder.clear(SK_ColorRED);
|
||||
recorder.restore();
|
||||
recorder.drawRect(SkRect::MakeWH(SkIntToScalar(kWidth/2), SkIntToScalar(kHeight/2)),
|
||||
SkPaint());
|
||||
|
||||
GrReplacements replacements;
|
||||
GrReplacements::ReplacementInfo* ri = replacements.push();
|
||||
ri->fStart = 0;
|
||||
ri->fStop = 2;
|
||||
ri->fPos.set(0, 0);
|
||||
ri->fImage = make_image(SK_ColorRED);
|
||||
ri->fPaint = paint;
|
||||
ri->fSrcRect = SkIRect::MakeWH(kWidth, kHeight);
|
||||
|
||||
SkAutoTUnref<SkBBoxHierarchy> bbh;
|
||||
|
||||
if (useBBH) {
|
||||
SkRTreeFactory factory;
|
||||
bbh.reset((factory)(kWidth, kHeight));
|
||||
SkRecordFillBounds(record, bbh);
|
||||
}
|
||||
|
||||
SkRecord rerecord;
|
||||
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
||||
GrRecordReplaceDraw(record, &canvas, bbh, &replacements, NULL/*callback*/);
|
||||
|
||||
REPORTER_ASSERT(r, 4 == rerecord.count());
|
||||
assert_type<SkRecords::Save>(r, rerecord, 0);
|
||||
assert_type<SkRecords::DrawBitmapRectToRect>(r, rerecord, 1);
|
||||
assert_type<SkRecords::DrawRect>(r, rerecord, 2);
|
||||
assert_type<SkRecords::Restore>(r, rerecord, 3);
|
||||
}
|
||||
|
||||
DEF_TEST(RecordReplaceDraw_Replace, r) { test_replacements(r, false); }
|
||||
DEF_TEST(RecordReplaceDraw_ReplaceWithBBH, r) { test_replacements(r, true); }
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user