19779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian/*
29779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * Copyright (C) 2007 The Android Open Source Project
39779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
49779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
59779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * you may not use this file except in compliance with the License.
69779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * You may obtain a copy of the License at
79779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
89779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
99779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
109779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * Unless required by applicable law or agreed to in writing, software
119779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
129779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * See the License for the specific language governing permissions and
149779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * limitations under the License.
159779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian */
169779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
17000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#ifndef ANDROID_SF_SHARED_BUFFER_STACK_H
18000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#define ANDROID_SF_SHARED_BUFFER_STACK_H
199779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
209779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <stdint.h>
219779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <sys/types.h>
229779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
239779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <cutils/compiler.h>
249779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
259779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <utils/Debug.h>
269779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <utils/threads.h>
279779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <utils/String8.h>
289779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
299779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian#include <ui/Rect.h>
309779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
319779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopiannamespace android {
329779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ---------------------------------------------------------------------------
339779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
349779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian/*
359779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * These classes manage a stack of buffers in shared memory.
369779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
379779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * SharedClient: represents a client with several stacks
389779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * SharedBufferStack: represents a stack of buffers
399779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * SharedBufferClient: manipulates the SharedBufferStack from the client side
409779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * SharedBufferServer: manipulates the SharedBufferStack from the server side
419779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
429779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * Buffers can be dequeued until there are none available, they can be locked
439779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * unless they are in use by the server, which is only the case for the last
449779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * dequeue-able buffer. When these various conditions are not met, the caller
459779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * waits until the condition is met.
469779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
479779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
489779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * CAVEATS:
499779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
509779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * In the current implementation there are several limitations:
519779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * - buffers must be locked in the same order they've been dequeued
529779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * - buffers must be enqueued in the same order they've been locked
539779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * - dequeue() is not reentrant
549779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian * - no error checks are done on the condition above
559779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian *
569779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian */
579779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
589779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// When changing these values, the COMPILE_TIME_ASSERT at the end of this
599779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// file need to be updated.
609779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianconst unsigned int NUM_LAYERS_MAX  = 31;
619779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianconst unsigned int NUM_BUFFER_MAX  = 4;
629779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianconst unsigned int NUM_DISPLAY_MAX = 4;
639779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
649779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ----------------------------------------------------------------------------
659779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
669779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass Region;
679779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedBufferStack;
689779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedClient;
699779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
709779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ----------------------------------------------------------------------------
719779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
729779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// should be 128 bytes (32 longs)
739779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedBufferStack
749779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
759779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedClient;
769779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferBase;
779779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferClient;
789779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferServer;
799779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
809779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianpublic:
81bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    struct FlatRegion { // 12 bytes
82bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        static const unsigned int NUM_RECT_MAX = 1;
83bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        uint32_t    count;
84bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        uint16_t    rects[4*NUM_RECT_MAX];
85bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    };
86bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian
87bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    struct Statistics { // 4 longs
88bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        typedef int32_t usecs_t;
89bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        usecs_t  totalTime;
90bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian        usecs_t  reserved[3];
91bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    };
92bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian
939779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedBufferStack();
94248b5bd51e325107f8119b564db6a06ac51c232aMathias Agopian    void init(int32_t identity);
959779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t setDirtyRegion(int buffer, const Region& reg);
969779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Region getDirtyRegion(int buffer) const;
979779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
989779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    // these attributes are part of the conditions/updates
999779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    volatile int32_t head;      // server's current front buffer
1009779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    volatile int32_t available; // number of dequeue-able buffers
1019779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    volatile int32_t queued;    // number of buffers waiting for post
1029779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    volatile int32_t inUse;     // buffer currently in use by SF
103436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian    volatile status_t status;   // surface's status code
1049779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1059779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    // not part of the conditions
1069779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    volatile int32_t reallocMask;
1079779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1089779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    int32_t     identity;       // surface's identity (const)
109bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    int32_t     reserved32[9];
110bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    Statistics  stats;
1119779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    FlatRegion  dirtyRegion[NUM_BUFFER_MAX];    // 12*4=48 bytes
1129779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
1139779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1149779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ----------------------------------------------------------------------------
1159779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1169779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// 4 KB max
1179779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedClient
1189779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
1199779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianpublic:
1209779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedClient();
1219779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    ~SharedClient();
1229779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1239779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t validate(size_t token) const;
1249779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint32_t getIdentity(size_t token) const;
1259779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1269779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianprivate:
1279779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferBase;
1289779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferClient;
1299779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend class SharedBufferServer;
1309779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1319779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    // FIXME: this should be replaced by a lock-less primitive
1329779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Mutex lock;
1339779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Condition cv;
1349779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedBufferStack surfaces[ NUM_LAYERS_MAX ];
1359779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
1369779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1379779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ============================================================================
1389779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1399779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedBufferBase
1409779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
1419779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianpublic:
1424961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    SharedBufferBase(SharedClient* sharedClient, int surface, int num,
1434961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian            int32_t identity);
1449779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    ~SharedBufferBase();
1459779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint32_t getIdentity();
1460c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian    status_t getStatus() const;
1479779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    size_t getFrontBuffer() const;
1489779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    String8 dump(char const* prefix) const;
1499779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1509779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianprotected:
1519779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedClient* const mSharedClient;
1529779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedBufferStack* const mSharedStack;
1539779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    const int mNumBuffers;
1544961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    const int mIdentity;
1559779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1569779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend struct Update;
1579779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend struct QueueUpdate;
1589779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1599779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct ConditionBase {
1609779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        SharedBufferStack& stack;
1619779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ConditionBase(SharedBufferBase* sbc)
1629779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian            : stack(*sbc->mSharedStack) { }
1639779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
1649779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1659779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct UpdateBase {
1669779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        SharedBufferStack& stack;
1679779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline UpdateBase(SharedBufferBase* sbb)
1689779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian            : stack(*sbb->mSharedStack) { }
1699779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
1709779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1719779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    template <typename T>
1729779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t waitForCondition(T condition);
1739779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1749779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    template <typename T>
1759779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t updateCondition(T update);
1769779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
1779779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1789779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopiantemplate <typename T>
1799779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianstatus_t SharedBufferBase::waitForCondition(T condition)
1809779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
181436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian    const SharedBufferStack& stack( *mSharedStack );
1829779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedClient& client( *mSharedClient );
1830c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian    const nsecs_t TIMEOUT = s2ns(1);
1849779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Mutex::Autolock _l(client.lock);
1854961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    while ((condition()==false) &&
1864961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian            (stack.identity == mIdentity) &&
1874961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian            (stack.status == NO_ERROR))
1884961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    {
1899779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        status_t err = client.cv.waitRelative(client.lock, TIMEOUT);
1909779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
1919779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        // handle errors and timeouts
1929779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        if (CC_UNLIKELY(err != NO_ERROR)) {
1939779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian            if (err == TIMED_OUT) {
1949779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                if (condition()) {
1959779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                    LOGE("waitForCondition(%s) timed out (identity=%d), "
1960c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian                        "but condition is true! We recovered but it "
1970c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian                        "shouldn't happen." , T::name(),
1984961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian                        stack.identity);
1999779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                    break;
2009779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                } else {
2010c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian                    LOGW("waitForCondition(%s) timed out "
2020c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian                        "(identity=%d, status=%d). "
2030c4cec7e4df87181486d280c98fba9c0f4774c37Mathias Agopian                        "CPU may be pegged. trying again.", T::name(),
2044961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian                        stack.identity, stack.status);
2059779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                }
2069779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian            } else {
2079779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                LOGE("waitForCondition(%s) error (%s) ",
2089779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                        T::name(), strerror(-err));
2099779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian                return err;
2109779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian            }
2119779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        }
2129779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    }
2134961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    return (stack.identity != mIdentity) ? status_t(BAD_INDEX) : stack.status;
2149779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian}
2159779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2169779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2179779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopiantemplate <typename T>
2189779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianstatus_t SharedBufferBase::updateCondition(T update) {
2199779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    SharedClient& client( *mSharedClient );
2209779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Mutex::Autolock _l(client.lock);
2219779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    ssize_t result = update();
2229779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    client.cv.broadcast();
2239779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    return result;
2249779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian}
2259779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2269779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ----------------------------------------------------------------------------
2279779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2289779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedBufferClient : public SharedBufferBase
2299779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
2309779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianpublic:
2314961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian    SharedBufferClient(SharedClient* sharedClient, int surface, int num,
2324961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian            int32_t identity);
2334961c959aebac31991fd7653853d47dfd79d3472Mathias Agopian
2349779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    ssize_t dequeue();
2359779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t undoDequeue(int buf);
2369779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2379779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t lock(int buf);
2389779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t queue(int buf);
2399779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    bool needNewBuffer(int buffer) const;
2409779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t setDirtyRegion(int buffer, const Region& reg);
241bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian
2429779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianprivate:
2439779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend struct Condition;
2449779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend struct DequeueCondition;
2459779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    friend struct LockCondition;
246bd8527110caa5427ade0edd7ce0d00e63f15ccbcMathias Agopian
247bd8527110caa5427ade0edd7ce0d00e63f15ccbcMathias Agopian    int32_t computeTail() const;
2489779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2499779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct QueueUpdate : public UpdateBase {
2509779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline QueueUpdate(SharedBufferBase* sbb);
2519779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ssize_t operator()();
2529779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
2539779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2549779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct UndoDequeueUpdate : public UpdateBase {
2559779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline UndoDequeueUpdate(SharedBufferBase* sbb);
2569779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ssize_t operator()();
2579779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
2589779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2599779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    // --
2609779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2619779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct DequeueCondition : public ConditionBase {
2629779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline DequeueCondition(SharedBufferClient* sbc);
2639779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline bool operator()();
2649779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        static inline const char* name() { return "DequeueCondition"; }
2659779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
2669779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2679779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct LockCondition : public ConditionBase {
2689779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        int buf;
2699779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline LockCondition(SharedBufferClient* sbc, int buf);
2709779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline bool operator()();
2719779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        static inline const char* name() { return "LockCondition"; }
2729779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
2739779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2749779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    int32_t tail;
275bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    // statistics...
276bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    nsecs_t mDequeueTime[NUM_BUFFER_MAX];
2779779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
2789779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2799779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ----------------------------------------------------------------------------
2809779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2819779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianclass SharedBufferServer : public SharedBufferBase
2829779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
2839779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianpublic:
284248b5bd51e325107f8119b564db6a06ac51c232aMathias Agopian    SharedBufferServer(SharedClient* sharedClient, int surface, int num,
285248b5bd51e325107f8119b564db6a06ac51c232aMathias Agopian            int32_t identity);
2869779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
2879779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    ssize_t retireAndLock();
2889779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t unlock(int buffer);
289436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian    void setStatus(status_t status);
2909779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t reallocate();
2919779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    status_t assertReallocate(int buffer);
292e05f07dffa196d6403733b26317faa9f267d518fMathias Agopian    int32_t getQueuedCount() const;
293436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian
2949779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    Region getDirtyRegion(int buffer) const;
2959779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
296bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian    SharedBufferStack::Statistics getStats() const;
297bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian
298bcef9ac35da08b9f7f8a4728af94c23a7a010669Mathias Agopian
2999779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianprivate:
3009779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct UnlockUpdate : public UpdateBase {
3019779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        const int lockedBuffer;
3029779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline UnlockUpdate(SharedBufferBase* sbb, int lockedBuffer);
3039779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ssize_t operator()();
3049779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
3059779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3069779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct RetireUpdate : public UpdateBase {
3079779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        const int numBuffers;
3089779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline RetireUpdate(SharedBufferBase* sbb, int numBuffers);
3099779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ssize_t operator()();
3109779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
3119779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
312436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian    struct StatusUpdate : public UpdateBase {
313436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian        const status_t status;
314436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian        inline StatusUpdate(SharedBufferBase* sbb, status_t status);
315436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian        inline ssize_t operator()();
316436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian    };
317436c627ca4228284caf363f0cb35947e58b841b9Mathias Agopian
3189779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    struct ReallocateCondition : public ConditionBase {
3199779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        int buf;
3209779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline ReallocateCondition(SharedBufferBase* sbb, int buf);
3219779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        inline bool operator()();
3229779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian        static inline const char* name() { return "ReallocateCondition"; }
3239779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    };
3249779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
3259779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3269779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ===========================================================================
3279779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3289779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianstruct display_cblk_t
3299779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
3309779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint16_t    w;
3319779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint16_t    h;
3329779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint8_t     format;
3339779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint8_t     orientation;
3349779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint8_t     reserved[2];
3359779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    float       fps;
3369779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    float       density;
3379779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    float       xdpi;
3389779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    float       ydpi;
3399779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint32_t    pad[2];
3409779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
3419779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3429779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopianstruct surface_flinger_cblk_t   // 4KB max
3439779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian{
3449779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint8_t         connected;
3459779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint8_t         reserved[3];
3469779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    uint32_t        pad[7];
3479779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian    display_cblk_t  displays[NUM_DISPLAY_MAX];
3489779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian};
3499779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3509779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ---------------------------------------------------------------------------
3519779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3529779b221e999583ff89e0dfc40e56398737adbb3Mathias AgopianCOMPILE_TIME_ASSERT(sizeof(SharedClient) <= 4096)
3539779b221e999583ff89e0dfc40e56398737adbb3Mathias AgopianCOMPILE_TIME_ASSERT(sizeof(SharedBufferStack) == 128)
3549779b221e999583ff89e0dfc40e56398737adbb3Mathias AgopianCOMPILE_TIME_ASSERT(sizeof(surface_flinger_cblk_t) <= 4096)
3559779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
3569779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian// ---------------------------------------------------------------------------
3579779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian}; // namespace android
3589779b221e999583ff89e0dfc40e56398737adbb3Mathias Agopian
359000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#endif /* ANDROID_SF_SHARED_BUFFER_STACK_H */
360