Home | History | Annotate | Download | only in transforms
      1 /*
      2  * Copyright (C) 2012 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 package com.android.ide.eclipse.gltrace.state.transforms;
     18 
     19 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.Function;
     20 import com.android.ide.eclipse.gltrace.state.IGLProperty;
     21 
     22 import java.nio.ByteBuffer;
     23 
     24 /**
     25  * A {@link BufferSubDataTransform} updates a portion of the buffer data as specified by
     26  * the {@link Function#glBufferSubData} function.
     27  */
     28 public class BufferSubDataTransform implements IStateTransform {
     29     private final IGLPropertyAccessor mAccessor;
     30     private final int mOffset;
     31 
     32     private final byte[] mSubData;
     33     private byte[] mOldData;
     34     private byte[] mNewData;
     35 
     36     public BufferSubDataTransform(IGLPropertyAccessor accessor, int offset, byte[] data) {
     37         mAccessor = accessor;
     38         mOffset = offset;
     39         mSubData = data;
     40     }
     41 
     42     @Override
     43     public void apply(IGLProperty state) {
     44         IGLProperty property = mAccessor.getProperty(state);
     45         mOldData = (byte[]) property.getValue();
     46 
     47         if (mOldData != null) {
     48             mNewData = new byte[mOldData.length];
     49             ByteBuffer bb = ByteBuffer.wrap(mNewData);
     50 
     51             // copy all of the old buffer
     52             bb.put(mOldData);
     53             bb.rewind();
     54 
     55             // update with the sub buffer data at specified offset
     56             bb.position(mOffset);
     57             bb.put(mSubData);
     58         }
     59 
     60         property.setValue(mNewData);
     61     }
     62 
     63     @Override
     64     public void revert(IGLProperty state) {
     65         if (mOldData != null) {
     66             IGLProperty property = mAccessor.getProperty(state);
     67             property.setValue(mOldData);
     68             mOldData = null;
     69         }
     70     }
     71 
     72     @Override
     73     public IGLProperty getChangedProperty(IGLProperty state) {
     74         return mAccessor.getProperty(state);
     75     }
     76 }
     77