Merge "Preliminary support for clipRect(Rect, Op)"

This commit is contained in:
Romain Guy
2012-02-07 17:07:00 -08:00
committed by Android (Google) Code Review
7 changed files with 246 additions and 22 deletions

View File

@@ -12734,9 +12734,9 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal
* </p> * </p>
* *
* <p> * <p>
* The actual mesurement work of a view is performed in * The actual measurement work of a view is performed in
* {@link #onMeasure(int, int)}, called by this method. Therefore, only * {@link #onMeasure(int, int)}, called by this method. Therefore, only
* {@link #onMeasure(int, int)} can and must be overriden by subclasses. * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
* </p> * </p>
* *
* *

View File

@@ -55,43 +55,51 @@ public:
void set(uint32_t w, uint32_t h); void set(uint32_t w, uint32_t h);
Region& orSelf(const Rect& rhs); Region& orSelf(const Rect& rhs);
Region& xorSelf(const Rect& rhs);
Region& andSelf(const Rect& rhs); Region& andSelf(const Rect& rhs);
Region& subtractSelf(const Rect& rhs); Region& subtractSelf(const Rect& rhs);
// boolean operators, applied on this // boolean operators, applied on this
Region& orSelf(const Region& rhs); Region& orSelf(const Region& rhs);
Region& xorSelf(const Region& rhs);
Region& andSelf(const Region& rhs); Region& andSelf(const Region& rhs);
Region& subtractSelf(const Region& rhs); Region& subtractSelf(const Region& rhs);
// boolean operators // boolean operators
const Region merge(const Rect& rhs) const; const Region merge(const Rect& rhs) const;
const Region mergeExclusive(const Rect& rhs) const;
const Region intersect(const Rect& rhs) const; const Region intersect(const Rect& rhs) const;
const Region subtract(const Rect& rhs) const; const Region subtract(const Rect& rhs) const;
// boolean operators // boolean operators
const Region merge(const Region& rhs) const; const Region merge(const Region& rhs) const;
const Region mergeExclusive(const Region& rhs) const;
const Region intersect(const Region& rhs) const; const Region intersect(const Region& rhs) const;
const Region subtract(const Region& rhs) const; const Region subtract(const Region& rhs) const;
// these translate rhs first // these translate rhs first
Region& translateSelf(int dx, int dy); Region& translateSelf(int dx, int dy);
Region& orSelf(const Region& rhs, int dx, int dy); Region& orSelf(const Region& rhs, int dx, int dy);
Region& xorSelf(const Region& rhs, int dx, int dy);
Region& andSelf(const Region& rhs, int dx, int dy); Region& andSelf(const Region& rhs, int dx, int dy);
Region& subtractSelf(const Region& rhs, int dx, int dy); Region& subtractSelf(const Region& rhs, int dx, int dy);
// these translate rhs first // these translate rhs first
const Region translate(int dx, int dy) const; const Region translate(int dx, int dy) const;
const Region merge(const Region& rhs, int dx, int dy) const; const Region merge(const Region& rhs, int dx, int dy) const;
const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
const Region intersect(const Region& rhs, int dx, int dy) const; const Region intersect(const Region& rhs, int dx, int dy) const;
const Region subtract(const Region& rhs, int dx, int dy) const; const Region subtract(const Region& rhs, int dx, int dy) const;
// convenience operators overloads // convenience operators overloads
inline const Region operator | (const Region& rhs) const; inline const Region operator | (const Region& rhs) const;
inline const Region operator ^ (const Region& rhs) const;
inline const Region operator & (const Region& rhs) const; inline const Region operator & (const Region& rhs) const;
inline const Region operator - (const Region& rhs) const; inline const Region operator - (const Region& rhs) const;
inline const Region operator + (const Point& pt) const; inline const Region operator + (const Point& pt) const;
inline Region& operator |= (const Region& rhs); inline Region& operator |= (const Region& rhs);
inline Region& operator ^= (const Region& rhs);
inline Region& operator &= (const Region& rhs); inline Region& operator &= (const Region& rhs);
inline Region& operator -= (const Region& rhs); inline Region& operator -= (const Region& rhs);
inline Region& operator += (const Point& pt); inline Region& operator += (const Point& pt);
@@ -158,6 +166,9 @@ private:
const Region Region::operator | (const Region& rhs) const { const Region Region::operator | (const Region& rhs) const {
return merge(rhs); return merge(rhs);
} }
const Region Region::operator ^ (const Region& rhs) const {
return mergeExclusive(rhs);
}
const Region Region::operator & (const Region& rhs) const { const Region Region::operator & (const Region& rhs) const {
return intersect(rhs); return intersect(rhs);
} }
@@ -172,6 +183,9 @@ const Region Region::operator + (const Point& pt) const {
Region& Region::operator |= (const Region& rhs) { Region& Region::operator |= (const Region& rhs) {
return orSelf(rhs); return orSelf(rhs);
} }
Region& Region::operator ^= (const Region& rhs) {
return xorSelf(rhs);
}
Region& Region::operator &= (const Region& rhs) { Region& Region::operator &= (const Region& rhs) {
return andSelf(rhs); return andSelf(rhs);
} }

View File

@@ -31,6 +31,7 @@ Snapshot::Snapshot(): flags(0), previous(NULL), layer(NULL), fbo(0),
transform = &mTransformRoot; transform = &mTransformRoot;
clipRect = &mClipRectRoot; clipRect = &mClipRectRoot;
region = NULL; region = NULL;
clipRegion = NULL;
} }
/** /**
@@ -52,8 +53,21 @@ Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags):
if (saveFlags & SkCanvas::kClip_SaveFlag) { if (saveFlags & SkCanvas::kClip_SaveFlag) {
mClipRectRoot.set(*s->clipRect); mClipRectRoot.set(*s->clipRect);
clipRect = &mClipRectRoot; clipRect = &mClipRectRoot;
#if STENCIL_BUFFER_SIZE
if (s->clipRegion) {
mClipRegionRoot.merge(*s->clipRegion);
clipRegion = &mClipRegionRoot;
} else {
clipRegion = NULL;
}
#else
clipRegion = NULL;
#endif
} else { } else {
clipRect = s->clipRect; clipRect = s->clipRect;
#if STENCIL_BUFFER_SIZE
clipRegion = s->clipRegion;
#endif
} }
if (s->flags & Snapshot::kFlagFboTarget) { if (s->flags & Snapshot::kFlagFboTarget) {
@@ -68,6 +82,77 @@ Snapshot::Snapshot(const sp<Snapshot>& s, int saveFlags):
// Clipping // Clipping
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void Snapshot::ensureClipRegion() {
#if STENCIL_BUFFER_SIZE
if (!clipRegion) {
clipRegion = &mClipRegionRoot;
android::Rect tmp(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
clipRegion->set(tmp);
}
#endif
}
void Snapshot::copyClipRectFromRegion() {
#if STENCIL_BUFFER_SIZE
if (!clipRegion->isEmpty()) {
android::Rect bounds(clipRegion->bounds());
clipRect->set(bounds.left, bounds.top, bounds.right, bounds.bottom);
if (clipRegion->isRect()) {
clipRegion->clear();
clipRegion = NULL;
}
} else {
clipRect->setEmpty();
clipRegion = NULL;
}
#endif
}
bool Snapshot::clipRegionOr(float left, float top, float right, float bottom) {
#if STENCIL_BUFFER_SIZE
android::Rect tmp(left, top, right, bottom);
clipRegion->orSelf(tmp);
copyClipRectFromRegion();
return true;
#else
return false;
#endif
}
bool Snapshot::clipRegionXor(float left, float top, float right, float bottom) {
#if STENCIL_BUFFER_SIZE
android::Rect tmp(left, top, right, bottom);
clipRegion->xorSelf(tmp);
copyClipRectFromRegion();
return true;
#else
return false;
#endif
}
bool Snapshot::clipRegionAnd(float left, float top, float right, float bottom) {
#if STENCIL_BUFFER_SIZE
android::Rect tmp(left, top, right, bottom);
clipRegion->andSelf(tmp);
copyClipRectFromRegion();
return true;
#else
return false;
#endif
}
bool Snapshot::clipRegionNand(float left, float top, float right, float bottom) {
#if STENCIL_BUFFER_SIZE
android::Rect tmp(left, top, right, bottom);
clipRegion->subtractSelf(tmp);
copyClipRectFromRegion();
return true;
#else
return false;
#endif
}
bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) { bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::Op op) {
Rect r(left, top, right, bottom); Rect r(left, top, right, bottom);
transform->mapRect(r); transform->mapRect(r);
@@ -77,32 +162,46 @@ bool Snapshot::clip(float left, float top, float right, float bottom, SkRegion::
bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) { bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
bool clipped = false; bool clipped = false;
// NOTE: The unimplemented operations require support for regions
// Supporting regions would require using a stencil buffer instead
// of the scissor. The stencil buffer itself is not too expensive
// (memory cost excluded) but on fillrate limited devices, managing
// the stencil might have a negative impact on the framerate.
switch (op) { switch (op) {
case SkRegion::kDifference_Op: case SkRegion::kDifference_Op: {
ensureClipRegion();
clipped = clipRegionNand(r.left, r.top, r.right, r.bottom);
break; break;
case SkRegion::kIntersect_Op: }
clipped = clipRect->intersect(r); case SkRegion::kIntersect_Op: {
if (!clipped) { if (CC_UNLIKELY(clipRegion)) {
clipRect->setEmpty(); clipped = clipRegionOr(r.left, r.top, r.right, r.bottom);
clipped = true; } else {
clipped = clipRect->intersect(r);
if (!clipped) {
clipRect->setEmpty();
clipped = true;
}
} }
break; break;
case SkRegion::kUnion_Op: }
clipped = clipRect->unionWith(r); case SkRegion::kUnion_Op: {
if (CC_UNLIKELY(clipRegion)) {
clipped = clipRegionAnd(r.left, r.top, r.right, r.bottom);
} else {
clipped = clipRect->unionWith(r);
}
break; break;
case SkRegion::kXOR_Op: }
case SkRegion::kXOR_Op: {
ensureClipRegion();
clipped = clipRegionXor(r.left, r.top, r.right, r.bottom);
break; break;
case SkRegion::kReverseDifference_Op: }
case SkRegion::kReverseDifference_Op: {
// TODO!!!!!!!
break; break;
case SkRegion::kReplace_Op: }
clipRect->set(r); case SkRegion::kReplace_Op: {
setClip(r.left, r.top, r.right, r.bottom);
clipped = true; clipped = true;
break; break;
}
} }
if (clipped) { if (clipped) {
@@ -114,6 +213,10 @@ bool Snapshot::clipTransformed(const Rect& r, SkRegion::Op op) {
void Snapshot::setClip(float left, float top, float right, float bottom) { void Snapshot::setClip(float left, float top, float right, float bottom) {
clipRect->set(left, top, right, bottom); clipRect->set(left, top, right, bottom);
if (clipRegion) {
clipRegion->clear();
clipRegion = NULL;
}
flags |= Snapshot::kFlagClipSet; flags |= Snapshot::kFlagClipSet;
} }
@@ -129,8 +232,7 @@ const Rect& Snapshot::getLocalClip() {
void Snapshot::resetClip(float left, float top, float right, float bottom) { void Snapshot::resetClip(float left, float top, float right, float bottom) {
clipRect = &mClipRectRoot; clipRect = &mClipRectRoot;
clipRect->set(left, top, right, bottom); setClip(left, top, right, bottom);
flags |= Snapshot::kFlagClipSet;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@@ -181,7 +181,7 @@ public:
mat4* transform; mat4* transform;
/** /**
* Current clip region. The clip is stored in canvas-space coordinates, * Current clip rect. The clip is stored in canvas-space coordinates,
* (screen-space coordinates in the regular case.) * (screen-space coordinates in the regular case.)
* *
* This is a reference to a rect owned by this snapshot or another * This is a reference to a rect owned by this snapshot or another
@@ -189,6 +189,17 @@ public:
*/ */
Rect* clipRect; Rect* clipRect;
/**
* Current clip region. The clip is stored in canvas-space coordinates,
* (screen-space coordinates in the regular case.)
*
* This is a reference to a region owned by this snapshot or another
* snapshot. This pointer must not be freed. See ::mClipRegionRoot.
*
* This field is used only if STENCIL_BUFFER_SIZE is > 0.
*/
Region* clipRegion;
/** /**
* The ancestor layer's dirty region. * The ancestor layer's dirty region.
* *
@@ -198,10 +209,22 @@ public:
Region* region; Region* region;
private: private:
void ensureClipRegion();
void copyClipRectFromRegion();
bool clipRegionOr(float left, float top, float right, float bottom);
bool clipRegionXor(float left, float top, float right, float bottom);
bool clipRegionAnd(float left, float top, float right, float bottom);
bool clipRegionNand(float left, float top, float right, float bottom);
mat4 mTransformRoot; mat4 mTransformRoot;
Rect mClipRectRoot; Rect mClipRectRoot;
Rect mLocalClip; Rect mLocalClip;
#if STENCIL_BUFFER_SIZE
Region mClipRegionRoot;
#endif
}; // class Snapshot }; // class Snapshot
}; // namespace uirenderer }; // namespace uirenderer

View File

@@ -126,6 +126,9 @@ void Region::addRectUnchecked(int l, int t, int r, int b)
Region& Region::orSelf(const Rect& r) { Region& Region::orSelf(const Rect& r) {
return operationSelf(r, op_or); return operationSelf(r, op_or);
} }
Region& Region::xorSelf(const Rect& r) {
return operationSelf(r, op_xor);
}
Region& Region::andSelf(const Rect& r) { Region& Region::andSelf(const Rect& r) {
return operationSelf(r, op_and); return operationSelf(r, op_and);
} }
@@ -143,6 +146,9 @@ Region& Region::operationSelf(const Rect& r, int op) {
Region& Region::orSelf(const Region& rhs) { Region& Region::orSelf(const Region& rhs) {
return operationSelf(rhs, op_or); return operationSelf(rhs, op_or);
} }
Region& Region::xorSelf(const Region& rhs) {
return operationSelf(rhs, op_xor);
}
Region& Region::andSelf(const Region& rhs) { Region& Region::andSelf(const Region& rhs) {
return operationSelf(rhs, op_and); return operationSelf(rhs, op_and);
} }
@@ -165,6 +171,9 @@ Region& Region::translateSelf(int x, int y) {
const Region Region::merge(const Rect& rhs) const { const Region Region::merge(const Rect& rhs) const {
return operation(rhs, op_or); return operation(rhs, op_or);
} }
const Region Region::mergeExclusive(const Rect& rhs) const {
return operation(rhs, op_xor);
}
const Region Region::intersect(const Rect& rhs) const { const Region Region::intersect(const Rect& rhs) const {
return operation(rhs, op_and); return operation(rhs, op_and);
} }
@@ -182,6 +191,9 @@ const Region Region::operation(const Rect& rhs, int op) const {
const Region Region::merge(const Region& rhs) const { const Region Region::merge(const Region& rhs) const {
return operation(rhs, op_or); return operation(rhs, op_or);
} }
const Region Region::mergeExclusive(const Region& rhs) const {
return operation(rhs, op_xor);
}
const Region Region::intersect(const Region& rhs) const { const Region Region::intersect(const Region& rhs) const {
return operation(rhs, op_and); return operation(rhs, op_and);
} }
@@ -205,6 +217,9 @@ const Region Region::translate(int x, int y) const {
Region& Region::orSelf(const Region& rhs, int dx, int dy) { Region& Region::orSelf(const Region& rhs, int dx, int dy) {
return operationSelf(rhs, dx, dy, op_or); return operationSelf(rhs, dx, dy, op_or);
} }
Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
return operationSelf(rhs, dx, dy, op_xor);
}
Region& Region::andSelf(const Region& rhs, int dx, int dy) { Region& Region::andSelf(const Region& rhs, int dx, int dy) {
return operationSelf(rhs, dx, dy, op_and); return operationSelf(rhs, dx, dy, op_and);
} }
@@ -222,6 +237,9 @@ Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
const Region Region::merge(const Region& rhs, int dx, int dy) const { const Region Region::merge(const Region& rhs, int dx, int dy) const {
return operation(rhs, dx, dy, op_or); return operation(rhs, dx, dy, op_or);
} }
const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
return operation(rhs, dx, dy, op_xor);
}
const Region Region::intersect(const Region& rhs, int dx, int dy) const { const Region Region::intersect(const Region& rhs, int dx, int dy) const {
return operation(rhs, dx, dy, op_and); return operation(rhs, dx, dy, op_and);
} }
@@ -421,6 +439,7 @@ void Region::boolean_operation(int op, Region& dst,
SkRegion::Op sk_op; SkRegion::Op sk_op;
switch (op) { switch (op) {
case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break; case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break; case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break; case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
} }

View File

@@ -40,6 +40,15 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name="ClipRegionActivity"
android:label="_ClipRegion">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity <activity
android:name="DisplayListLayersActivity" android:name="DisplayListLayersActivity"

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.test.hwui;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Region;
import android.os.Bundle;
import android.view.View;
@SuppressWarnings({"UnusedDeclaration"})
public class ClipRegionActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RegionView view = new RegionView(this);
setContentView(view);
}
public static class RegionView extends View {
public RegionView(Context c) {
super(c);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.clipRect(100.0f, 100.0f, getWidth() - 100.0f, getHeight() - 100.0f,
Region.Op.DIFFERENCE);
canvas.drawARGB(255, 255, 0, 0);
canvas.restore();
}
}
}