Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "PixelBuffer.h"
     18 
     19 #include "Debug.h"
     20 #include "Extensions.h"
     21 #include "Properties.h"
     22 #include "renderstate/RenderState.h"
     23 #include "utils/GLUtils.h"
     24 
     25 #include <utils/Log.h>
     26 
     27 namespace android {
     28 namespace uirenderer {
     29 
     30 ///////////////////////////////////////////////////////////////////////////////
     31 // CPU pixel buffer
     32 ///////////////////////////////////////////////////////////////////////////////
     33 
     34 class CpuPixelBuffer: public PixelBuffer {
     35 public:
     36     CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
     37 
     38     uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
     39 
     40     uint8_t* getMappedPointer() const override;
     41 
     42     void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
     43 
     44 protected:
     45     void unmap() override;
     46 
     47 private:
     48     std::unique_ptr<uint8_t[]> mBuffer;
     49 };
     50 
     51 CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
     52         : PixelBuffer(format, width, height)
     53         , mBuffer(new uint8_t[width * height * formatSize(format)]) {
     54 }
     55 
     56 uint8_t* CpuPixelBuffer::map(AccessMode mode) {
     57     if (mAccessMode == kAccessMode_None) {
     58         mAccessMode = mode;
     59     }
     60     return mBuffer.get();
     61 }
     62 
     63 void CpuPixelBuffer::unmap() {
     64     mAccessMode = kAccessMode_None;
     65 }
     66 
     67 uint8_t* CpuPixelBuffer::getMappedPointer() const {
     68     return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
     69 }
     70 
     71 void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
     72     glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
     73             mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
     74 }
     75 
     76 ///////////////////////////////////////////////////////////////////////////////
     77 // GPU pixel buffer
     78 ///////////////////////////////////////////////////////////////////////////////
     79 
     80 class GpuPixelBuffer: public PixelBuffer {
     81 public:
     82     GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
     83     ~GpuPixelBuffer();
     84 
     85     uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
     86 
     87     uint8_t* getMappedPointer() const override;
     88 
     89     void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
     90 
     91 protected:
     92     void unmap() override;
     93 
     94 private:
     95     GLuint mBuffer;
     96     uint8_t* mMappedPointer;
     97     Caches& mCaches;
     98 };
     99 
    100 GpuPixelBuffer::GpuPixelBuffer(GLenum format,
    101         uint32_t width, uint32_t height)
    102         : PixelBuffer(format, width, height)
    103         , mMappedPointer(nullptr)
    104         , mCaches(Caches::getInstance()){
    105     glGenBuffers(1, &mBuffer);
    106 
    107     mCaches.pixelBufferState().bind(mBuffer);
    108     glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
    109     mCaches.pixelBufferState().unbind();
    110 }
    111 
    112 GpuPixelBuffer::~GpuPixelBuffer() {
    113     glDeleteBuffers(1, &mBuffer);
    114 }
    115 
    116 uint8_t* GpuPixelBuffer::map(AccessMode mode) {
    117     if (mAccessMode == kAccessMode_None) {
    118         mCaches.pixelBufferState().bind(mBuffer);
    119         mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
    120         if (CC_UNLIKELY(!mMappedPointer)) {
    121             GLUtils::dumpGLErrors();
    122             LOG_ALWAYS_FATAL("Failed to map PBO");
    123         }
    124         mAccessMode = mode;
    125         mCaches.pixelBufferState().unbind();
    126     }
    127 
    128     return mMappedPointer;
    129 }
    130 
    131 void GpuPixelBuffer::unmap() {
    132     if (mAccessMode != kAccessMode_None) {
    133         if (mMappedPointer) {
    134             mCaches.pixelBufferState().bind(mBuffer);
    135             GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
    136             if (status == GL_FALSE) {
    137                 ALOGE("Corrupted GPU pixel buffer");
    138             }
    139         }
    140         mAccessMode = kAccessMode_None;
    141         mMappedPointer = nullptr;
    142     }
    143 }
    144 
    145 uint8_t* GpuPixelBuffer::getMappedPointer() const {
    146     return mMappedPointer;
    147 }
    148 
    149 void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
    150     // If the buffer is not mapped, unmap() will not bind it
    151     mCaches.pixelBufferState().bind(mBuffer);
    152     unmap();
    153     glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
    154             GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
    155     mCaches.pixelBufferState().unbind();
    156 }
    157 
    158 ///////////////////////////////////////////////////////////////////////////////
    159 // Factory
    160 ///////////////////////////////////////////////////////////////////////////////
    161 
    162 PixelBuffer* PixelBuffer::create(GLenum format,
    163         uint32_t width, uint32_t height, BufferType type) {
    164     if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
    165         return new GpuPixelBuffer(format, width, height);
    166     }
    167     return new CpuPixelBuffer(format, width, height);
    168 }
    169 
    170 }; // namespace uirenderer
    171 }; // namespace android
    172