12af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik/*
27031ff68bb998dbbd7caf3b2fd75eef747a86725Romain Guy * Copyright (C) 2013 The Android Open Source Project
32af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik *
42af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
52af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * you may not use this file except in compliance with the License.
62af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * You may obtain a copy of the License at
72af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik *
82af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
92af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik *
102af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * Unless required by applicable law or agreed to in writing, software
112af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
122af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * See the License for the specific language governing permissions and
142af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * limitations under the License.
152af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik */
162af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
172af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik#ifndef ANDROID_HWUI_DISPLAY_OPERATION_H
182af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik#define ANDROID_HWUI_DISPLAY_OPERATION_H
192af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
202af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik#include "OpenGLRenderer.h"
213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "AssetAtlas.h"
22c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "DeferredDisplayList.h"
23db663fe83f976107fd8fd9307d871b37d9e47370Chris Craik#include "DisplayListCanvas.h"
242dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "GammaFontRenderer.h"
252dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "Patch.h"
262dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "RenderNode.h"
2765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include "renderstate/RenderState.h"
283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include "UvMapper.h"
292af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik#include "utils/LinearAllocator.h"
308dfaa4904205772cdceee63ef3989bcdedf1a914Tom Hudson#include "utils/PaintUtils.h"
3114b56a514c9f49b3f9496e247c6cbb31db3e0db8Doris Liu#include "VectorDrawable.h"
322af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
33df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik#include <algorithm>
34df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik
3565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <SkColor.h>
3665fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <SkPath.h>
3765fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <SkPathOps.h>
3865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <SkXfermode.h>
3965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
4065fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <private/hwui/DrawGlInfo.h>
4165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
422af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik// Use OP_LOG for logging with arglist, OP_LOGS if just printing char*
4328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik#define OP_LOGS(s) OP_LOG("%s", (s))
443dc553babe515fbf9bd042dd63a792e5de578ecdChris Craik#define OP_LOG(s, ...) ALOGD( "%*s" s, level * 2, "", __VA_ARGS__ )
452af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
462af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craiknamespace android {
472af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craiknamespace uirenderer {
482af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
492af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik/**
502af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * Structure for storing canvas operations when they are recorded into a DisplayList, so that they
512af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * may be replayed to an OpenGLRenderer.
522af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik *
532af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * To avoid individual memory allocations, DisplayListOps may only be allocated into a
542af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * LinearAllocator's managed memory buffers.  Each pointer held by a DisplayListOp is either a
552af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * pointer into memory also allocated in the LinearAllocator (mostly for text and float buffers) or
562af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * references a externally refcounted object (Sk... and Skia... objects). ~DisplayListOp() is
572af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * never called as LinearAllocators are simply discarded, so no memory management should be done in
582af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik * this class.
592af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik */
602af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass DisplayListOp {
612af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
622af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    // These objects should always be allocated with a LinearAllocator, and never destroyed/deleted.
632af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    // standard new() intentionally not implemented, and delete/deconstructor should never be used.
6451d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    virtual ~DisplayListOp() { LOG_ALWAYS_FATAL("Destructor not supported"); }
6551d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    static void operator delete(void* ptr) { LOG_ALWAYS_FATAL("delete not supported"); }
6651d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    static void* operator new(size_t size) = delete; /** PURPOSELY OMITTED **/
672af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    static void* operator new(size_t size, LinearAllocator& allocator) {
687df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        // FIXME: Quick hack to keep old pipeline working, delete this when
697df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        // we no longer need to support HWUI_NEWOPS := false
707df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        return allocator.alloc<char>(size);
712af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
722af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
732af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    enum OpLogFlag {
742af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        kOpLogFlag_Recurse = 0x1,
752af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        kOpLogFlag_JSON = 0x2 // TODO: add?
762af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    };
772af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
78dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
79dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase            bool useQuickReject) = 0;
80c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
81dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase    virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level,
82dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase            bool useQuickReject) = 0;
83ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
84c5493fb7fa1f6995955c667d4377f2337f2cf465Chris Craik    virtual void output(int level, uint32_t logFlags = 0) const = 0;
852af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
862af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    // NOTE: it would be nice to declare constants and overriding the implementation in each op to
872af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    // point at the constants, but that seems to require a .cpp file
882af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    virtual const char* name() = 0;
892af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
902af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
912af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass StateOp : public DisplayListOp {
922af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
9364bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
94d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
95ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // default behavior only affects immediate, deferrable state, issue directly to renderer
96ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        applyState(deferStruct.mRenderer, saveCount);
97ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
98ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
99c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    /**
100c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     * State operations are applied directly to the renderer, but can cause the deferred drawing op
101c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     * list to flush
102c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     */
10364bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level,
104d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
105ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        applyState(replayStruct.mRenderer, saveCount);
106c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
107c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
1087273daace9303f4662444111c40bb83d3ead4a92Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const = 0;
1092af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
1102af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
1112af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass DrawOp : public DisplayListOp {
112527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikfriend class MergingDrawBatch;
1132af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
114d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    DrawOp(const SkPaint* paint)
1152af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mPaint(paint), mQuickRejected(false) {}
1162af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
11764bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
118d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
119dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase        if (mQuickRejected && CC_LIKELY(useQuickReject)) {
120ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            return;
121c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        }
122c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
123ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        deferStruct.mDeferredList.addDrawOp(deferStruct.mRenderer, this);
124ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
125ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
12664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void replay(ReplayStateStruct& replayStruct, int saveCount, int level,
127d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
128dd671599bed9d3ca28e2c744e8c224e1e15bc914Chet Haase        if (mQuickRejected && CC_LIKELY(useQuickReject)) {
129ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            return;
130c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        }
131c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
132107843de4507b3511006cb9c77b8d0364374385aTom Hudson        applyDraw(replayStruct.mRenderer, replayStruct.mDirty);
133c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
134c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
135107843de4507b3511006cb9c77b8d0364374385aTom Hudson    virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) = 0;
1362af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
137527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    /**
138527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * Draw multiple instances of an operation, must be overidden for operations that merge
139527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     *
140527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * Currently guarantees certain similarities between ops (see MergingDrawBatch::canMergeWith),
141527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * and pure translation transformations. Other guarantees of similarity should be enforced by
142527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * reducing which operations are tagged as mergeable.
143527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     */
144107843de4507b3511006cb9c77b8d0364374385aTom Hudson    virtual void multiDraw(OpenGLRenderer& renderer, Rect& dirty,
145272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck            const std::vector<OpStatePair>& ops, const Rect& bounds) {
146527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        for (unsigned int i = 0; i < ops.size(); i++) {
147c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            renderer.restoreDisplayState(*(ops[i].state), true);
148107843de4507b3511006cb9c77b8d0364374385aTom Hudson            ops[i].op->applyDraw(renderer, dirty);
149527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        }
150527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    }
151527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
15228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    /**
153527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * When this method is invoked the state field is initialized to have the
154527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * final rendering state. We can thus use it to process data as it will be
155527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * used at draw time.
156527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     *
157527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * Additionally, this method allows subclasses to provide defer-time preferences for batching
158527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * and merging.
159527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     *
16028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik     * if a subclass can set deferInfo.mergeable to true, it should implement multiDraw()
161527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     */
16264bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void onDefer(OpenGLRenderer& renderer, DeferInfo& deferInfo,
16364bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe            const DeferredDisplayState& state) {}
1640f6675332c04c74909425d1d328f02b32c0ff40eRomain Guy
1655e49b307eb99269db2db257760508b8efd7bb97dChris Craik    /**
1665e49b307eb99269db2db257760508b8efd7bb97dChris Craik     * Query the conservative, local bounds (unmapped) bounds of the op.
1675e49b307eb99269db2db257760508b8efd7bb97dChris Craik     *
1685e49b307eb99269db2db257760508b8efd7bb97dChris Craik     * returns true if bounds exist
1695e49b307eb99269db2db257760508b8efd7bb97dChris Craik     */
17064bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual bool getLocalBounds(Rect& localBounds) {
171c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        return false;
172c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    }
1732af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
1742af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    // TODO: better refine localbounds usage
1752af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    void setQuickRejected(bool quickRejected) { mQuickRejected = quickRejected; }
1762af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    bool getQuickRejected() { return mQuickRejected; }
1772af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
178c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger    virtual bool hasTextShadow() const {
179c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger        return false;
180c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger    }
181c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger
182527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    inline float strokeWidthOutset() {
183f0a590781b2c3e34132b2011d3956135add73ae0Chris Craik        // since anything AA stroke with less than 1.0 pixel width is drawn with an alpha-reduced
184f0a590781b2c3e34132b2011d3956135add73ae0Chris Craik        // 1.0 stroke, treat 1.0 as minimum.
185f0a590781b2c3e34132b2011d3956135add73ae0Chris Craik
186f0a590781b2c3e34132b2011d3956135add73ae0Chris Craik        // TODO: it would be nice if this could take scale into account, but scale isn't stable
187f0a590781b2c3e34132b2011d3956135add73ae0Chris Craik        // since higher levels of the view hierarchy can change scale out from underneath it.
188df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik        return std::max(mPaint->getStrokeWidth(), 1.0f) * 0.5f;
189e7c69c6fe3eac1fb01126ede550e5dc32979804aChris Craik    }
190c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
1912af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprotected:
19228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    // Helper method for determining op opaqueness. Assumes op fills its bounds in local
19328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    // coordinates, and that paint's alpha is used
194c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    inline bool isOpaqueOverBounds(const DeferredDisplayState& state) {
19528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        // ensure that local bounds cover mapped bounds
19628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        if (!state.mMatrix.isSimple()) return false;
19728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
1982262abbd16e4d6c7c2e40f7d81b02dfe7db846e8Chris Craik        if (state.mRoundRectClipState) return false;
1992262abbd16e4d6c7c2e40f7d81b02dfe7db846e8Chris Craik
20028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        // check state/paint for transparency
201d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        if (mPaint) {
202c5b5f0556b542a22f01d254e6284f69e9eb23e74Chris Craik            if (mPaint->getAlpha() != 0xFF) {
203c5b5f0556b542a22f01d254e6284f69e9eb23e74Chris Craik                return false;
204c5b5f0556b542a22f01d254e6284f69e9eb23e74Chris Craik            }
205d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III            if (mPaint->getShader() && !mPaint->getShader()->isOpaque()) {
206d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III                return false;
207d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III            }
2088dfaa4904205772cdceee63ef3989bcdedf1a914Tom Hudson            if (PaintUtils::isBlendedColorFilter(mPaint->getColorFilter())) {
209d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III                return false;
210d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III            }
211d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        }
212d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III
213d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        if (state.mAlpha != 1.0f) return false;
21428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
215bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(mPaint);
21628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        return (mode == SkXfermode::kSrcOver_Mode ||
21728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik                mode == SkXfermode::kSrc_Mode);
21828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
21928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    }
22028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
22109c2d4fe15fbac2faf8a97ba2cc59132ee12222aDerek Sollenberger    const SkPaint* mPaint;
2222af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    bool mQuickRejected;
2232af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
2242af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
2252af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass DrawBoundedOp : public DrawOp {
2262af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
227d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    DrawBoundedOp(float left, float top, float right, float bottom, const SkPaint* paint)
2282af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : DrawOp(paint), mLocalBounds(left, top, right, bottom) {}
2292af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
230d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    DrawBoundedOp(const Rect& localBounds, const SkPaint* paint)
23141541825bc90dac740e424cdd41a8c997e15cdb7Chris Craik            : DrawOp(paint), mLocalBounds(localBounds) {}
23241541825bc90dac740e424cdd41a8c997e15cdb7Chris Craik
2335d11676414b3606792e23c269cf75b44faa1a2afChris Craik    // Calculates bounds as smallest rect encompassing all points
2345d11676414b3606792e23c269cf75b44faa1a2afChris Craik    // NOTE: requires at least 1 vertex, and doesn't account for stroke size (should be handled in
2355d11676414b3606792e23c269cf75b44faa1a2afChris Craik    // subclass' constructor)
236d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    DrawBoundedOp(const float* points, int count, const SkPaint* paint)
2375d11676414b3606792e23c269cf75b44faa1a2afChris Craik            : DrawOp(paint), mLocalBounds(points[0], points[1], points[0], points[1]) {
2385d11676414b3606792e23c269cf75b44faa1a2afChris Craik        for (int i = 2; i < count; i += 2) {
239df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik            mLocalBounds.left = std::min(mLocalBounds.left, points[i]);
240df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik            mLocalBounds.right = std::max(mLocalBounds.right, points[i]);
241df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik            mLocalBounds.top = std::min(mLocalBounds.top, points[i + 1]);
242df72b63928cc1492b72ba9a4e99d5e714f93ccc6Chris Craik            mLocalBounds.bottom = std::max(mLocalBounds.bottom, points[i + 1]);
2435d11676414b3606792e23c269cf75b44faa1a2afChris Craik        }
2445d11676414b3606792e23c269cf75b44faa1a2afChris Craik    }
2455d11676414b3606792e23c269cf75b44faa1a2afChris Craik
2465d11676414b3606792e23c269cf75b44faa1a2afChris Craik    // default empty constructor for bounds, to be overridden in child constructor body
247d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    DrawBoundedOp(const SkPaint* paint): DrawOp(paint) { }
2482af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
249d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual bool getLocalBounds(Rect& localBounds) override {
2502af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        localBounds.set(mLocalBounds);
251bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        PaintUtils::TextShadow textShadow;
252bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        if (PaintUtils::getTextShadow(mPaint, &textShadow)) {
2539b5a1a28c327e6113d68302b1f0eed1d1c6f6183Romain Guy            Rect shadow(mLocalBounds);
254c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger            shadow.translate(textShadow.dx, textShadow.dx);
255c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger            shadow.outset(textShadow.radius);
2569b5a1a28c327e6113d68302b1f0eed1d1c6f6183Romain Guy            localBounds.unionWith(shadow);
2579b5a1a28c327e6113d68302b1f0eed1d1c6f6183Romain Guy        }
2582af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        return true;
2592af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
2602af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
2612af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprotected:
2622af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    Rect mLocalBounds; // displayed area in LOCAL coord. doesn't incorporate stroke, so check paint
2632af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
2642af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
2652af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik///////////////////////////////////////////////////////////////////////////////
2662af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik// STATE OPERATIONS - these may affect the state of the canvas/renderer, but do
2672af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik//         not directly draw or alter output
2682af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik///////////////////////////////////////////////////////////////////////////////
2692af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
2702af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass SaveOp : public StateOp {
2712af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
2722af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    SaveOp(int flags)
2732af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mFlags(flags) {}
2742af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
27564bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
276d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
277ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        int newSaveCount = deferStruct.mRenderer.save(mFlags);
278ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        deferStruct.mDeferredList.addSave(deferStruct.mRenderer, this, newSaveCount);
279ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
280ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
281d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
2822af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.save(mFlags);
2832af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
2842af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
285d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
2862af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Save flags %x", mFlags);
2872af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
2882af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
289d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "Save"; }
2902af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
291ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int getFlags() const { return mFlags; }
2922af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
2932af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    int mFlags;
2942af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
2952af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
2962af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass RestoreToCountOp : public StateOp {
2972af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
2982af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    RestoreToCountOp(int count)
2992af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mCount(count) {}
3002af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
30164bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
302d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
3037273daace9303f4662444111c40bb83d3ead4a92Chris Craik        deferStruct.mDeferredList.addRestoreToCount(deferStruct.mRenderer,
3047273daace9303f4662444111c40bb83d3ead4a92Chris Craik                this, saveCount + mCount);
305ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        deferStruct.mRenderer.restoreToCount(saveCount + mCount);
306ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
307ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
308d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
3092af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.restoreToCount(saveCount + mCount);
3102af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3112af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
312d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
3132af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Restore to count %d", mCount);
3142af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3152af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
316d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "RestoreToCount"; }
3172af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
3182af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
3192af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    int mCount;
3202af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
3212af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
3222af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass SaveLayerOp : public StateOp {
3232af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
324d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    SaveLayerOp(float left, float top, float right, float bottom, int alpha, int flags)
3253f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            : mArea(left, top, right, bottom)
3263f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            , mPaint(&mCachedPaint)
3273f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            , mFlags(flags)
328d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            , mConvexMask(nullptr) {
329d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger        mCachedPaint.setAlpha(alpha);
330d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    }
331d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger
332d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    SaveLayerOp(float left, float top, float right, float bottom, const SkPaint* paint, int flags)
3333f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            : mArea(left, top, right, bottom)
3343f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            , mPaint(paint)
3353f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik            , mFlags(flags)
336d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            , mConvexMask(nullptr)
3373f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    {}
3382af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
33964bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
340d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
341ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // NOTE: don't bother with actual saveLayer, instead issuing it at flush time
342d90144db52c7297879b950cbbc85137ed123ab5bChris Craik        int newSaveCount = deferStruct.mRenderer.getSaveCount();
343ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        deferStruct.mDeferredList.addSaveLayer(deferStruct.mRenderer, this, newSaveCount);
344d90144db52c7297879b950cbbc85137ed123ab5bChris Craik
345d90144db52c7297879b950cbbc85137ed123ab5bChris Craik        // NOTE: don't issue full saveLayer, since that has side effects/is costly. instead just
346d90144db52c7297879b950cbbc85137ed123ab5bChris Craik        // setup the snapshot for deferral, and re-issue the op at flush time
347d90144db52c7297879b950cbbc85137ed123ab5bChris Craik        deferStruct.mRenderer.saveLayerDeferred(mArea.left, mArea.top, mArea.right, mArea.bottom,
348d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger                mPaint, mFlags);
3492af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3502af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
351d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
3523f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        renderer.saveLayer(mArea.left, mArea.top, mArea.right, mArea.bottom,
3533f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik                mPaint, mFlags, mConvexMask);
3542af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3552af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
356d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
357ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        OP_LOG("SaveLayer%s of area " RECT_STRING,
358ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik                (isSaveLayerAlpha() ? "Alpha" : ""),RECT_ARGS(mArea));
3592af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3602af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
361d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override {
362d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        return isSaveLayerAlpha() ? "SaveLayerAlpha" : "SaveLayer";
363d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    }
364ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
365ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int getFlags() { return mFlags; }
366c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
3673f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    // Called to make SaveLayerOp clip to the provided mask when drawing back/restored
3683f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    void setMask(const SkPath* convexMask) {
3693f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik        mConvexMask = convexMask;
3703f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    }
3713f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
3722af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
373d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    bool isSaveLayerAlpha() const {
374bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(mPaint);
375bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        int alpha = PaintUtils::getAlphaDirect(mPaint);
376d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger        return alpha < 255 && mode == SkXfermode::kSrcOver_Mode;
377ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
378ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
3792af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    Rect mArea;
380d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    const SkPaint* mPaint;
381d44fbe55a9f434cb5bb0e34c143ba1445141990dDerek Sollenberger    SkPaint mCachedPaint;
3822af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    int mFlags;
3833f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik
3843f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    // Convex path, points at data in RenderNode, valid for the duration of the frame only
3853f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    // Only used for masking the SaveLayer which wraps projected RenderNodes
3863f085429fd47ebd32ac2463b3eae2a5a6c17be25Chris Craik    const SkPath* mConvexMask;
3872af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
3882af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
3892af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass TranslateOp : public StateOp {
3902af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
3912af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    TranslateOp(float dx, float dy)
3922af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mDx(dx), mDy(dy) {}
3932af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
394d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
3952af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.translate(mDx, mDy);
3962af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
3972af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
398d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
3992af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Translate by %f %f", mDx, mDy);
4002af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4012af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
402d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "Translate"; }
4032af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4042af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
4052af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mDx;
4062af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mDy;
4072af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
4082af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4092af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass RotateOp : public StateOp {
4102af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
4112af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    RotateOp(float degrees)
4122af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mDegrees(degrees) {}
4132af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
414d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
4152af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.rotate(mDegrees);
4162af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4172af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
418d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
4192af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Rotate by %f degrees", mDegrees);
4202af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4212af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
422d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "Rotate"; }
4232af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4242af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
4252af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mDegrees;
4262af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
4272af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4282af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass ScaleOp : public StateOp {
4292af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
4302af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    ScaleOp(float sx, float sy)
4312af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mSx(sx), mSy(sy) {}
4322af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
433d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
4342af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.scale(mSx, mSy);
4352af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4362af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
437d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
4382af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Scale by %f %f", mSx, mSy);
4392af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4402af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
441d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "Scale"; }
4422af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4432af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
4442af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mSx;
4452af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mSy;
4462af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
4472af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4482af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass SkewOp : public StateOp {
4492af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
4502af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    SkewOp(float sx, float sy)
4512af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mSx(sx), mSy(sy) {}
4522af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
453d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
4542af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.skew(mSx, mSy);
4552af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4562af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
457d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
4582af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("Skew by %f %f", mSx, mSy);
4592af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4602af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
461d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "Skew"; }
4622af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4632af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
4642af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mSx;
4652af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    float mSy;
4662af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
4672af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4682af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass SetMatrixOp : public StateOp {
4692af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
470139088228faa7f3c446af7387e017933998a5570Derek Sollenberger    SetMatrixOp(const SkMatrix& matrix)
4712af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mMatrix(matrix) {}
4722af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
473d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
4746daa13c5fa7577fa1d8371deca446f6ca911f38fChris Craik        // Setting a matrix on a Canvas isn't equivalent to setting a total matrix on the scene.
4756daa13c5fa7577fa1d8371deca446f6ca911f38fChris Craik        // Set a canvas-relative matrix on the renderer instead.
4766daa13c5fa7577fa1d8371deca446f6ca911f38fChris Craik        renderer.setLocalMatrix(mMatrix);
4772af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4782af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
479d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
480139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        if (mMatrix.isIdentity()) {
4814e7b772b733593fbe25c733e95b8dcea293234b6Romain Guy            OP_LOGS("SetMatrix (reset)");
482139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        } else {
483139088228faa7f3c446af7387e017933998a5570Derek Sollenberger            OP_LOG("SetMatrix " SK_MATRIX_STRING, SK_MATRIX_ARGS(&mMatrix));
4844e7b772b733593fbe25c733e95b8dcea293234b6Romain Guy        }
4852af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
4862af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
487d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "SetMatrix"; }
4882af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4892af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
490139088228faa7f3c446af7387e017933998a5570Derek Sollenberger    const SkMatrix mMatrix;
4912af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
4922af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
4932af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikclass ConcatMatrixOp : public StateOp {
4942af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
495139088228faa7f3c446af7387e017933998a5570Derek Sollenberger    ConcatMatrixOp(const SkMatrix& matrix)
4962af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik            : mMatrix(matrix) {}
4972af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
498d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
4992af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.concatMatrix(mMatrix);
5002af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
5012af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
502d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
503139088228faa7f3c446af7387e017933998a5570Derek Sollenberger        OP_LOG("ConcatMatrix " SK_MATRIX_STRING, SK_MATRIX_ARGS(&mMatrix));
5042af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
5052af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
506d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "ConcatMatrix"; }
5072af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
5082af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
509139088228faa7f3c446af7387e017933998a5570Derek Sollenberger    const SkMatrix mMatrix;
5102af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
5112af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
512ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass ClipOp : public StateOp {
513ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikpublic:
514ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    ClipOp(SkRegion::Op op) : mOp(op) {}
515ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
51664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
517d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool useQuickReject) override {
518ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // NOTE: must defer op BEFORE applying state, since it may read clip
519ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        deferStruct.mDeferredList.addClip(deferStruct.mRenderer, this);
520ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
521ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // TODO: Can we avoid applying complex clips at defer time?
522ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        applyState(deferStruct.mRenderer, saveCount);
523ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
524ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
525ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    bool canCauseComplexClip() {
526ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        return ((mOp != SkRegion::kIntersect_Op) && (mOp != SkRegion::kReplace_Op)) || !isRect();
527ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
528ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
529ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikprotected:
530ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    virtual bool isRect() { return false; }
531ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
532ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    SkRegion::Op mOp;
533ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik};
534ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
535ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass ClipRectOp : public ClipOp {
5362af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
5372af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    ClipRectOp(float left, float top, float right, float bottom, SkRegion::Op op)
538ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            : ClipOp(op), mArea(left, top, right, bottom) {}
5392af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
540d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
5412af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.clipRect(mArea.left, mArea.top, mArea.right, mArea.bottom, mOp);
5422af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
5432af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
544d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
5452af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("ClipRect " RECT_STRING, RECT_ARGS(mArea));
5462af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
5472af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
548d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual const char* name() override { return "ClipRect"; }
5492af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
550ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikprotected:
551d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual bool isRect() override { return true; }
552b98a016c6769b9e80d392df22fe77a2fca048d9fChris Craik
5532af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikprivate:
5542af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    Rect mArea;
5552af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik};
5562af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
557ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass ClipPathOp : public ClipOp {
5582af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craikpublic:
559d218a92c0afb8c0d98135b20b52ac87236e1c935Chris Craik    ClipPathOp(const SkPath* path, SkRegion::Op op)
560ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            : ClipOp(op), mPath(path) {}
5612af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
562d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override {
5632af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        renderer.clipPath(mPath, mOp);
5642af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik    }
5652af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik
566d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void output(int level, uint32_t logFlags) const override {
5672af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        SkRect bounds = mPath->getBounds();
5682af4635e4a9e448a65ff541252f8f94bc6ac48e0Chris Craik        OP_LOG("ClipPath bounds " RECT_