Android Native draw a window

拿到繪制好的buffer,并調(diào)用native繪制接口

        void *base;
        result = outBuffer->lock(android::GraphicBuffer::USAGE_SW_READ_OFTEN,
                                                 &base);
        if (base == nullptr || result != 0) {
            RTC_LOG(LS_INFO) << "Webrtc onFrameCallback base null";
            return;
        }
        client_->onFrameCaptured(reinterpret_cast<uint8_t *>(base),outBuffer->getStride(),region);
        outBuffer->unlock();

進行native繪制

void onFrameCaptured(uint8_t *data,int srcStride,Region &changeRegion);

void AndroidCallClient::onFrameCaptured(uint8_t *data, int srcStride, Region &changedRegion) {
        //webrtc::MutexLock lock(&pc_mutex_);
        ANativeWindow_Buffer outBuffer;
        int surfaceWidth = mRight - mLeft;
        int surfaceHeight = mBottom - mTop;
        if (mDisplayWidth <= 0) {
            return;
        }
        {
            webrtc::MutexLock lock(&pc_mutex_);
            if (!allowDrawPicture) {
                return;
            }
        }

        if (nativeWindow == nullptr) {
            return;
        }
        ANativeWindow_setBuffersGeometry(nativeWindow, surfaceWidth, surfaceHeight,
                                         WINDOW_FORMAT_RGBA_8888);
        ANativeWindow_acquire(nativeWindow);
        if (ANativeWindow_lock(nativeWindow, &outBuffer, nullptr) < 0) {
            ANativeWindow_release(nativeWindow);
            nativeWindow = nullptr;
            return;
        }

        //draw picture
        uint8_t *dst_data = static_cast<uint8_t *>(outBuffer.bits);
        int srcLineSize = srcStride * 4;
        int dstLineSize = outBuffer.stride * 4;
        libyuv::ARGBScale(data,
                          srcLineSize,
                          mDisplayWidth,
                          mDisplayHeight,
                          dst_data,
                          dstLineSize,
                          surfaceWidth,
                          surfaceHeight,
                          libyuv::kFilterNone);

        //draw red rect
        size_t count = 0;
        int alpha = 0xFF;
        int red = 0xFF;
        int green = 0x00;
        int blue = 0x00;
        int colorABGR = (alpha << 24) | (blue << 16) | (green << 8) | red;
        int x, y, left, top, right, bottom;
        const Rect *allRects = changedRegion.getArray(&count);
        if (count > 0) {
            float wRatio = surfaceWidth * 1.0 / mDisplayWidth;
            float hRatio = surfaceHeight * 1.0 / mDisplayHeight;
            RTC_LOG(LS_ERROR)
            << "rect count:" << count << ",wRatio:" << wRatio << ",hRatio:" << hRatio;
            for (int i = 0; i < count; i++) {
                Rect rect = allRects[i];
                if ((rect.left < 0 || rect.right <= 0 || rect.top < 0 || rect.bottom <= 0)
                    || (rect.right <= rect.left)
                    || (rect.bottom <= rect.top)) {
                    RTC_LOG(LS_ERROR) << "invalid rect,ignore";
                    continue;
                }
                left = (int) (rect.left * wRatio);
                top = (int) (rect.top * hRatio);
                right = (int) (rect.right * wRatio);
                bottom = (int) (rect.bottom * hRatio);
                if (right >= surfaceWidth) right = surfaceWidth - 1;
                if (bottom >= surfaceHeight) bottom = surfaceHeight - 1;

                //draw RED rectangle
                uint32_t *line = (uint32_t *) outBuffer.bits;

                //move buffer's Y position to top
                for (y = 0; y < top; y++) {
                    line = line + outBuffer.stride;
                }

                //top border
                for (x = left; x < right; x++) {
                    line[x] = colorABGR;
                }

                for (y = top; y < bottom; y++) {
                    line[left] = colorABGR;  //left border
                    line[right] = colorABGR;  //right border
                    line = line + outBuffer.stride;
                }

                //bottom border
                for (x = left; x < right; x++) {
                    line[x] = colorABGR;
                }
            }
        }

        if (ANativeWindow_unlockAndPost(nativeWindow) < 0) {
            RTC_LOG(LS_ERROR) << "ANativeWindow_unlockAndPost failed";
        }
    }

release native window

void releaseNativeWindow(JNIEnv* env);
void AndroidCallClient::releaseNativeWindow(JNIEnv *env) {
        webrtc::MutexLock lock(&pc_mutex_);

        if (false) {
            RTC_LOG(LS_WARNING) << "begin to download leak stack.";
            leaktracer::MemoryTrace::GetInstance().stopAllMonitoring();
            std::ofstream out;
            out.open("/sdcard/leaktracer.log", std::ios_base::out);

            if (out.is_open()) {
                leaktracer::MemoryTrace::GetInstance().writeLeaks(out);
            } else {
                RTC_LOG(LS_WARNING) << "Failed to write to /sdcard/leaktracer.log.";
            }
        }

        allowDrawPicture = false;
        RTC_LOG(LS_INFO) << "releaseNativeWindow";
        if (nativeWindow != nullptr) {
            RTC_LOG(LS_INFO) << "ANativeWindow_release ";
            ANativeWindow_release(nativeWindow);
            nativeWindow = nullptr;
        }
    }

setsurface init native window

void AndroidCallClient::SetSurface(JNIEnv *env,
                                       jobject surface,
                                       jint format,
                                       jint left,
                                       jint top,
                                       jint right,
                                       jint bottom,
                                       jint displayWidth,
                                       jint displayHeight) {
        RTC_LOG(LS_INFO)
        << "SetSurface:" << format << "," << left << "," << top << "," << right << "," << bottom
        << "," << displayWidth << "," << displayHeight;
        {
            webrtc::MutexLock lock(&pc_mutex_);
            allowDrawPicture = false;
        }
        if ((right - left) > displayWidth / 2) {
            RTC_LOG(LS_ERROR) << "surface is not determined,ignore";
            return;
        }
        if (nativeWindow != nullptr) {
            RTC_LOG(LS_INFO) << "ANativeWindow_release ";
            ANativeWindow_release(nativeWindow);
            nativeWindow = nullptr;
        }
        nativeWindow = ANativeWindow_fromSurface(env, surface);
        mLeft = left;
        mTop = top;
        mRight = right;
        mBottom = bottom;
        mFormat = format;
        mDisplayWidth = displayWidth;
        mDisplayHeight = displayHeight;
        {
            webrtc::MutexLock lock(&pc_mutex_);
            allowDrawPicture = true;
        }
        RTC_LOG(LS_INFO)
        << "set native Window:" << "width:" << (mRight - mLeft) << ",height:" << (mBottom - mTop);
    }

釋放資源

        if (nativeWindow != nullptr) {
           ANativeWindow_release(nativeWindow);
           nativeWindow = nullptr;
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,007評論 2 59
  • 最近在準(zhǔn)備android面試,整理了下相關(guān)的面試題,分為如下三個部分:android部分、Java部分、算法面試題...
    JasmineBen閱讀 7,511評論 10 137
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,335評論 0 17
  • 原文地址:https://juejin.cn/post/6844904190314037255[https://j...
    QM閱讀 366評論 0 2
  • 前言 經(jīng)過探索,讓我們理解了整個SF的消費者和生產(chǎn)者之間的關(guān)系。我們繼續(xù)根據(jù)開機動畫,來看看Android對Ope...
    yjy239閱讀 9,067評論 6 13

友情鏈接更多精彩內(nèi)容